Module: LightModels::Json::Query

Defined in:
lib/lightmodels/jsonser_nav.rb

Instance Method Summary collapse

Instance Method Details

#attrs(root) ⇒ Object



18
19
20
# File 'lib/lightmodels/jsonser_nav.rb', line 18

def attrs(root)
  root.keys.select {|k| k.start_with? 'attr_'}
end


44
45
46
47
48
49
50
51
52
# File 'lib/lightmodels/jsonser_nav.rb', line 44

def print_tree(root,depth=0)
  traverse(root) do |n,d|
    s = ""
    d.times { s = s + "  " }
    s = s + n['type'] if n
    s = s + '<NIL>' unless n
    puts s
  end
end

#rel_conts(root) ⇒ Object



10
11
12
# File 'lib/lightmodels/jsonser_nav.rb', line 10

def rel_conts(root)
  root.keys.select {|k| k.start_with? 'relcont_'}
end

#rel_non_conts(root) ⇒ Object



14
15
16
# File 'lib/lightmodels/jsonser_nav.rb', line 14

def rel_non_conts(root)
  root.keys.select {|k| k.start_with? 'relcont_'}
end

#traverse(root, depth = 0, &op) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lightmodels/jsonser_nav.rb', line 28

def traverse(root,depth=0,&op)
  return traverse(root['root'],depth,&op) if root and (root.key? 'root')
  op.call(root,depth)
  return unless root   
  rel_conts(root).each do |r|
    if root[r].is_a? Array
      root[r].each do |c|
        raise "expected an object but it is a #{c.class} (relation: #{r})" unless c.is_a? Hash
        traverse(c,depth+1,&op)
      end
    else
      traverse(root[r],depth+1,&op)
    end
  end
end

#values(root, feat) ⇒ Object



22
23
24
25
26
# File 'lib/lightmodels/jsonser_nav.rb', line 22

def values(root,feat)
  raw = root[feat]
  return raw if raw.is_a? Array
  return [raw]
end