Sha256: bc5e7394ddf86247ba9be40159d0ffe8d67b380f0454461c841c45340b13544e

Contents?: true

Size: 843 Bytes

Versions: 1

Compression:

Stored size: 843 Bytes

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright Steffi 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

1 entries across 1 versions & 1 rubygems

Version Path
muflax-0.2.6 lib/muflax/value.rb