Class: Hash

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

Overview

Override ruby Hash Obj

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#get_url_paramsObject

Attr to be external accessible



4
5
6
# File 'lib/rents/hash.rb', line 4

def get_url_params
  @get_url_params
end

Instance Method Details

#it_keys_to_get_paramObject

Convert it keys to get params



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rents/hash.rb', line 17

def it_keys_to_get_param
  self.it_keys_to_sym
  self.get_url_params = '?' if self.get_url_params.nil?

  self.keys.each do |key|
    self.get_url_params = self.get_url_params+'&' unless self.get_url_params.length == 1

    # Nested obj_attrs
    if self[key].is_a?(Hash)
      hash_name = key
      hash_obj = self[key]

      # Hash to GET URL
      param = to_nested_get_param hash_name, hash_obj
    end

    self.get_url_params = self.get_url_params+param
  end

  # Remove the last char: &
  return self.get_url_params[0..self.get_url_params.length-2]
end

#it_keys_to_symObject

Convert string keys to symbol keys



7
8
9
10
11
12
13
14
# File 'lib/rents/hash.rb', line 7

def it_keys_to_sym
  self.keys.each do |key|
    self[key].it_keys_to_sym if self[key].is_a?(Hash)
    self[(key.to_sym rescue key) || key] = self.delete(key)
  end

  return self
end

#to_nested_get_param(hash_name, hash_obj) ⇒ Object

SetUp a hash to hash URL GET



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rents/hash.rb', line 41

def to_nested_get_param  hash_name, hash_obj
  # initial value
  get_nested_params = ''

  # foreach keys to mount the URL_PARAM
  hash_obj.keys.each do |key|
    key_param = hash_obj[key].to_nested_get_param key, hash_obj[key] if hash_obj[key].is_a?(Hash)
    key_param = "#{hash_name}[#{key}]=#{hash_obj[key]}&" unless hash_obj[key].is_a?(Hash)
    get_nested_params = get_nested_params+key_param
  end

  # return
  get_nested_params
end