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(name, type, options = {}) ⇒ 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) (defaults to: {})

    the extra options hash

Returns:

  • (self)


45
46
47
48
49
50
# File 'lib/virtus/class_methods.rb', line 45

def attribute(name, type, options = {})
  attribute = Attribute.determine_type(type).new(name, options)
  virtus_define_attribute_methods(attribute)
  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:



69
70
71
72
73
74
75
# File 'lib/virtus/class_methods.rb', line 69

def attributes
  @attributes ||= begin
    superclass = self.superclass
    parent     = superclass.attributes if superclass.respond_to?(:attributes)
    AttributeSet.new(parent)
  end
end