Module: Mixpannenkoek::ClassInheritableAttribute

Included in:
Base
Defined in:
lib/mixpannenkoek/class_inheritable_attribute.rb

Instance Method Summary collapse

Instance Method Details

#class_inheritable_attribute(*attributes) ⇒ Object



3
4
5
6
7
8
# File 'lib/mixpannenkoek/class_inheritable_attribute.rb', line 3

def class_inheritable_attribute(*attributes)
  attributes.map(&:to_sym).each do |attribute|
    create_setter(attribute)
    create_getter(attribute)
  end
end

#create_getter(attribute) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mixpannenkoek/class_inheritable_attribute.rb', line 19

def create_getter(attribute)
  define_singleton_method(attribute) do
    if @@class_inheritable_attributes[attribute] && @@class_inheritable_attributes[attribute].has_key?(self.name)
      @@class_inheritable_attributes[attribute][self.name]
    elsif superclass.respond_to?(attribute)
      superclass.send(attribute)
    else
      nil
    end
  end
end

#create_setter(attribute) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/mixpannenkoek/class_inheritable_attribute.rb', line 10

def create_setter(attribute)
  define_singleton_method("#{attribute}=") do |value|
    @@class_inheritable_attributes ||= {}
    @@class_inheritable_attributes[attribute] ||= {}

    @@class_inheritable_attributes[attribute][self.name] = value
  end
end