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.

Examples:

contact
# => Crm::Contact

contact.first_name
# => "John"

contact['first_name']
# => "John"

contact[:first_name]
# => "John"

contact.unknown_attribute
# => raises NoMethodError

contact['unknown_attribute']
# => nil

contact.attributes
# => {
#  ...
#  'first_name' => 'John',
#  ...
# }

Instance Method Summary collapse

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.

Examples:

contact['first_name']
# => "John"

contact[:first_name]
# => "John"

contact['nonexistent']
# => nil

Parameters:

  • attribute_name (String, Symbol)

Returns:

  • (Object, nil)


78
79
80
# File 'lib/crm/core/mixins/attribute_provider.rb', line 78

def [](attribute_name)
  @attrs[attribute_name.to_s]
end

#attributesHashWithIndifferentAccess

Returns the hash of all attribute names and their values.

Returns:

  • (HashWithIndifferentAccess)


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.

Examples:

contact[:created_at]
# => 2012-05-07 17:15:00 +0200

contact.raw(:created_at)
# => "2012-05-07T15:15:00+00:00"

Returns:

  • (Object, nil)


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

Returns:

  • (Boolean)


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