Module: BraintreeRails::Attributes::InstanceMethods

Defined in:
lib/braintree_rails/attributes.rb

Instance Method Summary collapse

Instance Method Details

#assign_attributes(hash) ⇒ Object



28
29
30
31
32
# File 'lib/braintree_rails/attributes.rb', line 28

def assign_attributes(hash)
  hash.each do |attribute, value|
    send("#{attribute}=", value) if respond_to?("#{attribute}=")
  end
end

#attributesObject



13
14
15
16
17
18
19
20
# File 'lib/braintree_rails/attributes.rb', line 13

def attributes
  self.class.attributes.inject({}) do |hash, attribute|
    if (value = self.send(attribute)).present?
      hash[attribute] = value.respond_to?(:attributes_for) ? value.attributes_for(:as_association) : value
    end
    hash
  end
end

#attributes_for(action) ⇒ Object



22
23
24
25
26
# File 'lib/braintree_rails/attributes.rb', line 22

def attributes_for(action)
  attributes_for_action = attributes.slice(*self.class.attributes_for(action))
  attributes_for_action.slice!(*changed) unless action == :as_association
  attributes_for_action
end

#changedObject



41
42
43
# File 'lib/braintree_rails/attributes.rb', line 41

def changed
  new_record? ? changed_for_new_record : changed_for_persisted
end

#changed_for_new_recordObject



45
46
47
48
49
# File 'lib/braintree_rails/attributes.rb', line 45

def changed_for_new_record
  attributes.map do |attribute, value|
    attribute if value.present?
  end.compact
end

#changed_for_persistedObject



51
52
53
54
55
# File 'lib/braintree_rails/attributes.rb', line 51

def changed_for_persisted
  attributes.map do |attribute, value|
    attribute if !raw_object.respond_to?(attribute) || value != raw_object.send(attribute)
  end.compact
end

#extract_values(obj) ⇒ Object



34
35
36
37
38
39
# File 'lib/braintree_rails/attributes.rb', line 34

def extract_values(obj)
  self.class.attributes.inject({}) do |hash, attr|
    hash[attr] = obj.send(attr) if obj.respond_to?(attr)
    hash
  end
end