def coerce_property(property, options)
      unless options.is_a?(Hash)
        options = { class: options }
      end

      coercion_method = ->(obj) do
        if obj.respond_to?(:coerce)
          obj.method(:coerce)
        elsif obj.respond_to?(:new)
          obj.method(:new)
        else
          raise ArgumentError, "#{obj.inspect} does not implement neither .coerce nor .new"
        end
      end

      if options.has_key?(:collection)
        klass = options[:collection]
        coercion = ->(value) { value.map { |el| coercion_method[klass][el] } }
      elsif options.has_key?(:class)
        klass = options[:class]
        coercion = ->(value) { coercion_method[klass][value] }
      else
        raise ArgumentError, "You need to specify either :class or :collection"
      end

      coercions[property.to_s] = coercion
    end
def coerce_property(property, options)
      unless options.is_a?(Hash)
        options = { class: options }
      end

      coercion_method = ->(obj) do
        if obj.respond_to?(:coerce)
          obj.method(:coerce)
        elsif obj.respond_to?(:new)
          obj.method(:new)
        else
          raise ArgumentError, "#{obj.inspect} does not implement neither .coerce nor .new"
        end
      end

      if options.has_key?(:collection)
        klass = options[:collection]
        coercion = ->(value) { value.map { |el| coercion_method[klass][el] } }
      elsif options.has_key?(:class)
        klass = options[:class]
        coercion = ->(value) { coercion_method[klass][value] }
      else
        raise ArgumentError, "You need to specify either :class or :collection"
      end

      coercions[property.to_s] = coercion
    end
Public: Coerce a property of this hash into a given type. We will try to
 call .coerce on the object you pass as the class, and if that fails, we will
 call .new.

 property - The name of the property to coerce.
 class_or_options  - Either a class to which coerce, or a hash with options:
                     * class:      The class to which coerce
                     * collection: Coerce the key into an array of members of
                                   this class.

 Examples

   class Person < Hash
     extend Diffbot::CoercibleHash

     coerce_property :address, Address

     coerce_property :children, collection: Person

     coerce_property :dob, class: Date
   end