Module: ClusterPoint::ModificationMethods

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

Instance Method Summary collapse

Instance Method Details

#destroyObject



57
58
59
# File 'lib/cluster_point/modification_methods.rb', line 57

def destroy
  get_cp.delete(self["id"])
end

#merge(params) ⇒ 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
# File 'lib/cluster_point/modification_methods.rb', line 6

def merge(params)
  params.each do | k, v|
    if self[k].kind_of? ClusterPoint::Document
      self[k].merge(v)
    elsif k.to_s.end_with? "_attributes"
      key=k.to_s[0..-12]
      #puts key
      h = ClusterPoint::HashMethods.remove_attribs(v.to_h)
      #puts "RRR:" + h.to_s
      if h.keys.include?('_destroy') || h.keys.include?(:_destroy)
        #puts 'TTT'
        klass = Object.const_get(key.classify)
        val = nil
        unless h['_destroy'] == "1" || h[:_destroy] == "1"
          val = klass.from_hash(h, klass)
        end
        self[key] = val
      else
        #puts "AAA"
        arr=[]
        klass = Object.const_get(key.classify)
        h.values.each do |val|
          unless val['_destroy'] == "1" || val[:_destroy] == "1"
            arr << klass.from_hash(val, klass)
          end
        end
        self[key] = arr
      end
    else
      self[k] = v
    end
    #puts k.to_s + ":" + v.to_s
  end
end

#saveObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cluster_point/modification_methods.rb', line 46

def save
  if self["id"] != nil
    id = self["id"]
    get_cp.update(self)
  else
    id = SecureRandom.uuid
    self["id"] = id
    get_cp.insert(self)
  end
  self.class.get(id)
end

#update(params) ⇒ Object



40
41
42
43
44
45
# File 'lib/cluster_point/modification_methods.rb', line 40

def update(params)
  self.merge(params)
  puts "UPDATE_AFTER:"+self.to_s
  get_cp.update(self)
  self.class.get(self.id)
end