Class: ActiveAttrExtended::Typecasting::HashTypecaster

Inherits:
Object
  • Object
show all
Defined in:
lib/active_attr_extended/typecasting/hash_typecaster.rb

Overview

Typecasts an Object to a Hash

Examples:

Usage

HashTypecaster.new.call(1) # => [1]

Since:

  • 0.6.0

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ Hash?

Typecasts an Object to a Hash

Will typecast JSON or Hashes into a Hash

Examples:

Usage

HashTypecaster.new.call(nil) # => {}
HashTypecaster.new.call({:x => :y}) # => {:x => :y}
HashTypecaster.new.call({:x => :y}.to_json) # => {:x => :y}

Parameters:

  • value (Object)

    The object to typecast

Returns:

  • (Hash, nil)

    The result of typecasting

Since:

  • 0.6.0



26
27
28
29
30
# File 'lib/active_attr_extended/typecasting/hash_typecaster.rb', line 26

def call(value)
  #treat incoming strings as serialized JSON
  value = JSON::parse(value) if value.is_a? String
  value.is_a?(Hash) ? value : {}
end