Sha256: 9a1fa974aed4161e3f361022d77a62d358ccd10678311d2dd0ce893ff124deeb

Contents?: true

Size: 843 Bytes

Versions: 4

Compression:

Stored size: 843 Bytes

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright Stefan Dorn <mail@muflax.com>, 2016
# License: GNU GPLv3 (or later) <http://www.gnu.org/copyleft/gpl.html>

class Value
  def ==(other)
    self.class == other.class && differences_with(other).empty?
  end
  alias :eql? :==

  def inspect
    "#<#{self.class.name} #{to_h}>"
  end

  def hash
    to_a.hash
  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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
muflax-0.2.5 lib/muflax/value.rb
muflax-0.2.4 lib/muflax/value.rb
muflax-0.2.3 lib/muflax/value.rb
muflax-0.2.2 lib/muflax/value.rb