Module: Virtus::ClassMethods

Defined in:
lib/virtus/class_methods.rb

Overview

Class methods that are added when you include Virtus

Instance Method Summary collapse

Instance Method Details

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



49
50
51
52
53
54
# File 'lib/virtus/class_methods.rb', line 49

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

#attributesAttributeSet

Returns all the attributes defined on a Class

Examples:

class User
  include Virtus

  attribute :name, String
  attribute :age,  Integer
end

User.attributes  # =>

TODO: implement inspect so the output is not cluttered - solnic

Returns:



73
74
75
76
77
78
79
# File 'lib/virtus/class_methods.rb', line 73

def attributes
  return @attributes if defined?(@attributes)
  superclass  = self.superclass
  method      = __method__
  parent      = superclass.send(method) if superclass.respond_to?(method)
  @attributes = AttributeSet.new(parent)
end