Module: Topo::ParseGen

Instance Method Summary collapse

Instance Method Details

#convert_keys_to_sym(hash) ⇒ Object

convert keys to symbols (deep) NOTE: recurses into hashes but not into arrays



34
35
36
37
38
39
40
# File 'lib/topo/utils/parsegen.rb', line 34

def convert_keys_to_sym(hash)
  new_hash = {}
  hash.each do |key, val|
    new_hash[key.to_sym] = val
  end
  new_hash
end

#convert_keys_to_sym_deep(hash) ⇒ Object

convert keys to symbols (deep) NOTE: recurses into hashes but not into arrays



44
45
46
47
48
49
50
# File 'lib/topo/utils/parsegen.rb', line 44

def convert_keys_to_sym_deep(hash)
  new_hash = {}
  hash.each do |key, val|
    new_hash[key.to_sym] = val.kind_of?(Hash) ? convert_keys_to_sym_deep(val) : val
  end
  new_hash
end

#expand_ref(ref) ⇒ Object

Expand a particular reference into a node search



92
93
94
95
# File 'lib/topo/utils/parsegen.rb', line 92

def expand_ref(ref)
  path = ref['path']
  "topo_search_node_fn.call(#{ref['name'].inspect}, #{path})"
end

#lazy_attribute_to_s(hash) ⇒ Object

Convert lazy attributes to a string



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/topo/utils/parsegen.rb', line 72

def lazy_attribute_to_s (hash)
  str = ""
  hash.each do |key, val|
    str += ', ' if str != ""
    if val.kind_of?(Hash)
      if val.key?("topo_ref") && val.keys.length == 1
        # this is a topology reference so expand it
        str += "'#{key}' => " + expand_ref(val['topo_ref'])
      else
        str += lazy_attribute_to_s(val)
      end
    else
      str += "'#{key}' => " + val.inspect
    end
  end

  str
end

#topo_refs(hash, depends_on = Set.new) ⇒ Object

find and return dependencies (names) in topo_refs



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/topo/utils/parsegen.rb', line 54

def topo_refs(hash, depends_on=Set.new)
  if hash.kind_of? Hash
    hash.each do |key, val|
     if key == "topo_ref"
        depends_on.add val['name']
      else
        topo_refs(val, depends_on)
      end
    end
  elsif hash.kind_of? Array
    hash.each do |val|
      topo_refs(val, depends_on)
    end
  end
  depends_on
end

#value_from_path(hash, path) ⇒ Object



26
27
28
29
30
# File 'lib/topo/utils/parsegen.rb', line 26

def value_from_path(hash, path)
  path.reduce(hash) do |val, key|
    val.kind_of?(Hash) ? val[key] : nil
  end
end