Module: ActiveZuora::Fields

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_zuora/fields.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attributesObject


26
27
28
29
30
31
32
# File 'lib/active_zuora/fields.rb', line 26

def attributes
  # A requirement of ActiveModel::Attributes.
  # Hash must use string keys.
  attributes = {}
  fields.each { |field| attributes[field.name] = send(field.name) }
  attributes
end

#attributes=(attributes) ⇒ Object


34
35
36
# File 'lib/active_zuora/fields.rb', line 34

def attributes=(attributes)
  attributes.each { |key, value| send("#{key}=", value) }
end

#clear_changed_attributesObject


53
54
55
56
57
58
# File 'lib/active_zuora/fields.rb', line 53

def clear_changed_attributes
  changed_attributes.clear
  # If we have any fields that are also Base objects,
  # clear their attributes as well.
  fields.each { |field| field.clear_changed_attributes(send(field.name)) }
end

#initialize(attributes = {}) ⇒ Object


21
22
23
24
# File 'lib/active_zuora/fields.rb', line 21

def initialize(attributes={})
  # Start with defaults, and override those with the given attributes.
  self.attributes = default_attributes.merge(attributes)
end

#untracked_attributes=(attributes) ⇒ Object


38
39
40
41
42
43
# File 'lib/active_zuora/fields.rb', line 38

def untracked_attributes=(attributes)
  # Loads attributes without tracking dirt.
  self.attributes = attributes
  clear_changed_attributes
  attributes
end

#write_attribute(name, value) ⇒ Object


45
46
47
48
49
50
51
# File 'lib/active_zuora/fields.rb', line 45

def write_attribute(name, value)
  field = get_field!(name)
  value = field.type_cast(value)
  attribute_will_change!(name) if value != send(name)
  instance_variable_set("@#{name}", value)
  value
end