lib/fortnox/api/models/base.rb in fortnox-api-0.8.2 vs lib/fortnox/api/models/base.rb in fortnox-api-0.9.0

- old
+ new

@@ -24,12 +24,12 @@ def self.new(hash = {}) begin obj = preserve_meta_properties(hash) do super(hash) end - rescue Dry::Struct::Error => e - raise Fortnox::API::AttributeError, e + rescue Dry::Struct::Error => exception + raise Fortnox::API::AttributeError, exception end IceNine.deep_freeze(obj) end @@ -50,34 +50,34 @@ self.class.schema.find_all do |_name, attribute| options.all? { |option| attribute.is?(option) } end end - def to_hash(recursive = false) + def to_hash(recursive = false) # rubocop:disable Style/OptionalBooleanParameter return super() if recursive self.class.schema.each_with_object({}) do |key, result| - # Only output attributes that have a value set - result[key.name] = self[key.name] if self.send("#{key.name}?") + # Only output attributes that have a value set + result[key.name] = self[key.name] if send("#{key.name}?") end end def update(hash) old_attributes = to_hash - new_attributes = old_attributes.merge(hash) + new_attributes = old_attributes.merge(hash).compact return self if new_attributes == old_attributes - new_hash = new_attributes.delete_if { |_, value| value.nil? } - new_hash[:new] = @new - new_hash[:parent] = self - self.class.new(new_hash) + new_attributes[:new] = @new + new_attributes[:parent] = self + self.class.new(new_attributes) end # Generic comparison, by value, use .eql? or .equal? for object identity. def ==(other) return false unless other.is_a? self.class + to_hash == other.to_hash end def new? @new @@ -112,9 +112,11 @@ obj.instance_variable_set(:@new, is_new) obj.instance_variable_set(:@parent, parent) obj end + + private_class_method :preserve_meta_properties private def private_attributes @private_attributes ||= attribute_set.reject(&:public_writer?)