Class: Sym

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

Overview

creates a symbolized hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Sym

Returns a new instance of Sym.



9
10
11
12
# File 'lib/symbolize.rb', line 9

def initialize(hash)
  @hash = hash
  @hash = process_hash(@hash)
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



7
8
9
# File 'lib/symbolize.rb', line 7

def hash
  @hash
end

Instance Method Details

#process_array(array) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/symbolize.rb', line 29

def process_array(array)
  new_array = []

  array.each do |item|
    new_array.push item unless item.is_a?(Array) || item.is_a?(Hash)
    new_array.push process_array(item) if item.is_a? Array
    new_array.push process_hash(item) if item.is_a? Hash
  end
  new_array
end

#process_hash(hash) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/symbolize.rb', line 14

def process_hash(hash)
  raise CheckList::Exceptions::NotAHash, 'Please pass a valid hash' unless hash.is_a? Hash

  hash.transform_keys!(&:to_sym)
  keys = hash.keys
  new_hash = {}

  keys.each do |key|
    new_hash[key] = hash[key]
    new_hash[key] = process_hash(hash[key]) if hash[key].is_a? Hash
    new_hash[key] = process_array(hash[key]) if hash[key].is_a? Array
  end
  new_hash
end