Class: Value

Inherits:
Object show all
Defined in:
lib/muflax/value.rb

Overview

Copyright Stefan Dorn <[email protected]>, 2016 License: GNU GPLv3 (or later) <www.gnu.org/copyleft/gpl.html>

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.coerce_to_h(v) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/muflax/value.rb', line 32

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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



7
8
9
# File 'lib/muflax/value.rb', line 7

def ==(other)
  self.class == other.class && differences_with(other).empty?
end

#hashObject



16
17
18
# File 'lib/muflax/value.rb', line 16

def hash
  to_a.hash
end

#inspectObject



12
13
14
# File 'lib/muflax/value.rb', line 12

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

#recursive_to_hObject



24
25
26
# File 'lib/muflax/value.rb', line 24

def recursive_to_h
  Hash[to_a.map{|k, v| [k, Value.coerce_to_h(v)]}]
end

#to_aObject



28
29
30
# File 'lib/muflax/value.rb', line 28

def to_a
  self.class.attributes.map {|attr| [attr, send(attr)] }
end

#to_hObject



20
21
22
# File 'lib/muflax/value.rb', line 20

def to_h
  Hash[to_a]
end