Sha256: 079c6d12e55c7d3e4f2fcd3e1da4ed72af10e10466aae50a1e3c930c598bcdf8

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

module Virtus
  class Attribute

    # EmbeddedValue
    #
    # @example
    #
    #   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' })
    #
    class EmbeddedValue < Object
      primitive ::OpenStruct

      # @see Attribute.merge_options
      #
      # @return [Hash]
      #   an updated options hash for configuring an EmbeddedValue instance
      #
      # @api private
      def self.merge_options(type, options)
        options.merge(:primitive => type)
      end

      # Determine type based on class
      #
      # Virtus::EmbeddedValue.determine_type(Struct) # => Virtus::EmbeddedValue::FromStruct
      # Virtus::EmbeddedValue.determine_type(VirtusClass) # => Virtus::EmbeddedValue::FromOpenStruct
      #
      # @param [Class] klass
      #
      # @return [Virtus::Attribute::EmbeddedValue]
      #
      # @api private
      def self.determine_type(klass)
        if klass <= Virtus || klass <= OpenStruct
          FromOpenStruct
        elsif klass <= Struct
          FromStruct
        end
      end

      # Coerce attributes into a virtus object
      #
      # @param [Hash,Virtus]
      #
      # @return [Virtus]
      #
      # @api private
      def coerce(value)
        value if value.kind_of?(@primitive)
      end

    end # class EmbeddedValue
  end # class Attribute
end # module Virtus

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
virtus-0.5.5 lib/virtus/attribute/embedded_value.rb
virtus-0.5.4 lib/virtus/attribute/embedded_value.rb
virtus-0.5.3 lib/virtus/attribute/embedded_value.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/virtus-0.5.2/lib/virtus/attribute/embedded_value.rb
virtus-0.5.2 lib/virtus/attribute/embedded_value.rb