class TallyGem::PostParser

Public Instance Methods

convert(tree) click to toggle source
# File lib/tallygem/posts/parser.rb, line 62
def convert(tree)
  tree.each do |hm|
    hm[:task] = hm[:task].to_s if hm.key?(:task)
    hm[:rank] = Integer(hm[:rank]) if hm.key?(:rank)
    hm[:vote_text] = hm[:vote_text].to_s if hm.key?(:vote_text)
    if hm.key? :subvotes
      hm[:subvotes] = hm[:subvotes].is_a?(Array) ? convert(hm[:subvotes]) : []
    end
  end
end
parse(str) click to toggle source

Helpers

# File lib/tallygem/posts/parser.rb, line 46
def parse(str)
  str = str.each_line
           .collect(&:strip) # strip leading whitespace
           .reject { |s| s[0] != '-' && s[0] != '[' } # filter non-votes out
           .join("\n")

  # Fail fast
  return [] if str.empty?

  # parse the vote content
  result = root.parse(str)
  result = [] if result.nil? || result.empty?
  convert(result)
  result
end
plan_vote(depth) click to toggle source
# File lib/tallygem/posts/parser.rb, line 23
def plan_vote(depth)
  dashes = (ws? >> str('-')).repeat(depth).capture(:dashes)
  dashes >> selection >> vote_body >> dynamic do |_, ctx|
    dash_count = ctx.captures[:dashes].size
    plan_vote(dash_count + 1).repeat.as(:subvotes)
  end
end