Class: MetalArchives::Parsers::Genre

Inherits:
Base
  • Object
show all
Defined in:
lib/metal_archives/parsers/genre.rb

Overview

Genre parser

Constant Summary collapse

SUFFIXES =
%w((early) (later) metal).freeze

Class Method Summary collapse

Class Method Details

.parse(input) ⇒ Object

Opinionated parsing of genres

Returns an Array of String

The following components are omitted:

  • Metal

  • (early)

  • (later)

All genres are capitalized.

For examples on how genres are parsed, refer to gnre_spec.rb



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/metal_archives/parsers/genre.rb', line 25

def self.parse(input)
  genres = []
  # Split fields
  input.split(/[,;]/).each do |genre|
    ##
    # Start with a single empty genre string. Split the genre by spaces
    # and process each component. If a component does not have a slash,
    # concatenate it to all genre strings present in +temp+. If it does
    # have a slash present, duplicate all genre strings, and concatenate
    # the first component (before the slash) to the first half, and the
    # last component to the last half. +temp+ now has an array of genre
    # combinations.
    #
    # 'Traditional Heavy/Power Metal' => ['Traditional Heavy', 'Traditional Power']
    # 'Traditional/Classical Heavy/Power Metal' => [
    #                                       'Traditional Heavy', 'Traditional Power',
    #                                       'Classical Heavy', 'Classical Power']
    #
    temp = [""]

    genre.downcase.split.reject { |g| SUFFIXES.include? g }.each do |g|
      if g.include? "/"
        # Duplicate all WIP genres
        temp2 = temp.dup

        # Assign first and last components to temp and temp2 respectively
        split = g.split "/"
        temp.map! { |t| t.empty? ? split.first.capitalize : "#{t.capitalize} #{split.first.capitalize}" }
        temp2.map! { |t| t.empty? ? split.last.capitalize : "#{t.capitalize} #{split.last.capitalize}" }

        # Add both genre trees
        temp += temp2
      else
        temp.map! { |t| t.empty? ? g.capitalize : "#{t.capitalize} #{g.capitalize}" }
      end
    end
    genres += temp
  end
  genres.uniq
end