Sha256: 908281b242f4e293449aeea52dca09cf18e7038c15551395493cec3300ad361a
Contents?: true
Size: 1.97 KB
Versions: 2
Compression:
Stored size: 1.97 KB
Contents
module VzaarApi module Lib module ActiveObject module Find def self.included(base) base.extend(ClassMethods) end module ClassMethods def find(id) response = Lib::Api.new.get(resource_url(id)) new response.data end end end module Create def self.included(base) base.extend(ClassMethods) end module ClassMethods def create(attrs) scope_id = attrs.delete :scope_id response = Lib::Api.new.post(resource_url(nil, scope_id), attrs) new response.data.merge(scope_id: scope_id) end end end module Save def self.included(base) base.include(InstanceMethods) end module InstanceMethods 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 def changed? !changes.empty? end def changed changes.keys end def changed_attributes {}.tap do |result| changes.each do |attr, vals| result[attr] = vals[1] end end end def changes @changes ||= {} end def has_changed?(attr) changed.include? attr.to_sym end def saved! @changes = nil end private :saved! end end module Delete def self.included(base) base.include(InstanceMethods) end module InstanceMethods def delete Lib::Api.new.delete(resource_url(id, scope_id)) true end end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
vzaar_api-2.0.2 | lib/vzaar_api/lib/active_object.rb |
vzaar_api-2.0.1 | lib/vzaar_api/lib/active_object.rb |