lib/rest_in_peace.rb in rest-in-peace-6.0.0 vs lib/rest_in_peace.rb in rest-in-peace-6.0.2
- old
+ new
@@ -1,11 +1,10 @@
require 'active_support/core_ext/hash/indifferent_access'
require 'rest_in_peace/definition_proxy'
module RESTinPeace
-
def self.included(base)
base.send :extend, ClassMethods
base.send :include, ActiveModel::Dirty
end
@@ -23,11 +22,11 @@
changed.each do |key|
value = send(key)
hash_representation[key.to_sym] = hash_representation_of_object(value)
end
else
- hash_representation.merge! to_h.keep_if { |key| write_attribute?(key) }
+ hash_representation.merge!(to_write_only_hash)
end
if self.class.rip_namespace
{ id: id, self.class.rip_namespace => hash_representation }
else
@@ -54,13 +53,15 @@
end
end
def to_h
hash_representation = {}
+
self.class.rip_attributes.values.flatten.each do |attr|
hash_representation[attr] = send(attr)
end
+
hash_representation
end
def set_attributes(attributes)
attributes.each do |key, value|
@@ -107,8 +108,16 @@
def rip_attributes
@rip_attributes ||= {
read: [],
write: [],
}
+ end
+ end
+
+ private
+
+ def to_write_only_hash
+ self.class.rip_attributes[:write].inject({}) do |h, attr|
+ h.merge(attr => send(attr))
end
end
end