Sha256: f09ff0556d610bcd79af2de315844b0a452db91f25183ba7d735a192a62e623a
Contents?: true
Size: 1.43 KB
Versions: 3
Compression:
Stored size: 1.43 KB
Contents
require "fortnox/api/mappers/base/from_json" require "fortnox/api/mappers/base/to_json" module Fortnox module API module Mapper class Base include FromJSON include ToJSON Hash = ->(hash) do hash.each do |key, value| name = Fortnox::API::Mapper::Base.canonical_name_sym( value ) hash[key] = Fortnox::API::Registry[ name ].call( value ) end end Registry.register( :hash, Fortnox::API::Mapper::Base::Hash ) def self.canonical_name_sym( *values ) klass = if values.empty? self elsif values.first.is_a? Class values.first else values.first.class end # For Ruby < 2.4, make sure we don't pass Bignum and Fixnum around klass = Integer if %w(Bignum Fixnum).include?(klass.to_s) # Stringify to avoid warnings on 2.4 klass.name.split('::').last.downcase.to_sym end def diff( entity_hash, parent_hash ) hash_diff( entity_hash[self.class::JSON_ENTITY_WRAPPER], parent_hash[self.class::JSON_ENTITY_WRAPPER] ) end private def hash_diff( hash1, hash2 ) hash1.dup. delete_if{ |k, v| hash2[k] == v }. merge!(hash2.dup.delete_if{ |k, _| hash1.has_key?(k) }) end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
fortnox-api-0.5.2 | lib/fortnox/api/mappers/base.rb |
fortnox-api-0.5.1 | lib/fortnox/api/mappers/base.rb |
fortnox-api-0.5.0 | lib/fortnox/api/mappers/base.rb |