Sha256: db1650d0bd3c131e1c41623c9c88eca6867a37292dc3d1b9e615f9a98b64b15e

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 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 < Attribute
      TYPES = [Struct, OpenStruct, Virtus].freeze

      class FromStruct < self

        # @api public
        def coerce(input)
          if input.kind_of?(primitive)
            input
          elsif not input.nil?
            primitive.new(*input)
          end
        end
      end # FromStruct

      class FromOpenStruct < self

        # @api public
        def coerce(input)
          if input.kind_of?(primitive)
            input
          elsif not input.nil?
            primitive.new(input)
          end
        end
      end # FromOpenStruct

      # @api private
      def self.handles?(klass)
        klass.is_a?(Class) && TYPES.any? { |type| klass <= type }
      end

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

      # @api private
      def self.build_type(options)
        Axiom::Types::Object.new { primitive options[:type] }
      end

      # @api public
      def primitive
        type.primitive
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
virtus-1.0.0.beta5 lib/virtus/attribute/embedded_value.rb
virtus-1.0.0.beta4 lib/virtus/attribute/embedded_value.rb
virtus-1.0.0.beta3 lib/virtus/attribute/embedded_value.rb