Class: WTFChord::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/wtf_chord/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, with_rates = false) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
10
11
# File 'lib/wtf_chord/formatter.rb', line 7

def initialize(name, with_rates = false)
  @name = name
  @with_rates = with_rates
  require_formatter! unless formatter?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/wtf_chord/formatter.rb', line 5

def name
  @name
end

Instance Method Details

#call(*fingerings) ⇒ Object Also known as: []



13
14
15
# File 'lib/wtf_chord/formatter.rb', line 13

def call(*fingerings)
  formatter.with_rates(@with_rates) { |f| fingerings.map(&f) }
end

#formatterObject



23
24
25
# File 'lib/wtf_chord/formatter.rb', line 23

def formatter
  Formatters.const_get(formatter_name)
end

#formatter?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/wtf_chord/formatter.rb', line 27

def formatter?
  Formatters.const_defined?(formatter_name)
end

#formatter_nameObject



31
32
33
# File 'lib/wtf_chord/formatter.rb', line 31

def formatter_name
  @formatter_name ||= @name.gsub(/(?:^|_)([a-z\d]*)/i) { "#{$1.capitalize}" }.to_s.freeze
end

#require_formatter!Object



35
36
37
# File 'lib/wtf_chord/formatter.rb', line 35

def require_formatter!
  require "wtf_chord/formatters/#{@name}"
end

#separatorObject



19
20
21
# File 'lib/wtf_chord/formatter.rb', line 19

def separator
  formatter.separator
end