module AsValue::ValueObject::InstanceMethods

Public Class Methods

new(params = {}) click to toggle source
# File lib/as_value/value_object.rb, line 5
def initialize(params = {})
  self.class.send(:attr_accessor, *instance_attributes)

  params.each do |key, value|
    send("#{key}=", value) if instance_attributes.include? key
  end
  
  before_freeze.call(self) unless before_freeze.nil?

  freeze!
end

Public Instance Methods

==(other) click to toggle source
# File lib/as_value/value_object.rb, line 33
def ==(other)
  hash == other.hash
end
Also aliased as: eql?
before_freeze() click to toggle source
# File lib/as_value/value_object.rb, line 17
def before_freeze
  self.class.instance_variable_get(:@before_freeze)
end
eql?(other)
Alias for: ==
freeze!() click to toggle source
# File lib/as_value/value_object.rb, line 39
def freeze!
  instance_attributes.each do |attribute|
    define_singleton_method("#{attribute}=") do |*|
      fail AsValue::ImmutableObjectError,
           "ValueObjects are immutable -- write methods are unavailable."
    end
  end
  self.freeze
end
hash() click to toggle source
# File lib/as_value/value_object.rb, line 29
def hash
  to_h.sort.hash
end
instance_attributes() click to toggle source
# File lib/as_value/value_object.rb, line 21
def instance_attributes
  self.class.instance_variable_get(:@instance_attributes)
end
to_h() click to toggle source
# File lib/as_value/value_object.rb, line 25
def to_h
  Hash[instance_attributes.map { |attribute| [attribute, send(attribute)] }]
end