class Vym::HierarchicalParser

Constants

Branch

Attributes

input_text[R]

Public Class Methods

new(input_text) click to toggle source
# File lib/vym/hierarchical_parser.rb, line 3
def initialize(input_text)
  @input_text = input_text
end

Public Instance Methods

branches() click to toggle source
# File lib/vym/hierarchical_parser.rb, line 11
def branches
  input_text.scan(branch_regex).map { |asterisks, text|
    if asterisks.size > 0
      title = text.gsub(suffix_regex, "")
      level = get_level_from_size(asterisks.size)
      Branch[title, level]
    end
  }.compact
end
main_concept() click to toggle source
# File lib/vym/hierarchical_parser.rb, line 7
def main_concept
  input_text[main_concept_regex, 1].gsub(suffix_regex, "")
end
to_h() click to toggle source
# File lib/vym/hierarchical_parser.rb, line 21
def to_h
  return {} if !input_text || input_text =~ /\A\s*\Z/

  yaml_arr = []

  yaml_arr << "---"
  yaml_arr << "- #{main_concept}:"

  branches.map { |branch|
    yaml_arr << "  " * branch.level + "- " + branch.title + ":"
  }

  yaml = yaml_arr.join("\n")
  sanitize_hash(YAML.load(yaml).first)
end

Private Instance Methods

get_level_from_size(size) click to toggle source
# File lib/vym/hierarchical_parser.rb, line 47
def get_level_from_size(size)
  size
end
sanitize_hash(h) click to toggle source
# File lib/vym/hierarchical_parser.rb, line 41
def sanitize_hash(h)
  h.each_with_object({}) { |(k,v), result|
    result[k] = Array(v).map { |hash| sanitize_hash(hash) }
  }
end