Module: InheritableAccessors::InheritableOptionAccessor::ClassMethods

Defined in:
lib/inheritable_accessors/inheritable_option_accessor.rb

Instance Method Summary collapse

Instance Method Details

#inheritable_option_accessor(*names) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/inheritable_accessors/inheritable_option_accessor.rb', line 6

def inheritable_option_accessor(*names)
  options = names.pop if names.last.kind_of?(Hash)

  opts_location = options[:for].to_s
  unless respond_to?(opts_location) && send(opts_location).kind_of?(InheritableAccessors::InheritableHash)
    raise ArgumentError, 'requires for: to be kind of InheritableHashAccessor'
  end

  names.each do |name|
    name = name.to_s

    module_eval <<-METHUD, __FILE__, __LINE__
      def #{name}(new_val=nil, &block)
        InheritableAccessors::InheritableOptionAccessor.__inheritable_option(self, #{opts_location}, :#{name}, new_val, &block)
      end

      def self.#{name}(new_val=nil, &block)
        InheritableAccessors::InheritableOptionAccessor.__inheritable_option(self, #{opts_location}, :#{name}, new_val, &block)
      end
    METHUD
  end

end