Skip to content
This repository
Browse code

Exposed PitchClass.[] and made it take a single argument (name) inste…

…ad of name,value.

This makes the interface more consistent with Pitch and cleans up some evaluation logic in the grammar.
  • Loading branch information...
commit 0604eeeb836d5bb20b4eafc95e0626dbfa72896e 1 parent 1816a0d
Adam Murray authored

Showing 2 changed files with 27 additions and 21 deletions. Show diff stats Hide diff stats

  1. +12 14 lib/mtk/pitch_class.rb
  2. +15 7 spec/mtk/pitch_class_spec.rb
26 lib/mtk/pitch_class.rb
@@ -34,29 +34,27 @@ def initialize(name, int_value)
34 34
35 35 @flyweight = {}
36 36
37   - # Return a new object, only constructing a new instance when not already in the flyweight cache
38   - def self.[](name, value)
39   - @flyweight[name] ||= new(name, value) if VALID_NAMES.include? name
40   - end
41   - private_class_method :[]
42   -
43   - def self.from_s(s)
44   - s = s.to_s
45   - s = s[0..0].upcase + s[1..-1].downcase # normalize the name
  37 + # Lookup a PitchClass by name.
  38 + # @param name [String, #to_s] one of the values in {VALID_NAMES}
  39 + def self.[](name)
  40 + name = name.to_s
  41 + name = name[0..0].upcase + name[1..-1].downcase # normalize the name
46 42 VALID_NAMES_BY_VALUE.each_with_index do |names, value|
47   - return self[s, value] if names.include? s
  43 + if names.include? name
  44 + return @flyweight[name] ||= new(name, value)
  45 + end
48 46 end
49 47 nil
50 48 end
51 49
52 50 class << self
53   - alias from_name from_s # alias self.from_name to self.from_s
  51 + alias :from_s :[]
  52 + alias :from_name :[]
54 53 end
55 54
56 55 def self.from_i(value)
57   - value = value.to_i % 12
58   - name = NAMES[value]
59   - self[name, value]
  56 + name = NAMES[value.to_i % 12]
  57 + self[name]
60 58 end
61 59
62 60 def == other
22 spec/mtk/pitch_class_spec.rb
@@ -48,29 +48,37 @@
48 48 describe '.from_s' do
49 49 context "the argument is a valid name" do
50 50 it "returns a PitchClass" do
51   - names.each { |name| PitchClass.from_s(name).should be_a PitchClass }
  51 + names.each { |name| PitchClass[name].should be_a PitchClass }
52 52 end
53 53 it "returns an object with that name" do
54   - names.each { |name| PitchClass.from_s(name).name.should == name }
  54 + names.each { |name| PitchClass[name].name.should == name }
55 55 end
56 56 it "ignores case" do
57 57 for name in names
58   - PitchClass.from_s(name.upcase).name.should == name
59   - PitchClass.from_s(name.downcase).name.should == name
  58 + PitchClass[name.upcase].name.should == name
  59 + PitchClass[name.downcase].name.should == name
60 60 end
61 61 end
62 62 end
63 63 context "the argument is not a valid name" do
64 64 it "returns nil, if the name doesn't exist" do
65   - PitchClass.from_s('z').should be_nil
  65 + PitchClass['z'].should be_nil
  66 + end
  67 + end
  68 + end
  69 +
  70 + describe '.from_s' do
  71 + it "acts like .[]" do
  72 + for name in ['C', 'bbb', 'z']
  73 + PitchClass.from_s(name).should == PitchClass[name]
66 74 end
67 75 end
68 76 end
69 77
70 78 describe '.from_name' do
71   - it "acts like from_s" do
  79 + it "acts like .[]" do
72 80 for name in ['C', 'bbb', 'z']
73   - PitchClass.from_name(name).should == PitchClass.from_s(name)
  81 + PitchClass.from_name(name).should == PitchClass[name]
74 82 end
75 83 end
76 84 end

0 comments on commit 0604eee

Please sign in to comment.
Something went wrong with that request. Please try again.