module GirlScout::Concerns::HasAttributes
Public Instance Methods
[](key)
click to toggle source
# File lib/girlscout/concerns/has_attributes.rb, line 14 def [](key) @attributes[attr_key(key)] end
[]=(key, value)
click to toggle source
# File lib/girlscout/concerns/has_attributes.rb, line 18 def []=(key, value) @attributes[attr_key(key)] = value end
as_json()
click to toggle source
# File lib/girlscout/concerns/has_attributes.rb, line 10 def as_json @attributes end
attributes()
click to toggle source
# File lib/girlscout/concerns/has_attributes.rb, line 6 def attributes @attributes || {} end
key?(key)
click to toggle source
# File lib/girlscout/concerns/has_attributes.rb, line 22 def key?(key) @attributes.key?(attr_key(key)) end
method_missing(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/girlscout/concerns/has_attributes.rb, line 30 def method_missing(method_name, *args, &block) key = attr_key(method_name) return super unless key?(key) @attributes[key] end
respond_to_missing?(method_name, include_all = false)
click to toggle source
Calls superclass method
# File lib/girlscout/concerns/has_attributes.rb, line 26 def respond_to_missing?(method_name, include_all = false) key?(method_name) || super end
Protected Instance Methods
attr_key(sym)
click to toggle source
# File lib/girlscout/concerns/has_attributes.rb, line 48 def attr_key(sym) parts = sym.to_s.split('_') # camelize w/o active support parts[0] + parts[1..-1].map(&:capitalize).join('') end
normalize_attributes(attr)
click to toggle source
# File lib/girlscout/concerns/has_attributes.rb, line 39 def normalize_attributes(attr) attr ||= @attributes attr = attr.attributes if attr.respond_to?(:attributes) attr.each_with_object({}) do |(k, v), hash| hash[attr_key(k)] = v end end