module Topo::ParseGen

Public Instance Methods

convert_keys_to_sym(hash) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

Expand a particular reference into a node search

# 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) click to toggle source

Convert lazy attributes to a string

# 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) click to toggle source

find and return dependencies (names) in topo_refs

# 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) click to toggle source
# 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