Sha256: 5a5e1ea80034d14d0ee0699c049db379fdefd73d12b81df6fcfb09c3b555deb0

Contents?: true

Size: 851 Bytes

Versions: 1

Compression:

Stored size: 851 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)
    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

Version data entries

1 entries across 1 versions & 1 rubygems

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