Module: Virtus

Defined in:
lib/virtus.rb,
lib/virtus/model.rb,
lib/virtus/builder.rb,
lib/virtus/coercer.rb,
lib/virtus/version.rb,
lib/virtus/attribute.rb,
lib/virtus/extensions.rb,
lib/virtus/value_object.rb,
lib/virtus/attribute_set.rb,
lib/virtus/class_methods.rb,
lib/virtus/configuration.rb,
lib/virtus/attribute/hash.rb,
lib/virtus/support/options.rb,
lib/virtus/attribute/strict.rb,
lib/virtus/class_inclusions.rb,
lib/virtus/instance_methods.rb,
lib/virtus/attribute/boolean.rb,
lib/virtus/attribute/builder.rb,
lib/virtus/attribute/coercer.rb,
lib/virtus/module_extensions.rb,
lib/virtus/support/equalizer.rb,
lib/virtus/attribute/accessor.rb,
lib/virtus/attribute/coercible.rb,
lib/virtus/support/type_lookup.rb,
lib/virtus/attribute/collection.rb,
lib/virtus/builder/hook_context.rb,
lib/virtus/attribute/lazy_default.rb,
lib/virtus/attribute/default_value.rb,
lib/virtus/attribute/embedded_value.rb,
lib/virtus/const_missing_extensions.rb,
lib/virtus/attribute/default_value/from_symbol.rb,
lib/virtus/attribute/default_value/from_callable.rb,
lib/virtus/attribute/default_value/from_clonable.rb

Overview

Builds a module for…modules

Examples:


module Common
  include Virtus.module

  attribute :name, String
  attribute :age,  Integer
end

class User
  include Common
end

class Admin
  include Common
end

Returns:

  • (Module)

Defined Under Namespace

Modules: ClassInclusions, ClassMethods, ConstMissingExtensions, Extensions, InstanceMethods, Model, ModuleExtensions, Options, TypeLookup, ValueObject Classes: Attribute, AttributeSet, Builder, Coercer, CoercionError, Configuration, Equalizer, ModelBuilder, ModuleBuilder, PendingAttribute, TypeDefinition, ValueObjectBuilder

Constant Summary collapse

EXTRA_CONST_ARGS =
(RUBY_VERSION < '1.9' ? [] : [ false ]).freeze
Undefined =
Object.new.freeze
VERSION =
'1.0.2'

Class Method Summary collapse

Class Method Details

.coerceObject



89
90
91
# File 'lib/virtus.rb', line 89

def self.coerce
  configuration.coerce
end

.coerce=(value) ⇒ Object



79
80
81
82
# File 'lib/virtus.rb', line 79

def self.coerce=(value)
  configuration.coerce = value
  self
end

.coercer(&block) ⇒ Object



68
69
70
# File 'lib/virtus.rb', line 68

def self.coercer(&block)
  configuration.coercer(&block)
end

.config(&block) ⇒ Object



103
104
105
# File 'lib/virtus.rb', line 103

def self.config(&block)
  configuration.call(&block)
end

.configurationObject

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.

Global configuration instance

@ return [Configuration]



185
186
187
# File 'lib/virtus.rb', line 185

def self.configuration
  @configuration ||= Configuration.new
end

.finalizeArray

Finalize pending attributes

Examples:

class User
  include Virtus.model(:finalize => false)

  attribute :address, 'Address'
end

class Address
  include Virtus.model(:finalize => false)

  attribute :user, 'User'
end

Virtus.finalize # this will resolve constant names

Returns:

  • (Array)

    array of finalized models



209
210
211
212
213
# File 'lib/virtus.rb', line 209

def self.finalize
  Builder.pending.each do |klass|
    klass.attribute_set.finalize
  end
end

.model(options = {}, &block) ⇒ Object



130
131
132
# File 'lib/virtus.rb', line 130

def self.model(options = {}, &block)
  ModelBuilder.call(options, &block)
end

.module(options = {}, &block) ⇒ Object



156
157
158
# File 'lib/virtus.rb', line 156

def self.module(options = {}, &block)
  ModuleBuilder.call(options, &block)
end

.value_object(options = {}, &block) ⇒ Module

Builds a module for value object models

Examples:


class GeoLocation
  include Virtus.value_object

  values do
    attribute :lat, Float
    attribute :lng, Float
  end
end

Returns:

  • (Module)


176
177
178
# File 'lib/virtus.rb', line 176

def self.value_object(options = {}, &block)
  ValueObjectBuilder.call(options, &block)
end

.warn(msg) ⇒ Object

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.



216
217
218
# File 'lib/virtus.rb', line 216

def self.warn(msg)
  Kernel.warn(msg)
end