Class: WTFChord::Rules

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Rules

Returns a new instance of Rules.



8
9
10
11
12
13
14
15
16
17
# File 'lib/wtf_chord/rules.rb', line 8

def initialize(path)
  @path = path
  rules = YAML.load_file(@path)
  @chords  = rules[:chords]
  @extra   = rules[:extra]
  @pattern = /
    (?<name>#{Regexp.union(@chords.keys.sort_by(&:length).reverse!)})?
    (?<ext>#{Regexp.union(@extra.keys.sort_by(&:length).reverse!)})?
  /x
end

Instance Attribute Details

#chordsObject (readonly)

Returns the value of attribute chords.



6
7
8
# File 'lib/wtf_chord/rules.rb', line 6

def chords
  @chords
end

#extraObject (readonly)

Returns the value of attribute extra.



6
7
8
# File 'lib/wtf_chord/rules.rb', line 6

def extra
  @extra
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/wtf_chord/rules.rb', line 6

def path
  @path
end

#patternObject (readonly)

Returns the value of attribute pattern.



6
7
8
# File 'lib/wtf_chord/rules.rb', line 6

def pattern
  @pattern
end

Instance Method Details

#find(name = nil) ⇒ Object Also known as: []



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wtf_chord/rules.rb', line 19

def find(name = nil)
  steps = []
  name ||= ""

  name.match(pattern) do |m|
    base = chords[m[:name]] || chords["M"]
    steps.concat(base)

    ext = extra[m[:ext]] if m[:ext]
    steps.concat(ext) if ext
  end

  steps.tap(&:uniq!)
end