Module: DynamicAttributes

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/dynamic_attributes.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/concerns/dynamic_attributes.rb', line 22

def method_missing(name, *args)
  method_name = name.to_s
  if method_name =~ /^[\w]+\=$/
    self[:dynamic_attributes] = dynamic_attributes.merge(method_name.chop => args[0])
    args[0]
  elsif has_dynamic_attribute?(method_name)
    self[method_name]
  else
    super
  end
end

Instance Method Details

#[](name) ⇒ Object



5
6
7
8
9
10
11
12
# File 'app/models/concerns/dynamic_attributes.rb', line 5

def [](name)
  attribute_name = name.to_s
  if attribute_name == 'dynamic_attributes' || !has_dynamic_attribute?(attribute_name)
    super
  else
    dynamic_attributes[attribute_name]
  end
end

#[]=(key, value) ⇒ Object



14
15
16
17
18
19
20
# File 'app/models/concerns/dynamic_attributes.rb', line 14

def []=(key, value)
  begin
    super
  rescue ActiveModel::MissingAttributeError
    send "#{key}=", value
  end
end

#attributesObject



34
35
36
37
38
# File 'app/models/concerns/dynamic_attributes.rb', line 34

def attributes
  attrs = super
  props = attrs.delete('dynamic_attributes') || {}
  attrs.merge(props)
end

#dynamic_attributesObject



40
41
42
# File 'app/models/concerns/dynamic_attributes.rb', line 40

def dynamic_attributes
  self[:dynamic_attributes] || {}
end