Module: Crm::Core::Mixins::AttributeProvider
- Included in:
- Activity::Comment, BasicResource, ChangeLoggable::Change, ChangeLoggable::Change::Detail, MailingDelivery
- Defined in:
- lib/crm/core/mixins/attribute_provider.rb
Overview
AttributeProvider
provides multiple ways to access the attributes of an item. All attributes are available using #[], #attributes or a method named like the attribute.
Instance Method Summary collapse
-
#[](attribute_name) ⇒ Object?
Returns the value associated with
attribute_name
. -
#attributes ⇒ HashWithIndifferentAccess
Returns the hash of all attribute names and their values.
- #initialize(attributes = nil) ⇒ Object
-
#method_missing(method_name, *args) ⇒ Object
Makes all attributes accessible as methods.
- #methods(*args) ⇒ Object
-
#raw(attribute_name) ⇒ Object?
Returns the value before type cast.
-
#respond_to_missing?(method_name) ⇒ Boolean
Motivation see blog.marc-andre.ca/2010/11/15/methodmissing-politely/.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Makes all attributes accessible as methods.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/crm/core/mixins/attribute_provider.rb', line 39 def method_missing(method_name, *args) return self[method_name] if has_attribute?(method_name) [ "#{method_name}_id", "#{method_name.to_s.singularize}_ids", ].each do |id_name| return Crm.find(self[id_name]) if has_attribute?(id_name) end super end |
Instance Method Details
#[](attribute_name) ⇒ Object?
Returns the value associated with attribute_name
. Returns nil
if not found.
78 79 80 |
# File 'lib/crm/core/mixins/attribute_provider.rb', line 78 def [](attribute_name) @attrs[attribute_name.to_s] end |
#attributes ⇒ HashWithIndifferentAccess
Returns the hash of all attribute names and their values.
85 86 87 |
# File 'lib/crm/core/mixins/attribute_provider.rb', line 85 def attributes @attrs end |
#initialize(attributes = nil) ⇒ Object
32 33 34 35 36 |
# File 'lib/crm/core/mixins/attribute_provider.rb', line 32 def initialize(attributes = nil) load_attributes(attributes || {}) super() end |
#methods(*args) ⇒ Object
60 61 62 |
# File 'lib/crm/core/mixins/attribute_provider.rb', line 60 def methods(*args) super | @extra_methods end |
#raw(attribute_name) ⇒ Object?
Returns the value before type cast. Returns nil
if attribute_name not found.
99 100 101 |
# File 'lib/crm/core/mixins/attribute_provider.rb', line 99 def raw(attribute_name) @raw_attrs[attribute_name] || @attrs[attribute_name] end |
#respond_to_missing?(method_name) ⇒ Boolean
Motivation see blog.marc-andre.ca/2010/11/15/methodmissing-politely/
52 53 54 55 56 57 58 |
# File 'lib/crm/core/mixins/attribute_provider.rb', line 52 def respond_to_missing?(method_name, *) return true if has_attribute?(method_name) return true if has_attribute?("#{method_name}_id") return true if has_attribute?("#{method_name.to_s.singularize}_ids") super end |