lib/wcc/contentful/indexed_representation.rb in wcc-contentful-0.1.0 vs lib/wcc/contentful/indexed_representation.rb in wcc-contentful-0.2.0

- old
+ new

@@ -30,10 +30,20 @@ def to_json @types.to_json end + def deep_dup + self.class.new(@types.deep_dup) + end + + def ==(other) + my_keys = keys + return false unless my_keys == other.keys + my_keys.all? { |k| self[k] == other[k] } + end + class ContentType ATTRIBUTES = %i[ name content_type fields @@ -55,10 +65,22 @@ end end hash_or_id.each { |k, v| public_send("#{k}=", v) } end + + def deep_dup + dup_hash = + ATTRIBUTES.each_with_object({}) do |att, h| + h[att] = public_send(att) + end + self.class.new(dup_hash) + end + + def ==(other) + ATTRIBUTES.all? { |att| public_send(att) == other.public_send(att) } + end end class Field ATTRIBUTES = %i[ name @@ -94,18 +116,27 @@ if hash_or_id.is_a?(String) @name = hash_or_id return end + unless hash_or_id.is_a?(Hash) + ATTRIBUTES.each { |att| public_send("#{att}=", hash_or_id.public_send(att)) } + return + end + if raw_type = hash_or_id.delete('type') raw_type = raw_type.to_sym unless TYPES.include?(raw_type) raise ArgumentError, "Unknown type #{raw_type}, expected one of: #{TYPES}" end @type = raw_type end hash_or_id.each { |k, v| public_send("#{k}=", v) } + end + + def ==(other) + ATTRIBUTES.all? { |att| public_send(att) == other.public_send(att) } end end end end