Module: ClusterPoint::HashMethods

Included in:
Document
Defined in:
lib/cluster_point/hash_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.remove_attribs(h) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cluster_point/hash_methods.rb', line 103

def self.remove_attribs(h)
  h.keys.each do |k_s|
    #puts "REMOVE_ATTRIBS:"+k_s.to_s + ":" + h[k_s].class.to_s
    if h[k_s].class == ActionController::Parameters
      h[k_s] = h[k_s].to_h
    end
    if h[k_s].class == Hash
      h[k_s] == remove_attribs(h[k_s])
    end
    if(k_s.to_s.end_with? "_attributes")
        h[k_s.to_s[0..-12]] = h[k_s]
        h.delete(k_s)
    end
  end
  return h
end

Instance Method Details

#from_array(array, klass) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cluster_point/hash_methods.rb', line 54

def from_array(array, klass)      
  #puts "FROM_ARRAY:" + array.class.to_s + ":" + array.to_s
  arr = []
  if array != nil
    if array.class == Hash
      if like_array(array)
        #puts "FROM_ARRAY-LIKE_ARRAY"
        array.each do | key, value |
          unless value['_destroy'] == "1" || value[:_destroy] == "1"
            arr << klass.from_hash(value, klass)
          end
        end
      else
        #puts "FROM_ARRAY-HASH"
        unless array['_destroy'] == "1" || array[:_destroy] == "1"
          arr << klass.from_hash(array, klass)
        end
      end
    else
      #puts "FROM_ARRAY-ARRAY"
      array.each do | el |
        unless el['_destroy'] == "1" || el[:_destroy] == "1"
          arr << klass.from_hash(el, klass)
        end
      end
    end
  end
  arr
end

#from_hash(hash, klass) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cluster_point/hash_methods.rb', line 6

def from_hash(hash, klass)
  #puts "FROM_HASH:" + klass.to_s
  obj = klass.new(hash.except('_destroy'))
  if hash["id"]
    obj.id = hash["id"]
  end
  if hash[:id]
    obj.id = hash[:id]
  end
  if klass.get_contains_many
    #puts klass.get_contains_many
    klass.get_contains_many.each do |cont|
      key = cont.to_s
      exists = Object.const_get(cont.to_s.classify).is_a?(Class) rescue false
      if exists
        cont_klass = Object.const_get(cont.to_s.classify)
        arr = obj[key]
        unless arr == nil || arr == ""
          obj[key] = cont_klass.from_array(arr, cont_klass)
        else
          obj[key] = nil
        end
        #puts key
        #puts obj[key]
      end
    end
  end
  if klass.get_contains
    #puts klass.get_contains
    klass.get_contains.each do |cont|
      key = cont.to_s
      exists = Object.const_get(cont.to_s.classify).is_a?(Class) rescue false
      if exists
        cont_klass = Object.const_get(cont.to_s.classify)
        hash = obj[key]
        unless hash == nil || hash == ""
          obj[key] = cont_klass.from_hash(hash, cont_klass)
        else
          obj[key] = nil
        end
        #puts key
        #puts obj[key]
      end
    end
  end
  obj
end

#like_array(doc) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cluster_point/hash_methods.rb', line 84

def like_array(doc)
  result = false
  if doc.class == Array
    result = true
  elsif doc.class == Hash
    keys = doc.keys.sort!
    if keys.size > 0
      match = true
      for i in 0..keys.size - 1
        unless keys[i].to_s.to_i.to_s == keys[i] && doc[keys[i]].class == Hash
          match = false
        end
      end
      result = match
    end
  end
  return result
end