Module: Virtus::ValueObject::ClassMethods

Defined in:
lib/virtus/value_object.rb

Instance Method Summary collapse

Instance Method Details

#allowed_writer_methodsSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The list of writer methods that can be mass-assigned to in #attributes=

Returns:

  • (Set)


129
130
131
132
133
134
135
136
# File 'lib/virtus/value_object.rb', line 129

def allowed_writer_methods
  @allowed_writer_methods ||=
    begin
      allowed_writer_methods = super
      allowed_writer_methods += attribute_set.map{|attr| "#{attr.name}="}
      allowed_writer_methods.to_set.freeze
    end
end

#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


96
97
98
99
100
# File 'lib/virtus/value_object.rb', line 96

def attribute(name, type, options = {})
  equalizer << name
  options[:writer] = :private
  super
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



115
116
117
118
119
120
121
122
# File 'lib/virtus/value_object.rb', line 115

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