module Yasuri

Constants

DefaultInterval_ms
DefaultRetryCount
NodeRegexps
Text2Node
VERSION

Public Class Methods

json2tree(json_string) click to toggle source
# File lib/yasuri/yasuri.rb, line 19
def self.json2tree(json_string)
  raise RuntimeError if json_string.nil? or json_string.empty?

  node_hash = JSON.parse(json_string, {symbolize_names: true})
  self.hash2node(node_hash)
end
node_name(name, opt) click to toggle source
# File lib/yasuri/yasuri.rb, line 55
def self.node_name(name, opt)
  symbolize_names = opt[:symbolize_names]
  symbolize_names ? name.to_sym : name
end
tree2json(node) click to toggle source
# File lib/yasuri/yasuri.rb, line 33
def self.tree2json(node)
  raise RuntimeError if node.nil?

  self.node2hash(node).to_json
end
with_retry( retry_count = DefaultRetryCount, interval_ms = DefaultInterval_ms) { || ... } click to toggle source
# File lib/yasuri/yasuri.rb, line 39
def self.with_retry(
  retry_count = DefaultRetryCount,
  interval_ms = DefaultInterval_ms)

  begin
    Kernel.sleep(interval_ms * 0.001)
    return yield() if block_given?
  rescue => e
    if retry_count > 0
      retry_count -= 1
      retry
    end
    fail e
  end
end
yaml2tree(yaml_string) click to toggle source
# File lib/yasuri/yasuri.rb, line 26
def self.yaml2tree(yaml_string)
  raise RuntimeError if yaml_string.nil? or yaml_string.empty?

  node_hash = YAML.safe_load(yaml_string, [Symbol], symbolize_names: true)
  self.hash2node(node_hash.deep_symbolize_keys)
end

Private Class Methods

hash2child_node(node_hash) click to toggle source
# File lib/yasuri/yasuri.rb, line 97
def self.hash2child_node(node_hash)
  child_nodes = []
  opt = {}
  path = nil

  node_hash.each do |key, value|
    # is node?

    node_regexp = NodeRegexps.find { |r| key =~ r }

    case key
    when node_regexp
      node_type_sym = $1.to_sym
      child_node_name = $2
      child_node_type = Text2Node[node_type_sym]
      child_nodes << self.hash2node(value, child_node_name, child_node_type)
    when :path
      path = value
    else
      opt[key] = value
    end
  end

  [child_nodes, opt, path]
end
hash2node(node_hash, node_name = nil, node_type_class = nil) click to toggle source

private

# File lib/yasuri/yasuri.rb, line 62
def self.hash2node(node_hash, node_name = nil, node_type_class = nil)
  raise RuntimeError.new("") if node_name.nil? and node_hash.empty?

  child_nodes = []
  opt = {}
  path = nil

  if node_hash.is_a?(String)
    path = node_hash
  else
    child_nodes, opt, path = self.hash2child_node(node_hash)
  end

  # If only single node under root, return only the node.
  return child_nodes.first if node_name.nil? and child_nodes.size == 1

  node = if node_type_class.nil?
    Yasuri::MapNode.new(node_name, child_nodes, **opt)
  else
    node_type_class::new(path, node_name, child_nodes, **opt)
  end

  node
end
method_missing(method_name, pattern=nil, **opt, &block) click to toggle source
Calls superclass method
# File lib/yasuri/yasuri.rb, line 131
def self.method_missing(method_name, pattern=nil, **opt, &block)
  generated = Yasuri::NodeGenerator.gen(method_name, pattern, **opt, &block)
  generated || super(method_name, **opt)
end
node2hash(node) click to toggle source
# File lib/yasuri/yasuri.rb, line 123
def self.node2hash(node)
  return node.to_h if node.instance_of?(Yasuri::MapNode)

  {
    "#{node.node_type_str}_#{node.name}" => node.to_h
  }
end