Module: EnumeratedAttribute::Attribute::InstanceMethods

Defined in:
lib/enumerated_attribute/attribute/instance_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/enumerated_attribute/attribute/instance_methods.rb', line 4

def self.included(base)

  method_missing_suffix = "enumerated_attribute_#{base.name.gsub(/.+::/,'')}_#{base.hash.abs}".to_sym
  define_method("method_missing_with_#{method_missing_suffix}") do |methId, *args|
    return self.__send__(methId) if define_enumerated_attribute_dynamic_method(methId)
    self.__send__("method_missing_without_#{method_missing_suffix}", methId, *args)
  end

  respond_to_suffix = "enumerated_attribute_#{base.name.gsub(/.+::/,'')}_#{base.hash.abs}".to_sym
  base.class_eval %{
    def respond_to_with_#{respond_to_suffix}?(method, include_private=false)
      self.respond_to_without_#{respond_to_suffix}?(method, include_private) ||
        (!!parse_dynamic_method_parts!(method.to_s) rescue false)
    end
  }, __FILE__, __LINE__

  base.safe_alias_method_chain :method_missing, method_missing_suffix
  base.safe_alias_method_chain :respond_to?, respond_to_suffix

end

Instance Method Details

#enums(attr) ⇒ Object



25
26
27
# File 'lib/enumerated_attribute/attribute/instance_methods.rb', line 25

def enums(attr)
  self.class.enumerated_attributes[attr.to_sym]
end

#initialize_enumerated_attributes(only_if_nil = false) ⇒ Object



30
31
32
33
34
# File 'lib/enumerated_attribute/attribute/instance_methods.rb', line 30

def initialize_enumerated_attributes(only_if_nil = false)
  self.class.enumerated_attributes.each do |k,v|
    self.write_enumerated_attribute(k, v.init_value) unless (only_if_nil && read_enumerated_attribute(k) != nil)
  end
end