Module: Virtus::Extensions::Methods

Included in:
ClassMethods
Defined in:
lib/virtus/extensions.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)


82
83
84
85
86
87
88
89
# File 'lib/virtus/extensions.rb', line 82

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(name, type = nil, options = {}) ⇒ self

Defines an attribute on an object’s class or instance

Examples:

class Book
  include Virtus.model

  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, Array, Hash, Axiom::Types::Type, String, Symbol) (defaults to: nil)

    the type class of an attribute

  • options (#to_hash) (defaults to: {})

    the extra options hash

Returns:

  • (self)

See Also:



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

def attribute(name, type = nil, options = {})
  assert_valid_name(name)
  attribute_set << Attribute.build(type, options.merge(:name => name))
  self
end

#values(&block) ⇒ Object

See Also:

  • Virtus.default_value


71
72
73
74
75
# File 'lib/virtus/extensions.rb', line 71

def values(&block)
  private :attributes= if instance_methods.include?(:attributes=)
  yield
  include(Equalizer.new(name, attribute_set.map(&:name)))
end