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



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

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



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

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

#clear_changed_attributesObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_zuora/fields.rb', line 54

def clear_changed_attributes
  if ActiveSupport.version.to_s.to_f >= 5.2
    clear_changes_information
  else
    changed_attributes.clear
  end

  # 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



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

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

#untracked_attributes=(attributes) ⇒ Object



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

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

#write_attribute(name, value) ⇒ Object



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

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