lib/pigeon/option_accessor.rb
# Given a list of names, this declares an option writer which works like
# a combination of cattr_writer and attr_writer, except that defaults
# defined for a class will propagate down to the instances and subclasses,
# but these defaults can be over-ridden in subclasses and instances
# without interference.
Given a list of names, this declares an option writer which works like
a combination of cattr_writer and attr_writer, except that defaults
defined for a class will propagate down to the instances and subclasses,
but these defaults can be over-ridden in subclasses and instances
without interference.
Given a list of names this declares an option writer which works like a combination of cattr_writer and attr_writer except that defaults defined for a class will propagate down to the instances and subclasses but these defaults can be over - ridden in subclasses and instances without interference .
def option_writer(*names, boolean: false, on: [ :class, :instance ])
    names = [ names ].flatten.compact
    on = [ on ].flatten
    names.each do |name|
      iv = :"@#{name}"
      if (on.include?(:class))
        (class << self; self; end).class_eval do
          if (boolean)
            define_method(:"#{name}=") do |value|
              instance_variable_set(iv, value.nil? ? nil : !!value)
            end
          else
            define_method(:"#{name}=") do |value|
              instance_variable_set(iv, value)
            end
          end
        end
      end
      if (on.include?(:instance))
        if (boolean)
          define_method(:"#{name}=") do |value|
            instance_variable_set(iv, value.nil? ? nil : !!value)
          end
        else
          define_method(:"#{name}=") do |value|
            instance_variable_set(iv, value)
          end
        end
      end
    end
  end
def option_writer ( * names , boolean : false , on : [ :class , :instance ] ) names = [ names ] . flatten . compact on = [ on ] . flatten names . each do | name | iv = :" @ #{ name } " if ( on . include? ( :class ) ) ( class << self ; self ; end ) . class_eval do if ( boolean ) define_method ( :" #{ name } = " ) do | value | instance_variable_set ( iv , value . nil? ? nil : ! ! value ) end else define_method ( :" #{ name } = " ) do | value | instance_variable_set ( iv , value ) end end end end if ( on . include? ( :instance ) ) if ( boolean ) define_method ( :" #{ name } = " ) do | value | instance_variable_set ( iv , value . nil? ? nil : ! ! value ) end else define_method ( :" #{ name } = " ) do | value | instance_variable_set ( iv , value ) end end end end end
Given a list of names, this declares an option writer which works like
a combination of cattr_writer and attr_writer, except that defaults
defined for a class will propagate down to the instances and subclasses,
but these defaults can be over-ridden in subclasses and instances
without interference.
Given a list of names this declares an option writer which works like a combination of cattr_writer and attr_writer except that defaults defined for a class will propagate down to the instances and subclasses but these defaults can be over - ridden in subclasses and instances without interference .
names
boolean
on