Module: Virtus::Extensions

Included in:
ClassMethods
Defined in:
lib/virtus/extensions.rb

Overview

Extensions common for both classes and instances

Constant Summary collapse

WRITER_METHOD_REGEXP =
/=\z/.freeze
INVALID_WRITER_METHODS =
%w[ == != === []= attributes= ].to_set.freeze

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)


62
63
64
65
66
67
68
69
# File 'lib/virtus/extensions.rb', line 62

def allowed_writer_methods
  @allowed_writer_methods ||=
    begin
      allowed_writer_methods  = allowed_methods.grep(WRITER_METHOD_REGEXP).to_set
      allowed_writer_methods -= INVALID_WRITER_METHODS
      allowed_writer_methods.freeze
  end
end

#attribute(*args) ⇒ self

Defines an attribute on an object’s class

Examples:

class Book
  include Virtus

  attribute :title,        String
  attribute :author,       String
  attribute :published_at, DateTime
  attribute :page_count,   Integer
  attribute :index                   # defaults to Object
end

Parameters:

  • name (Symbol)

    the name of an attribute

  • type (Class)

    the type class of an attribute

  • options (#to_hash)

    the extra options hash

Returns:

  • (self)

See Also:



51
52
53
54
55
# File 'lib/virtus/extensions.rb', line 51

def attribute(*args)
  attribute = Attribute.build(*args)
  virtus_add_attribute(attribute)
  self
end