class Syobocal::Comment::Helper::Fragment

Constants

CHILD_BEGIN
CHILD_END
SEPARATOR

Attributes

child[R]
following[R]
text[R]

Public Class Methods

new(text, child, following) click to toggle source
# File lib/syobocal/comment/helper/fragment.rb, line 11
def initialize(text, child, following)
  @text, @child, @following = text, child, following
end
parse(str) click to toggle source
# File lib/syobocal/comment/helper/fragment.rb, line 15
def self.parse(str)
  chars = str.each_char.to_a

  parse_chars(chars)
end
parse_chars(chars) click to toggle source
# File lib/syobocal/comment/helper/fragment.rb, line 21
def self.parse_chars(chars)
  text = ""
  child = nil
  following = nil

  until chars.empty?
    c = chars.shift

    case c
    when CHILD_BEGIN
      child = parse_chars(chars)
    when CHILD_END
      return Fragment.new(text, child, following)
    when SEPARATOR
      following = parse_chars(chars)
    else
      text << c
    end
  end

  Fragment.new(text, child, following)
end

Public Instance Methods

pretty_output(level = 0) click to toggle source
# File lib/syobocal/comment/helper/fragment.rb, line 44
def pretty_output(level = 0)
  line = level.times.map{' '}.join

  line << text

  puts line

  child&.pretty_output(level + 1)

  following&.pretty_output(level)
end
to_a() click to toggle source
# File lib/syobocal/comment/helper/fragment.rb, line 56
def to_a
  array = [self]
  target = self

  while target.following
    array << target.following
    target = target.following
  end

  array
end
to_s() click to toggle source
# File lib/syobocal/comment/helper/fragment.rb, line 68
def to_s
  to_a.map{|f|
    if f.child
      "#{f.text}(#{f.child.to_s})"
    else
      "#{f.text}"
    end
  }.join(SEPARATOR)
end