Class: Virtus::Attribute::EmbeddedValue

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

Overview

EmbeddedValue

Examples:


class Address
  include Virtus

  attribute :street,  String
  attribute :zipcode, String
  attribute :city,    String
end

class User
  include Virtus

  attribute :address, Address
end

user = User.new(:address => {
  :street => 'Street 1/2', :zipcode => '12345', :city => 'NYC' })

Constant Summary

Constants included from TypeLookup

TypeLookup::EXTRA_CONST_ARGS, TypeLookup::TYPE_FORMAT

Instance Attribute Summary

Attributes inherited from Virtus::Attribute

#coercion_method, #default, #instance_variable_name, #name, #options, #reader_visibility, #writer_visibility

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Virtus::Attribute

build, #define_accessor_methods, #define_reader_method, #define_writer_method, determine_type, #get, #get!, #inspect, #public_reader?, #public_writer?, #set, #set!, #value_coerced?

Methods included from DescendantsTracker

#add_descendant, #descendants

Methods included from TypeLookup

#determine_type, #primitive

Methods included from Options

#accept_options, #accepted_options, #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.

Sets @model ivar



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

def initialize(name, options = {})
  super
  @model = options.fetch(:model, OpenStruct)
end

Class Method Details

.merge_options(type, options) ⇒ Hash

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 an updated options hash for configuring an EmbeddedValue instance.

Returns:

  • (Hash)

    an updated options hash for configuring an EmbeddedValue instance

See Also:



35
36
37
# File 'lib/virtus/attribute/embedded_value.rb', line 35

def self.merge_options(type, options)
  options.merge(:model => type)
end

Instance Method Details

#coerce(attributes_or_object) ⇒ Virtus

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.

Coerce attributes into a virtus object

Parameters:

Returns:



58
59
60
61
62
63
64
65
66
# File 'lib/virtus/attribute/embedded_value.rb', line 58

def coerce(attributes_or_object)
  value = if attributes_or_object.kind_of?(::Hash)
            @model.new(attributes_or_object)
          else
            attributes_or_object
          end

  super(value)
end