class Tamiyo::Value
Public Class Methods
new(*fields, &block)
click to toggle source
# File lib/tamiyo/value.rb, line 7 def self.new(*fields, &block) Class.new do attr_reader(:hash, *fields) define_method(:initialize) do |*values| raise ArgumentError.new("wrong number of arguments, #{values.size} for #{fields.size}") if fields.size != values.size fields.zip(values) do |field, value| instance_variable_set(:"@#{field}", value) end @hash = self.class.hash ^ values.hash freeze end const_set :VALUE_ATTRS, fields def self.with(hash) unexpected_keys = hash.keys - self::VALUE_ATTRS if unexpected_keys.any? raise ArgumentError.new("Unexpected hash keys: #{unexpected_keys}") end new(*hash.values_at(*self::VALUE_ATTRS)) end def ==(other) eql?(other) end def eql?(other) self.class == other.class && values == other.values end def values self.class::VALUE_ATTRS.map { |field| send(field) } end class_eval &block if block_given? end end
with(hash)
click to toggle source
# File lib/tamiyo/value.rb, line 25 def self.with(hash) unexpected_keys = hash.keys - self::VALUE_ATTRS if unexpected_keys.any? raise ArgumentError.new("Unexpected hash keys: #{unexpected_keys}") end new(*hash.values_at(*self::VALUE_ATTRS)) end
Public Instance Methods
==(other)
click to toggle source
# File lib/tamiyo/value.rb, line 34 def ==(other) eql?(other) end
eql?(other)
click to toggle source
# File lib/tamiyo/value.rb, line 38 def eql?(other) self.class == other.class && values == other.values end
values()
click to toggle source
# File lib/tamiyo/value.rb, line 42 def values self.class::VALUE_ATTRS.map { |field| send(field) } end