Class: ARMS::IndifferentHashesCoder

Inherits:
Object
  • Object
show all
Defined in:
lib/arms/indifferent_hashes_coder.rb

Overview

ARMS::IndifferentHashesCoder will replace any Hashes in a structure of Arrays and Hashes with ActiveSupport::HashWithIndifferentAccess on load, and convert back to plain hashes on dump.

Instance Method Summary collapse

Instance Method Details

#dump(object) ⇒ Object

Parameters:

  • object (#to_ary, #to_hash, Object)

    a structure in which ActiveSupport::HashWithIndifferentAccess instances will be replaced with plain Hashes



16
17
18
19
20
21
22
23
24
# File 'lib/arms/indifferent_hashes_coder.rb', line 16

def dump(object)
  if object.respond_to?(:to_ary)
    object.to_ary.map { |el| dump(el) }
  elsif object.respond_to?(:to_hash)
    object.to_hash.transform_values { |v| dump(v) }
  else
    object
  end
end

#load(column_data) ⇒ Object

Parameters:

  • column_data (Array, Hash, Object)

    a structure in which Hashes will be replaced with ActiveSupport::HashWithIndifferentAccess



5
6
7
8
9
10
11
12
13
# File 'lib/arms/indifferent_hashes_coder.rb', line 5

def load(column_data)
  if column_data.respond_to?(:to_ary)
    column_data.to_ary.map { |el| load(el) }
  elsif column_data.respond_to?(:to_hash)
    ActiveSupport::HashWithIndifferentAccess.new(column_data).transform_values { |v| load(v) }
  else
    column_data
  end
end