Module: Virtus::ValueObject::ClassMethods

Defined in:
lib/virtus/value_object.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, options = {}) ⇒ self

Define an attribute on the receiver

The Attribute will have private writer methods (eg., immutable instances)

and be used in equality/equivalence comparisons

Examples:

class GeoLocation
  include Virtus::ValueObject

  attribute :latitude,  Float
  attribute :longitude, Float
end

Returns:

  • (self)

See Also:

  • ClassMethods.attribute


119
120
121
122
# File 'lib/virtus/value_object.rb', line 119

def attribute(name, type, options = {})
  equalizer << name
  super name, type, options.merge(:writer => :private)
end

#equalizerEqualizer

Define and include a module that provides Value Object semantics

Included module will have #inspect, #eql?, #== and #hash methods whose definition is based on the keys argument

Examples:

virtus_class.equalizer

Returns:

  • (Equalizer)

    An Equalizer module which defines #inspect, #eql?, #== and #hash for instances of this class



137
138
139
140
141
142
143
144
# File 'lib/virtus/value_object.rb', line 137

def equalizer
  @equalizer ||=
    begin
      equalizer = Virtus::Equalizer.new(name || inspect)
      include equalizer
      equalizer
    end
end