stringed 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stringed (0.0.1)
4
+ stringed (0.0.2)
5
5
  music (~> 0.6)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -18,8 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- guitar = Stringed::Instrument.new(%w( E2 A2 D3 G3 B3 E4 ))
22
- guitar.
21
+ See specs for usage.
23
22
 
24
23
  ## Contributing
25
24
 
@@ -27,4 +26,4 @@ Or install it yourself as:
27
26
  2. Create your feature branch (`git checkout -b my-new-feature`)
28
27
  3. Commit your changes (`git commit -am 'Add some feature'`)
29
28
  4. Push to the branch (`git push origin my-new-feature`)
30
- 5. Create new Pull Request
29
+ 5. Create new Pull Request. Please provide tests.
@@ -2,9 +2,9 @@ require "music"
2
2
  require "stringed/version"
3
3
  require "stringed/instrument"
4
4
  require "stringed/instrument_string"
5
+ require "stringed/formatters"
5
6
 
6
7
  module Stringed
7
- # Your code goes here...
8
8
 
9
9
  class Music::Note
10
10
  def name
@@ -0,0 +1,67 @@
1
+ module Stringed
2
+
3
+ module Formatters
4
+
5
+ module ASCII
6
+ def self.fret_numbers(string)
7
+ fret_numbers = " "*(string.to_s.length)
8
+ string.frets.each{ |fret| fret_numbers << " "*6 << fret.to_s }
9
+ fret_numbers << "\n"
10
+ end
11
+ end
12
+
13
+ class ASCIIString
14
+ include ASCII
15
+
16
+ def initialize(string,options)
17
+ @selected_frets = options.fetch(:selected_frets,[])
18
+ @output = "#{string.to_s} |"
19
+ @string = string
20
+ @string.frets.each do |fret|
21
+ if @selected_frets.include?(fret) then
22
+ @output << "--x--|"
23
+ else
24
+ @output << "-----|"
25
+ end
26
+ end
27
+ @output << "\n"
28
+ @output << fret_numbers if options.has_key? :numbered_frets
29
+ end
30
+
31
+ def fret_numbers
32
+ ASCII.fret_numbers(@string)
33
+ end
34
+
35
+ def to_s
36
+ @output
37
+ end
38
+
39
+ end
40
+
41
+ class ASCIIInstrument
42
+ include ASCII
43
+
44
+ def initialize(instrument, options={})
45
+ @selected_frets = options.fetch(:selected_frets,[])
46
+ @output = ""
47
+ @instrument = instrument
48
+ @instrument.strings.each_with_index do |string,i|
49
+ selected_frets = @selected_frets[i] ? @selected_frets[i] : []
50
+ @output << string.to(:ascii,selected_frets: selected_frets ).to_s
51
+ end
52
+ @output << fret_numbers if options.fetch(:numbered_frets,true)
53
+ end
54
+
55
+ def fret_numbers
56
+ ASCII.fret_numbers(@instrument.strings[@instrument.strings.map{|i| i.to_s.length}.each_with_index.max[1]])
57
+ end
58
+
59
+ def to_s
60
+ @output
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -29,6 +29,13 @@ module Stringed
29
29
  fret_no >= 0 and fret_no <= fret_count
30
30
  end
31
31
 
32
+ def to(format,options={})
33
+ require "stringed/formatters"
34
+ if format == :ascii then
35
+ Formatters::ASCIIInstrument.new(self,options)
36
+ end
37
+ end
38
+
32
39
  end
33
40
 
34
41
  end
@@ -3,11 +3,12 @@ module Stringed
3
3
  class InstrumentString
4
4
  include Music
5
5
 
6
- attr_reader :root_note, :fret_count
6
+ attr_reader :root_note, :fret_count, :frets
7
7
 
8
8
  def initialize(root_note, options={})
9
9
  @root_note = Music::Note.new(root_note.to_s)
10
10
  @fret_count = options.fetch(:fret_count,14)
11
+ @frets =* (1..@fret_count)
11
12
  end
12
13
 
13
14
  def fret_note(fret_no)
@@ -48,5 +49,12 @@ module Stringed
48
49
  matches.flatten.compact.sort
49
50
  end
50
51
 
52
+ def to(format,options={})
53
+ require "stringed/formatters"
54
+ if format == :ascii then
55
+ Formatters::ASCIIString.new(self,options)
56
+ end
57
+ end
58
+
51
59
  end
52
60
  end
@@ -1,3 +1,3 @@
1
1
  module Stringed
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -4,20 +4,20 @@ describe Stringed::Instrument do
4
4
 
5
5
  let(:guitar) { Stringed::Instrument.new(%w( E2 A2 D3 G3 B3 E4 ), fret_count: 20) }
6
6
 
7
- it "should have strings" do
7
+ it "has strings" do
8
8
  guitar.strings.count.should == 6
9
9
  end
10
10
 
11
- it "should have a fret count" do
11
+ it "has a fret count" do
12
12
  guitar.fret_count.should == 20
13
13
  end
14
14
 
15
- it "should know where all the matches for a given note are" do
15
+ it "knows where all the matches for a given note are" do
16
16
  guitar.find('F').should eq [[1,13], [8,20], [3,15], [10], [6,18], [1,13]]
17
17
  guitar.find('D').should eq [[10], [5, 17], [0, 12], [7, 19], [3, 15], [10]]
18
18
  end
19
19
 
20
- it "should know where all the matches for a given chord are" do
20
+ it "knows where all the matches for a given chord are" do
21
21
  guitar.find_chord(Chord.new(['C4', 'E4', 'G4'])).should eq [[0, 3, 8, 12, 15, 20],
22
22
  [3, 7, 10, 15, 19],
23
23
  [2, 5, 10, 14, 17],
@@ -26,4 +26,26 @@ describe Stringed::Instrument do
26
26
  [0, 3, 8, 12, 15, 20]]
27
27
  end
28
28
 
29
+ describe "formatting"
30
+ context "ascii" do
31
+ let(:nano) { Stringed::Instrument.new(%w( C4 G4 ), fret_count: 6)}
32
+
33
+ it "can print an ascii fretboard" do
34
+ nano.to(:ascii).to_s.should eq <<-ASCII
35
+ C4 |#{"-----|" * 6}
36
+ G4 |#{"-----|" * 6}
37
+ 1 2 3 4 5 6
38
+ ASCII
39
+ end
40
+
41
+ it "can print selected frets" do
42
+ nano.to(:ascii, selected_frets: [[1,4],[3]]).to_s.should eq <<-ASCII
43
+ C4 |--x--|-----|-----|--x--|-----|-----|
44
+ G4 |-----|-----|--x--|-----|-----|-----|
45
+ 1 2 3 4 5 6
46
+ ASCII
47
+ end
48
+
49
+ end
50
+
29
51
  end
@@ -40,4 +40,21 @@ describe InstrumentString do
40
40
  e2.find_chord(Chord.new(["D3","F#3","A3"])).should eq [2,5,10,14,17]
41
41
  end
42
42
 
43
+ context "formatting" do
44
+ let(:e2){ Stringed::InstrumentString.new('E2', { :fret_count => 6 } ) }
45
+
46
+ it "can produce an Ascii format" do
47
+ e2.to(:ascii).to_s.should eq "E2 " << "|-----" * 6 << "|\n"
48
+ end
49
+
50
+ it "can produce an Ascii format with selected notes" do
51
+ e2.to(:ascii, selected_frets: [2,5]).to_s.should eq "E2 |-----|--x--|-----|-----|--x--|-----|\n"
52
+ end
53
+
54
+ it "can number the frets" do
55
+ e2.to(:ascii, numbered_frets: true).to_s.should eq "E2 " << "|-----" * 6 << "|\n" << " 1 2 3 4 5 6\n"
56
+ end
57
+
58
+ end
59
+
43
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stringed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-03 00:00:00.000000000 Z
12
+ date: 2013-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: music
@@ -58,6 +58,7 @@ files:
58
58
  - README.md
59
59
  - Rakefile
60
60
  - lib/stringed.rb
61
+ - lib/stringed/formatters.rb
61
62
  - lib/stringed/instrument.rb
62
63
  - lib/stringed/instrument_string.rb
63
64
  - lib/stringed/version.rb