Sha256: bf13fa2b1ba7a25d27153b2fd3a2d6b26b5d6981bdae22d6a104fd76fcb39b1a

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

require 'ostruct'

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

      # @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(:model => type)
      end

      # Sets @model ivar
      #
      # @see Virtus::Attribute#initialize
      #
      # @return [undefined]
      #
      # @api private
      def initialize(name, options = {})
        super
        @model = options.fetch(:model, OpenStruct)
      end

      # Coerce attributes into a virtus object
      #
      # @param [Hash,Virtus]
      #
      # @return [Virtus]
      #
      # @api private
      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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
virtus-0.2.0 lib/virtus/attribute/embedded_value.rb
virtus-0.1.0 lib/virtus/attribute/embedded_value.rb