Class: Virtus::Attribute::DefaultValue

Inherits:
Object
  • Object
show all
Defined in:
lib/virtus/attribute/default_value.rb

Overview

Class representing the default value option

Constant Summary collapse

SINGLETON_CLASSES =
[ ::NilClass, ::TrueClass, ::FalseClass,
::Numeric,  ::Symbol ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, value) ⇒ 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 default value instance

Parameters:



31
32
33
# File 'lib/virtus/attribute/default_value.rb', line 31

def initialize(attribute, value)
  @attribute, @value = attribute, value
end

Instance Attribute Details

#attributeVirtus::Attribute::Object (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 the attribute associated with this default value instance



14
15
16
# File 'lib/virtus/attribute/default_value.rb', line 14

def attribute
  @attribute
end

#valueObject (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 the value instance

Returns:



21
22
23
# File 'lib/virtus/attribute/default_value.rb', line 21

def value
  @value
end

Instance Method Details

#evaluate(instance) ⇒ 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.

Evaluates the value

Parameters:

Returns:

  • (Object)

    evaluated value



42
43
44
45
46
47
48
49
50
# File 'lib/virtus/attribute/default_value.rb', line 42

def evaluate(instance)
  if callable?
    call(instance)
  elsif cloneable?
    value.clone
  else
    value
  end
end