Class: Eloqua::RemoteObject

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::MassAssignmentSecurity, ActiveModel::Naming, ActiveModel::Validations, ActiveSupport::Callbacks, Helper::AttributeMap
Defined in:
lib/eloqua/remote_object.rb

Direct Known Subclasses

Asset, Entity

Constant Summary collapse

DIRTY_PRIVATE_METHODS =
[:attribute_was, :attribute_changed?, :attribute_change]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr = {}, remote = false) ⇒ RemoteObject

If the remote flag is set to :remote (or true) the object assumes that the attributes are from eloqua directly in their format (IE: C_EmailAddress) it will then format them to a more ruby-ish key (:email_address) and then store the original name This means if you do not have a #map for the object when you are creating it for the first time the object cannot determine the original eloqua name



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/eloqua/remote_object.rb', line 55

def initialize(attr = {}, remote = false)
  @instance_reverse_keys = attribute_map_reverse.clone
  if(remote)
    @_persisted = true
    attr = map_attributes(attr)
  end
  @attributes = convert_attribute_values(attr).with_indifferent_access
  if(@attributes.has_key?(primary_key) && @attributes[primary_key])
    @_persisted = true
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/eloqua/remote_object.rb', line 184

def method_missing(method, *args)
  attr_method = is_attribute_method?(method)
  attr = method.to_s.gsub(/\=$/, '')
  if(attr_method)
    case attr_method
      when :write then write_attribute(attr, *args)
      when :read then read_attribute(attr)
    end
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



30
31
32
# File 'lib/eloqua/remote_object.rb', line 30

def attributes
  @attributes
end

Class Method Details

.apiObject



245
246
247
# File 'lib/eloqua/remote_object.rb', line 245

def api
  Eloqua::Api::Service
end

.attr_checkbox(*attrs) ⇒ Object



238
239
240
241
242
243
# File 'lib/eloqua/remote_object.rb', line 238

def attr_checkbox(*attrs)
  options = attrs.extract_options!
  attrs.each do |column|
    attribute_types[column] = attr_type_hash(:boolean_checkbox)
  end
end

.attr_type_hash(name) ⇒ Object

Attribute types



230
231
232
233
234
235
236
# File 'lib/eloqua/remote_object.rb', line 230

def attr_type_hash(name)
  {
    :type => name.to_sym,
    :import => "import_#{name}".to_sym,
    :export => "export_#{name}".to_sym
  }
end

.find(id) ⇒ Object



249
250
251
252
253
254
255
256
# File 'lib/eloqua/remote_object.rb', line 249

def find(id)
  result = find_object(id)
  if(result)
    self.new(result, :remote)
  else
    result
  end
end

Instance Method Details

#changed_attributesObject

Monkey Patch. Rails uses a normal array for changed_attributes and relys on method missing to provide the same type all the time



158
159
160
# File 'lib/eloqua/remote_object.rb', line 158

def changed_attributes
  @changed_attributes ||= {}.with_indifferent_access
end

#convert_attribute_values(attributes, convert_type = :import) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/eloqua/remote_object.rb', line 83

def convert_attribute_values(attributes, convert_type = :import)
  attributes = attributes.clone
  attributes.each do |key, value|
    attributes[key] = self.send(attribute_types[key][convert_type], key, value) if(attribute_types.has_key?(key))
  end
  attributes
end

#createObject

Persistence



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/eloqua/remote_object.rb', line 96

def create
  run_callbacks :create do
    attrs = convert_attribute_values(attributes, :export)
    attrs = reverse_map_attributes(attrs)
    result = self.class.create_object(attrs)
    if(result)
      @_persisted = true
      write_attribute(:id, result[:id])
      true
    else
      false
    end
  end
end

#idObject



180
181
182
# File 'lib/eloqua/remote_object.rb', line 180

def id
  read_attribute(:id)
end

#is_attribute_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
# File 'lib/eloqua/remote_object.rb', line 171

def is_attribute_method?(method)
  attr = method.to_s.gsub(/\=$/, '')
  if(attributes.has_key?(attr) || attribute_map_reverse.has_key?(attr))
    attr_type = (method.to_s =~ /\=$/)? :write : :read
  else
    false
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/eloqua/remote_object.rb', line 79

def persisted?
  @_persisted ||= false
end

#read_attribute(attr) ⇒ Object



162
163
164
# File 'lib/eloqua/remote_object.rb', line 162

def read_attribute(attr)
  attributes[attr]
end

#reloadObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/eloqua/remote_object.rb', line 67

def reload
  if(persisted?)
    attr = self.class.find_object(id)
    attr = map_attributes(attr)
    attr = convert_attribute_values(attr)
    @attributes.update(attr)
    changed_attributes.update({}) if changed_attributes
    previous_changes.update({}) if previous_changes
    true
  end
end

#respond_to?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
202
203
# File 'lib/eloqua/remote_object.rb', line 197

def respond_to?(method, *args)
  if(is_attribute_method?(method))
    true
  else
    super
  end
end

#save(options = {}) ⇒ Object Also known as: save!



123
124
125
126
127
128
129
130
131
132
# File 'lib/eloqua/remote_object.rb', line 123

def save(options = {})
  if(valid?)
    run_callbacks :save do
      (persisted?) ? update : create
    end
    true
  else
    false
  end
end

#updateObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/eloqua/remote_object.rb', line 111

def update
  run_callbacks :update do
    update_attributes = changed.inject({}) do |map, attr|
      map[attr] = send(attr.to_sym)
      map
    end
    attrs = convert_attribute_values(update_attributes, :export)
    attrs = reverse_map_attributes(attrs)
    self.class.update_object(self.attributes[primary_key].to_i, attrs)
  end
end

#update_attributes(attrs, ignore_security = false) ⇒ Boolean

Updates the attributes in the record with given hash and then saves the object.

By default uses assignment security provided by ActiveModel. by using ignore_security you can turn this off

Parameters:

  • attributes (Hash)

    to write

  • when (Boolean)

    true ignores assignment security

Returns:

  • (Boolean)

    Result of the save



146
147
148
149
150
151
152
# File 'lib/eloqua/remote_object.rb', line 146

def update_attributes(attrs, ignore_security = false)
  attrs = sanitize_for_mass_assignment(attrs) unless ignore_security
  attrs.each do |key, value|
    write_attribute(key, value)
  end
  save
end

#write_attribute(attr, value) ⇒ Object



166
167
168
169
# File 'lib/eloqua/remote_object.rb', line 166

def write_attribute(attr, value)
  attribute_will_change!(attr) unless read_attribute(attr) == value
  attributes[attr] = value
end