Class: JOR::Doc

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

Class Method Summary collapse

Class Method Details

.deep_merge(dest, source) ⇒ Object



54
55
56
57
58
59
# File 'lib/jor/doc.rb', line 54

def self.deep_merge(dest, source)
  res = Hash.new
  dest.merge(source) do |key, old_v, new_v|
    res[key] = ((old_v.class == Hash) && (new_v.class == Hash))  ? deep_merge(old_v, new_v) : new_v
  end
end

.difference(set, set_to_substract) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jor/doc.rb', line 34

def self.difference(set, set_to_substract)
  return set if set_to_substract.nil? || set_to_substract.size==0

  to_exclude = []
  set_to_substract.each do |item|
    raise FieldIdCannotBeExcludedFromIndex.new unless item["path_to"].match(/\/_id/)==nil
    to_exclude << Regexp.new("^#{item["path_to"]}")
  end

  res = []
  set.each do |item|
    not_found = true
    to_exclude.each do |re|
      not_found = not_found && re.match(item["path_to"])==nil
    end
    res << item if not_found
  end
  return res
end

.paths(path, h) ⇒ Object



5
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
# File 'lib/jor/doc.rb', line 5

def self.paths(path,h)
  if h.class==Hash
    v = []
    h.each do |k,val|
      if JOR::Storage::SELECTORS_ALL.member?(k)
        return [{"path_to" => path, "obj" => h, "class" => h.class, "selector" => true}]
      else
        raise InvalidFieldName.new(k) if ((k!="_id") && (k!="_created_at") && (k!="_updated_at")) && (k[0]=="_" || k[0]=="$")
        v << paths("#{path}/#{k}",val)
      end
    end
    return v.flatten
  else
    if h.class==Array
      v = []
      if h.size>0
        h.each do |item|
          v << paths("#{path}",item)
       end
      else
        v << ["#{path}"]
      end
      return v.flatten
    else
      return [{"path_to" => path, "obj" => h, "class" => h.class}]
    end
  end
end