Class: Syobocal::Comment::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/syobocal/comment/parser.rb

Constant Summary collapse

ELEMENT_CLASSES =
[
  Element::Header2,
  Element::Header1,
  Element::List,
  Element::Row,
  Element::Blank,
  Element::TextNode, # NOTE Sentinel
]

Instance Method Summary collapse

Constructor Details

#initialize(comment) ⇒ Parser

Returns a new instance of Parser.



13
14
15
# File 'lib/syobocal/comment/parser.rb', line 13

def initialize(comment)
  @comment = comment
end

Instance Method Details

#all_sectionsObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/syobocal/comment/parser.rb', line 64

def all_sections
  return enum_for(:all_sections) unless block_given?

  sections.each do |section|
    yield section

    section.sub_sections.each do |sub_section|
      yield sub_section
    end
  end
end

#castsObject



38
39
40
41
42
43
44
# File 'lib/syobocal/comment/parser.rb', line 38

def casts
  return @casts if defined? @casts

  rows = sections.find { |section| section.cast_section? }&.rows || []

  @casts = create_cast_list(rows)
end


52
53
54
55
56
# File 'lib/syobocal/comment/parser.rb', line 52

def links
  return @links if defined? @links

  @links = sections.find { |section| section.link_section? }&.links || []
end

#musicsObject



46
47
48
49
50
# File 'lib/syobocal/comment/parser.rb', line 46

def musics
  return @musics if defined? @musics

  @musics = create_musics(all_sections)
end

#parseObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/syobocal/comment/parser.rb', line 17

def parse
  return @parse if defined? @parse

  return @parse = Element::Root.new([]) unless @comment

  elements = @comment.each_line.map do |line|
    line.chomp!
    ELEMENT_CLASSES.find { |clazz| clazz.match?(line) }.parse(line)
  end

  @parse = Element::Root.new(elements)
end

#sectionsObject



58
59
60
61
62
# File 'lib/syobocal/comment/parser.rb', line 58

def sections
  return @sections if defined? @sections

  @sections = Section.create_sections(parse.elements)
end

#staffsObject



30
31
32
33
34
35
36
# File 'lib/syobocal/comment/parser.rb', line 30

def staffs
  return @staffs if defined? @staffs

  rows = sections.find { |section| section.staff_section? }&.rows || []

  @staffs = create_staff_list(rows)
end