Class: Syobocal::Comment::Section

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

Defined Under Namespace

Classes: RestrictedOperation

Constant Summary collapse

STAFF_WORD =
"スタッフ"
CAST_WORD =
"キャスト"
"リンク"
MUSIC_WORDS =
%w(テーマ ソング 歌 曲)
MUSIC_TITLE_REGEXP =
/\A(.*)「(.+?)」\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, elements) ⇒ Section

Returns a new instance of Section.



8
9
10
11
# File 'lib/syobocal/comment/section.rb', line 8

def initialize(header, elements)
  @header, @elements = header, elements
  @sub_sections = []
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



4
5
6
# File 'lib/syobocal/comment/section.rb', line 4

def elements
  @elements
end

#headerObject (readonly)

Returns the value of attribute header.



4
5
6
# File 'lib/syobocal/comment/section.rb', line 4

def header
  @header
end

#sub_sectionsObject (readonly)

Returns the value of attribute sub_sections.



4
5
6
# File 'lib/syobocal/comment/section.rb', line 4

def sub_sections
  @sub_sections
end

Class Method Details

.create_sections(elements) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/syobocal/comment/section.rb', line 90

def self.create_sections(elements)
  sections = []
  current_header = nil
  buffered_elements = []

  elements.each do |element|
    case element
    when Element::Header1, Element::Header2
      if current_header
        sections << Section.new(current_header, buffered_elements)
        buffered_elements = []
      end

      current_header = element
    else
      buffered_elements << element if current_header
    end
  end

  if current_header
    sections << Section.new(current_header, buffered_elements)
  end

  structure_sections(sections)
end

Instance Method Details

#add_subsection(sub_section) ⇒ Object



17
18
19
20
21
22
# File 'lib/syobocal/comment/section.rb', line 17

def add_subsection(sub_section)
  raise RestrictedOperation if sub_section?
  raise RestrictedOperation unless sub_section.sub_section?

  @sub_sections << sub_section
end

#cast_section?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/syobocal/comment/section.rb', line 34

def cast_section?
  header.text_node.inner_text.include?(CAST_WORD)
end

Returns:

  • (Boolean)


38
39
40
# File 'lib/syobocal/comment/section.rb', line 38

def link_section?
  header.text_node.inner_text.include?(LINK_WORD)
end


79
80
81
82
83
84
85
86
87
88
# File 'lib/syobocal/comment/section.rb', line 79

def links
  elements.each_with_object([]) { |element, links|
    case element
    when Element::List
      links << element.text_node.text_elements.select { |elm| elm.instance_of? Element::Link }
    when Element::TextNode
      links << element.text_elements.select { |elm| elm.instance_of? Element::Link }
    end
  }.flatten
end

#music_section?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/syobocal/comment/section.rb', line 42

def music_section?
  str = header.text_node.inner_text

  MUSIC_WORDS.any? { |keyword| str.include?(keyword) } && str.match(MUSIC_TITLE_REGEXP)
end

#rowsObject



75
76
77
# File 'lib/syobocal/comment/section.rb', line 75

def rows
  elements.select { |element| element.is_a? Element::Row }
end

#staff_section?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/syobocal/comment/section.rb', line 30

def staff_section?
  header.text_node.inner_text.include?(STAFF_WORD)
end

#sub_section?Boolean

Returns:

  • (Boolean)


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

def sub_section?
  @header.instance_of? Element::Header2
end

#to_musicObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/syobocal/comment/section.rb', line 48

def to_music
  m = header.text_node.inner_text.match(MUSIC_TITLE_REGEXP)
  title = m[2]
  category = m[1]

  data_list = rows.map do |row|
    attr = row.attr_node.inner_text

    attr_fragment = Helper::Fragment.parse(attr)

    if attr_fragment.to_a.size == 1
      attr_text = attr_fragment.text
      attr_note = attr_fragment&.child&.to_s
    else
      attr_text = attr_fragment.to_s
      attr_note = nil
    end

    value = row.value_node.inner_text
    people = Person.multi_parse(value)

    MusicData.new(attr, attr_text, attr_note, value, people)
  end

  Music.new(title, category, data_list)
end