Module: Virtus::ClassMethods

Defined in:
lib/virtus/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, options = {}) ⇒ Virtus::Attributes::Object

Defines an attribute on an object’s class.

Usage:

class Book
  include Virtus

  attribute :title,        String
  attribute :author,       String
  attribute :published_at, DateTime
  attribute :page_count,   Integer
end

Parameters:

  • name (Symbol)

    the name of an attribute

  • type (Class)

    the type class of an attribute

  • options (Hash) (defaults to: {})

    the extra options hash

Returns:



28
29
30
31
# File 'lib/virtus/class_methods.rb', line 28

def attribute(name, type, options = {})
  attribute_klass  = Virtus.determine_type(type)
  attributes[name] = attribute_klass.new(name, self, options)
end

#attributesHash

Returns all the attributes defined on a Class.

Returns:

  • (Hash)

    an attributes hash indexed by attribute names



39
40
41
# File 'lib/virtus/class_methods.rb', line 39

def attributes
  @attributes ||= {}
end