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)


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

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, *args) ⇒ 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


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

def attribute(name, *args)
  equalizer << name
  options = args.last.kind_of?(Hash) ? args.pop : {}
  super name, *args << 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



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

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