Class: TallyGem::PostParser

Inherits:
Parslet::Parser
  • Object
show all
Includes:
Singleton
Defined in:
lib/tallygem/posts/parser.rb

Instance Method Summary collapse

Instance Method Details

#convert(tree) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object

Helpers



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



23
24
25
26
27
28
29
# 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