Class: UltimateLyrics::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/ultimate_lyrics/field.rb

Constant Summary collapse

DATA =
{
  'artist' => %w[artist lower],
  'artist2' => %w[artist lower nospace],
  'album' => %w[album lower],
  'album2' => %w[album lower nospace],
  'title' => %w[title lower],
  'Artist' => %w[artist],
  'Album' => %w[album],
  'ARTIST' => %w[artist upper],
  'year' => %w[year pretty],
  'Title' => %w[title],
  'Title2' => %w[title titlecase],
  'a' => %w[artist firstchar],
  'track' => %w[track number]
}.freeze
BEGIN_DELIMITER =
'{'
END_DELIMITER =
'}'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allArray<UltimateLyrics::Field>

Returns:



26
27
28
29
30
31
# File 'lib/ultimate_lyrics/field.rb', line 26

def all
  @all ||= DATA.map do |name, modifiers|
    source_attr = modifiers.shift
    new(name, source_attr, modifiers)
  end
end

.by_name(name) ⇒ UltimateLyrics::Field



34
35
36
37
# File 'lib/ultimate_lyrics/field.rb', line 34

def by_name(name)
  all.find { |field| field.name == name } ||
    raise("No field with name \"#{name}\" (Available: #{all.values})")
end

.from_string(string) ⇒ Array<UltimateLyrics::Field>

Returns:



40
41
42
# File 'lib/ultimate_lyrics/field.rb', line 40

def from_string(string)
  names_from_string(string).map { |name| by_name(name) }
end

.names_from_string(string) ⇒ Array<String>

Returns:

  • (Array<String>)


45
46
47
48
# File 'lib/ultimate_lyrics/field.rb', line 45

def names_from_string(string)
  string.scan(/#{::Regexp.quote(BEGIN_DELIMITER)}[^\}]+#{::Regexp.quote(END_DELIMITER)}/)
        .map { |s| s.delimited_inner(BEGIN_DELIMITER, END_DELIMITER) }
end

Instance Method Details

#apply(string, value) ⇒ Object



53
54
55
# File 'lib/ultimate_lyrics/field.rb', line 53

def apply(string, value)
  string.gsub(apply_pattern, value)
end

#apply_patternObject



57
58
59
# File 'lib/ultimate_lyrics/field.rb', line 57

def apply_pattern
  /#{::Regexp.quote(BEGIN_DELIMITER)}#{::Regexp.quote(name)}#{::Regexp.quote(END_DELIMITER)}/
end

#to_sObject



65
66
67
# File 'lib/ultimate_lyrics/field.rb', line 65

def to_s
  name
end

#value(value) ⇒ Object



61
62
63
# File 'lib/ultimate_lyrics/field.rb', line 61

def value(value)
  modifiers.inject(value.to_s) { |a, e| send(e, a) }
end