module RESTinPeace
Public Class Methods
included(base)
click to toggle source
# File lib/rest_in_peace.rb, line 6 def self.included(base) base.send :extend, ClassMethods base.send :include, ActiveModel::Dirty end
new(attributes = {})
click to toggle source
# File lib/rest_in_peace.rb, line 15 def initialize(attributes = {}) set_attributes attributes end
Public Instance Methods
api()
click to toggle source
# File lib/rest_in_peace.rb, line 11 def api self.class.api end
clear_changes()
click to toggle source
# File lib/rest_in_peace.rb, line 37 def clear_changes case when respond_to?(:clear_changes_information) # ActiveModel >= 4.2 clear_changes_information when respond_to?(:reset_changes) # ActiveModel >= 4.0 && <= 4.1 reset_changes else # ActiveModel <= 3.2 return unless @changed_attributes @changed_attributes.clear end end
force_attributes_from_hash(attributes)
click to toggle source
# File lib/rest_in_peace.rb, line 77 def force_attributes_from_hash(attributes) set_attributes attributes clear_changes end
hash_representation_of_object(object)
click to toggle source
# File lib/rest_in_peace.rb, line 82 def hash_representation_of_object(object) return object.payload if object.respond_to?(:payload) return object.map { |element| hash_representation_of_object(element) } if object.is_a?(Array) object end
payload(changes_only = true)
click to toggle source
# File lib/rest_in_peace.rb, line 19 def payload(changes_only = true) hash_representation = { id: id } if changes_only changed.each do |key| value = send(key) hash_representation[key.to_sym] = hash_representation_of_object(value) end else hash_representation.merge!(to_write_only_hash) end if self.class.rip_namespace { id: id, self.class.rip_namespace => hash_representation } else hash_representation end end
set_attributes(attributes)
click to toggle source
# File lib/rest_in_peace.rb, line 66 def set_attributes(attributes) attributes.each do |key, value| next unless respond_to?(key) if respond_to?("#{key}=") send("#{key}=", value) else instance_variable_set("@#{key}", value) end end end
to_h()
click to toggle source
# File lib/rest_in_peace.rb, line 56 def to_h hash_representation = {} self.class.rip_attributes.values.flatten.each do |attr| hash_representation[attr] = send(attr) end hash_representation end
update_attributes(attributes)
click to toggle source
# File lib/rest_in_peace.rb, line 49 def update_attributes(attributes) attributes.each do |key, value| next unless respond_to?("#{key}=") send("#{key}=", value) end end
write_attribute?(attribute)
click to toggle source
# File lib/rest_in_peace.rb, line 88 def write_attribute?(attribute) self.class.rip_attributes[:write].include?(attribute.to_sym) end
Private Instance Methods
to_write_only_hash()
click to toggle source
# File lib/rest_in_peace.rb, line 118 def to_write_only_hash self.class.rip_attributes[:write].inject({}) do |h, attr| h.merge(attr => send(attr)) end end