Class: Virtus::Attribute::EmbeddedValue

Inherits:
Object show all
Defined in:
lib/virtus/attribute/embedded_value.rb,
lib/virtus/attribute/embedded_value/from_struct.rb,
lib/virtus/attribute/embedded_value/from_open_struct.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' })

Direct Known Subclasses

FromOpenStruct, FromStruct

Defined Under Namespace

Classes: FromOpenStruct, FromStruct

Constant Summary

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary

Attributes inherited from Virtus::Attribute

#coercion_method, #default, #name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Virtus::Attribute

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

Methods included from DescendantsTracker

#add_descendant, #descendants

Methods included from TypeLookup

#determine_type, extended, #primitive

Methods included from Options

#accept_options, #accepted_options, #options

Constructor Details

This class inherits a constructor from Virtus::Attribute

Class Method Details

.determine_type(klass) ⇒ Virtus::Attribute::EmbeddedValue

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.

Determine type based on class

Virtus::EmbeddedValue.determine_type(Struct) # => Virtus::EmbeddedValue::FromStruct Virtus::EmbeddedValue.determine_type(VirtusClass) # => Virtus::EmbeddedValue::FromOpenStruct

Parameters:

Returns:



48
49
50
51
52
53
54
# File 'lib/virtus/attribute/embedded_value.rb', line 48

def self.determine_type(klass)
  if klass <= Virtus || klass <= OpenStruct
    FromOpenStruct
  elsif klass <= Struct
    FromStruct
  end
end

.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:



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

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

Instance Method Details

#coerce(value) ⇒ 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:



63
64
65
# File 'lib/virtus/attribute/embedded_value.rb', line 63

def coerce(value)
  value if value.kind_of?(@primitive)
end