Module: Virtus::Extensions::Methods

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

Instance Method Summary collapse

Instance Method Details

#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:



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

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


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

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