Class: Virtus::Attribute Abstract

Inherits:
Object
  • Object
show all
Extended by:
DescendantsTracker, Options, TypeLookup
Defined in:
lib/virtus/attribute.rb,
lib/virtus/attribute/date.rb,
lib/virtus/attribute/hash.rb,
lib/virtus/attribute/time.rb,
lib/virtus/attribute/array.rb,
lib/virtus/attribute/class.rb,
lib/virtus/attribute/float.rb,
lib/virtus/attribute/object.rb,
lib/virtus/attribute/string.rb,
lib/virtus/attribute/boolean.rb,
lib/virtus/attribute/decimal.rb,
lib/virtus/attribute/integer.rb,
lib/virtus/attribute/numeric.rb,
lib/virtus/attribute/date_time.rb,
lib/virtus/attribute/default_value.rb

Overview

This class is abstract.

Abstract class implementing base API for attribute types

Direct Known Subclasses

Object

Defined Under Namespace

Classes: Array, Boolean, Class, Date, DateTime, Decimal, DefaultValue, Float, Hash, Integer, Numeric, Object, String, Time

Constant Summary collapse

DEFAULT_ACCESSOR =
:public
OPTIONS =
[ :primitive, :accessor, :reader,
:writer, :coercion_method, :default ].freeze

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DescendantsTracker

add_descendant, descendants

Methods included from TypeLookup

determine_type, primitive

Methods included from Options

accept_options, accepted_options

Constructor Details

#initialize(name, options = {}) ⇒ undefined

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.

Initializes an attribute instance

Parameters:

  • name (Symbol)

    the name of an attribute

  • options (#to_hash) (defaults to: {})

    hash of extra options which overrides defaults set on an attribute class



81
82
83
84
85
86
87
88
89
90
# File 'lib/virtus/attribute.rb', line 81

def initialize(name, options = {})
  @name    = name
  @options = self.class.options.merge(options.to_hash).freeze

  @instance_variable_name = "@#{@name}".freeze
  @coercion_method        = @options.fetch(:coercion_method)
  @default                = DefaultValue.new(self, @options[:default])

  set_visibility
end

Instance Attribute Details

#coercion_methodSymbol (readonly)

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.

Returns method name that should be used for coerceing

Returns:

  • (Symbol)


54
55
56
# File 'lib/virtus/attribute.rb', line 54

def coercion_method
  @coercion_method
end

#defaultObject (readonly)

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.

Returns default value

Returns:



61
62
63
# File 'lib/virtus/attribute.rb', line 61

def default
  @default
end

#instance_variable_nameString (readonly)

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.

Returns instance variable name of the attribute

Returns:



33
34
35
# File 'lib/virtus/attribute.rb', line 33

def instance_variable_name
  @instance_variable_name
end

#nameSymbol (readonly)

Returns name of the attribute

Examples:

User.attributes[:age].name  # => :age

Returns:

  • (Symbol)


19
20
21
# File 'lib/virtus/attribute.rb', line 19

def name
  @name
end

#optionsHash (readonly)

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.

Returns options hash for the attribute

Returns:



26
27
28
# File 'lib/virtus/attribute.rb', line 26

def options
  @options
end

#reader_visibilitySymbol (readonly)

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.

Returns reader visibility

Returns:

  • (Symbol)


40
41
42
# File 'lib/virtus/attribute.rb', line 40

def reader_visibility
  @reader_visibility
end

#writer_visibilitySymbol (readonly)

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.

Returns write visibility

Returns:

  • (Symbol)


47
48
49
# File 'lib/virtus/attribute.rb', line 47

def writer_visibility
  @writer_visibility
end

Instance Method Details

#coerce(value) ⇒ Object

Converts the given value to the primitive type

Examples:

attribute.coerce(value)  # => primitive_value

Parameters:

  • value (Object)

    the value

Returns:

  • (Object)

    nil, original value or value converted to the primitive type



174
175
176
# File 'lib/virtus/attribute.rb', line 174

def coerce(value)
  Coercion[value.class].send(coercion_method, value)
end

#define_reader_method(mod) ⇒ self

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.

Creates an attribute reader method

Parameters:

  • mod (Module)

Returns:

  • (self)


185
186
187
188
189
190
191
192
193
# File 'lib/virtus/attribute.rb', line 185

def define_reader_method(mod)
  reader_method_name = name
  attribute          = self

  mod.send(:define_method,    reader_method_name) { attribute.get(self) }
  mod.send(reader_visibility, reader_method_name)

  self
end

#define_writer_method(mod) ⇒ self

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.

Creates an attribute writer method

Parameters:

  • mod (Module)

Returns:

  • (self)


202
203
204
205
206
207
208
209
210
# File 'lib/virtus/attribute.rb', line 202

def define_writer_method(mod)
  writer_method_name = "#{name}="
  attribute          = self

  mod.send(:define_method,    writer_method_name) { |value| attribute.set(self, value) }
  mod.send(writer_visibility, writer_method_name)

  self
end

#get(instance) ⇒ Object

Returns value of an attribute for the given instance

Sets the default value if an ivar is not set and default value is configured

Examples:

attribute.get(instance)  # => value

Returns:

  • (Object)

    value of an attribute



117
118
119
120
121
122
123
# File 'lib/virtus/attribute.rb', line 117

def get(instance)
  if instance.instance_variable_defined?(instance_variable_name)
    get!(instance)
  else
    set!(instance, default.evaluate(instance))
  end
end

#get!(instance) ⇒ Object

Returns the instance variable of the attribute

Examples:

attribute.get!(instance)  # => value

Returns:

  • (Object)

    value of an attribute



134
135
136
# File 'lib/virtus/attribute.rb', line 134

def get!(instance)
  instance.instance_variable_get(instance_variable_name)
end

#inspectString

Returns a concise string representation of the attribute instance

Examples:

attribute = Virtus::Attribute::String.new(:name)
attribute.inspect # => #<Virtus::Attribute::String @name=:name>

Returns:



101
102
103
# File 'lib/virtus/attribute.rb', line 101

def inspect
  "#<#{self.class.name} @name=#{name.inspect}>"
end

#set(instance, value) ⇒ self

Sets the value on the instance

Examples:

attribute.set(instance, value)  # => value

Returns:

  • (self)


146
147
148
# File 'lib/virtus/attribute.rb', line 146

def set(instance, value)
  set!(instance, coerce(value))
end

#set!(instance, value) ⇒ self

Sets instance variable of the attribute

Examples:

attribute.set!(instance, value)  # => value

Returns:

  • (self)


158
159
160
# File 'lib/virtus/attribute.rb', line 158

def set!(instance, value)
  instance.instance_variable_set(instance_variable_name, value)
end