#!/usr/bin/env ruby # -*- encoding: utf-8 -*- # Copyright Stefan Dorn , 2016 # License: GNU GPLv3 (or later) class Value def ==(other) eql?(other) end def eql?(other) self.class == other.class && differences_with(other).empty? end def inspect "#<#{self.class.name} #{self.class.attributes}>" end def to_h Hash[to_a] end def recursive_to_h Hash[to_a.map{|k, v| [k, Value.coerce_to_h(v)]}] end def to_a self.class.attributes.map {|attr| [attr, send(attr)] } end def self.coerce_to_h(v) case when v.is_a?(Hash) ; Hash[v.map{|hk, hv| [hk, coerce_to_h(hv)]}] when v.respond_to?(:map) ; v.map{|x| coerce_to_h(x)} when v && v.respond_to?(:to_h) ; v.to_h else ; v end end end