Sha256: d28ada6e0fb8e2345dc4abf58975d2c924ba400ceab5b82bfc58432d7760bfb9

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 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, Model::Constructor].freeze

      class Coercer
        attr_reader :primitive

        def initialize(primitive)
          @primitive = primitive
        end

      end # Coercer

      class FromStruct < Coercer

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

      end # FromStruct

      class FromOpenStruct < Coercer

        # @api public
        def call(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.build_type(options)
        Axiom::Types::Object.new { primitive options[:type] }
      end

      # @api private
      def self.build_coercer(type, _options)
        primitive = type.primitive

        if primitive < Virtus || primitive < Model::Constructor || primitive <= OpenStruct
          FromOpenStruct.new(primitive)
        elsif primitive < Struct
          FromStruct.new(primitive)
        end
      end

      # @api public
      def primitive
        type.primitive
      end

    end # class EmbeddedValue

  end # class Attribute
end # module Virtus

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
virtus-1.0.0.beta7 lib/virtus/attribute/embedded_value.rb