Sha256: 22b3928fc789b2f164506004b11496735904666e9bf3b66779b164608da2bbb6

Contents?: true

Size: 921 Bytes

Versions: 3

Compression:

Stored size: 921 Bytes

Contents

require 'active_support/core_ext/hash/indifferent_access'

module ActiveAttr
  module Typecasting
    # Typecasts an Object to a HashWithInddifferentAccess
    #
    # @example Usage
    #   typecaster = HashTypecaster.new
    #   typecaster.call([[:foo, :bar]]) #=> { foo: :bar }
    #
    # @since 0.5.0
    class HashTypecaster
      # Typecasts an object to a HashWithInddifferentAccess
      #
      # Attempts to convert using #to_h.
      #
      # @example Typecast an Array
      #   typecaster.call([[:foo, :bar]]) #=> { foo: :bar }
      #
      # @param [Object, #to_h] value The object to typecast
      #
      # @return [HashWithInddifferentAccess] The result of typecasting
      #
      # @since 0.5.0
      def call(value)
        if value.respond_to? :to_h
          HashWithIndifferentAccess.new(value.to_h)
        else
          HashWithIndifferentAccess.new
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
syncano-4.0.0.alpha1 lib/active_attr/typecasting/hash_typecaster.rb
syncano-4.0.0.alpha lib/active_attr/typecasting/hash_typecaster.rb
syncano-4.0.0.pre lib/active_attr/typecasting/hash_typecaster.rb