Class: SyoboiCalendar::CommentSection

Inherits:
Object
  • Object
show all
Defined in:
lib/syoboi_calendar/comment_section.rb

Constant Summary collapse

PATTERN_NAME_CASTS =
"キャスト"
PATTERN_NAME_STAFFS =
"スタッフ"
PATTERN_NAME_SONG_ENDING =
/\Aエンディング\d*\s*「(.+)」\z/
PATTERN_NAME_SONG_INSERTED =
/\A挿入歌\d*\s*「(.+)」\z/
PATTERN_NAME_SONG_OPENING =
/\Aオープニング\d*\s*「(.+)」\z/
PATTERN_NAME_SONG_THEME =
/\A主題歌\d*\s*「(.+)」\z/
PATTERN_SONG_NAME =
/「(.+)」/
PATTERN_SONG_ROLE =
/(\S+)\s*「.+」/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ CommentSection

Returns a new instance of CommentSection.

Parameters:

  • source (String)


15
16
17
# File 'lib/syoboi_calendar/comment_section.rb', line 15

def initialize(source)
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/syoboi_calendar/comment_section.rb', line 12

def source
  @source
end

Instance Method Details

#about_casts?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/syoboi_calendar/comment_section.rb', line 20

def about_casts?
  PATTERN_NAME_CASTS === name
end

#about_personalities?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/syoboi_calendar/comment_section.rb', line 53

def about_personalities?
  about_casts? ||
    about_song? ||
    about_staffs?
end

#about_song?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/syoboi_calendar/comment_section.rb', line 25

def about_song?
  about_song_ending? ||
    about_song_inserted? ||
    about_song_opening? ||
    about_song_theme?
end

#about_song_ending?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/syoboi_calendar/comment_section.rb', line 33

def about_song_ending?
  PATTERN_NAME_SONG_ENDING === name
end

#about_song_inserted?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/syoboi_calendar/comment_section.rb', line 38

def about_song_inserted?
  PATTERN_NAME_SONG_INSERTED === name
end

#about_song_opening?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/syoboi_calendar/comment_section.rb', line 43

def about_song_opening?
  PATTERN_NAME_SONG_OPENING === name
end

#about_song_theme?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/syoboi_calendar/comment_section.rb', line 48

def about_song_theme?
  PATTERN_NAME_SONG_THEME === name
end

#about_staffs?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/syoboi_calendar/comment_section.rb', line 60

def about_staffs?
  PATTERN_NAME_STAFFS === name
end

#arrayArray<String>

Returns:

  • (Array<String>)


65
66
67
68
69
70
71
72
73
# File 'lib/syoboi_calendar/comment_section.rb', line 65

def array
  if lines[1][0] == "-"
    lines[1..-1].grep(/\A-/).map do |line|
      line[1..-1]
    end
  else
    lines
  end
end

#has_hash?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/syoboi_calendar/comment_section.rb', line 76

def has_hash?
  lines[1][0] == ":"
end

#hashHash{String => String}

Returns:

  • (Hash{String => String})


81
82
83
84
85
86
87
88
# File 'lib/syoboi_calendar/comment_section.rb', line 81

def hash
  lines[1..-1].grep(/\A:/).each_with_object({}) do |line, result|
    key, value = line[1..-1].split(/[::]/, 2)
    if value
      result[key] = value.split("")
    end
  end
end

#linesArray<String>

Returns:

  • (Array<String>)


91
92
93
# File 'lib/syoboi_calendar/comment_section.rb', line 91

def lines
  source.strip.split("\n")
end

#nameString

Returns:

  • (String)


96
97
98
# File 'lib/syoboi_calendar/comment_section.rb', line 96

def name
  lines[0][1..-1]
end

#personalitiesArray?

Returns:

  • (Array, nil)


101
102
103
104
105
106
107
108
109
# File 'lib/syoboi_calendar/comment_section.rb', line 101

def personalities
  if about_personalities?
    hash.flat_map do |role, sources|
      sources.map do |source|
        ::SyoboiCalendar::Personality.new(role: role, source: source)
      end
    end
  end
end

#song_attributesHash

Returns:

  • (Hash)


112
113
114
115
116
117
118
# File 'lib/syoboi_calendar/comment_section.rb', line 112

def song_attributes
  {
    name: song_name,
    role: song_role,
    personalities: personalities,
  }
end

#song_nameString?

Returns:

  • (String, nil)


121
122
123
124
125
# File 'lib/syoboi_calendar/comment_section.rb', line 121

def song_name
  if about_song?
    name[PATTERN_SONG_NAME, 1]
  end
end

#song_roleString?

Returns:

  • (String, nil)


128
129
130
# File 'lib/syoboi_calendar/comment_section.rb', line 128

def song_role
    name[PATTERN_SONG_ROLE, 1]
end