Module: VzaarApi::Lib::ActiveObject::Save::InstanceMethods

Defined in:
lib/vzaar_api/lib/active_object.rb

Instance Method Summary collapse

Instance Method Details

#changedObject



55
56
57
# File 'lib/vzaar_api/lib/active_object.rb', line 55

def changed
  changes.keys
end

#changed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/vzaar_api/lib/active_object.rb', line 51

def changed?
  !changes.empty?
end

#changed_attributesObject



59
60
61
62
63
64
65
# File 'lib/vzaar_api/lib/active_object.rb', line 59

def changed_attributes
  {}.tap do |result|
    changes.each do |attr, vals|
      result[attr] = vals[1]
    end
  end
end

#changesObject



67
68
69
# File 'lib/vzaar_api/lib/active_object.rb', line 67

def changes
  @changes ||= {}
end

#has_changed?(attr) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/vzaar_api/lib/active_object.rb', line 71

def has_changed?(attr)
  changed.include? attr.to_sym
end

#saveObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vzaar_api/lib/active_object.rb', line 38

def save
  if changed?
    response = Lib::Api.new.patch(
      resource_url(id, scope_id),
      changed_attributes
    )

    update_from_attributes response.data
    saved!
  end
  true
end