Module: JsonApiClient::Helpers::Dirty

Included in:
Relationships::Relations, Resource
Defined in:
lib/json_api_client/helpers/dirty.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



58
59
60
61
62
63
64
65
66
# File 'lib/json_api_client/helpers/dirty.rb', line 58

def method_missing(method, *args, &block)
  if method.to_s =~ /^(.*)_changed\?$/
    has_attribute?($1) ? attribute_changed?($1) : nil
  elsif method.to_s =~ /^(.*)_was$/
    has_attribute?($1) ? attribute_was($1) : nil
  else
    super
  end
end

Instance Method Details

#attribute_change(attr) ⇒ Object



52
53
54
# File 'lib/json_api_client/helpers/dirty.rb', line 52

def attribute_change(attr)
  [changed_attributes[attr], attributes[attr]] if attribute_changed?(attr)
end

#attribute_changed?(attr) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/json_api_client/helpers/dirty.rb', line 48

def attribute_changed?(attr)
  changed.include?(attr.to_s)
end

#attribute_was(attr) ⇒ Object

:nodoc:



44
45
46
# File 'lib/json_api_client/helpers/dirty.rb', line 44

def attribute_was(attr) # :nodoc:
  attribute_changed?(attr) ? changed_attributes[attr] : attributes[attr]
end

#attribute_will_change!(attr) ⇒ Object



31
32
33
34
# File 'lib/json_api_client/helpers/dirty.rb', line 31

def attribute_will_change!(attr)
  return if attribute_changed?(attr)
  set_attribute_was(attr, attributes[attr])
end

#changedObject



9
10
11
# File 'lib/json_api_client/helpers/dirty.rb', line 9

def changed
  changed_attributes.keys
end

#changed?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/json_api_client/helpers/dirty.rb', line 5

def changed?
  changed_attributes.present?
end

#changed_attributesObject



13
14
15
# File 'lib/json_api_client/helpers/dirty.rb', line 13

def changed_attributes
  @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end

#clear_changes_informationObject



17
18
19
# File 'lib/json_api_client/helpers/dirty.rb', line 17

def clear_changes_information
  @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
end

#forget_change!(attr) ⇒ Object



21
22
23
# File 'lib/json_api_client/helpers/dirty.rb', line 21

def forget_change!(attr)
  @changed_attributes.delete(attr.to_s)
end

#set_all_attributes_dirtyObject



25
26
27
28
29
# File 'lib/json_api_client/helpers/dirty.rb', line 25

def set_all_attributes_dirty
  attributes.each do |k, v|
    set_attribute_was(k, v)
  end
end

#set_attribute_was(attr, value) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/json_api_client/helpers/dirty.rb', line 36

def set_attribute_was(attr, value)
  begin
    value = value.duplicable? ? value.clone : value
    changed_attributes[attr] = value
  rescue TypeError, NoMethodError
  end
end