lib/virtus/attribute/hash.rb in virtus-1.0.0.rc1 vs lib/virtus/attribute/hash.rb in virtus-1.0.0.rc2

- old
+ new

@@ -1,27 +1,20 @@ module Virtus class Attribute - # Hash + # Handles attributes with Hash type # - # @example - # class Post - # include Virtus - # - # attribute :meta, Hash - # end - # - # Post.new(:meta => { :tags => %w(foo bar) }) - # class Hash < Attribute primitive ::Hash default primitive.new - attr_reader :key_type - attr_reader :value_type + # @api private + attr_reader :key_type, :value_type # FIXME: remove this once axiom-types supports it + # + # @private Type = Struct.new(:key_type, :value_type) do def self.infer(type) if axiom_type?(type) new(type.key_type, type.value_type) else @@ -31,28 +24,32 @@ new(key_class, value_class) end end + # @api private def self.pending?(primitive) primitive.is_a?(String) || primitive.is_a?(Symbol) end + # @api private def self.axiom_type?(type) type.is_a?(Class) && type < Axiom::Types::Type end + # @api private def self.determine_type(type) return type if pending?(type) if EmbeddedValue.handles?(type) type else Axiom::Types.infer(type) end end + # @api private def self.infer_key_and_value_types(type) return {} unless type.kind_of?(::Hash) if type.size > 1 raise ArgumentError, "more than one [key => value] pair in `#{type}`" @@ -75,14 +72,16 @@ { :key_type => key_primitive, :value_type => value_primitive} end end + # @api private def coercion_method :to_hash end + # @api private def primitive ::Hash end end @@ -95,10 +94,14 @@ def self.merge_options!(type, options) options[:key_type] ||= Attribute.build(type.key_type) options[:value_type] ||= Attribute.build(type.value_type) end + # Coerce members + # + # @see [Attribute#coerce] + # # @api public def coerce(*) coerced = super return coerced unless coerced.respond_to?(:each_with_object) @@ -120,7 +123,8 @@ def finalized? super && key_type.finalized? && value_type.finalized? end end # class Hash + end # class Attribute end # module Virtus