tortoise 0.9.0 → 0.9.1

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.
data/bin/tortoise CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  $:.push File.expand_path("../../lib", __FILE__)
3
- require 'tortoise/interpreter'
3
+ require 'tortoise'
4
4
 
5
5
  def print_usage
6
6
  puts "Usage: tortoise [OPTIONS] [FILE]\n\n"
@@ -35,28 +35,11 @@ module Tortoise
35
35
  commands.each { |command| execute(command) }
36
36
  end
37
37
 
38
- def to_ascii
39
- s = ''
40
- oriented_canvas.each do |column|
41
- column.each do |pixel|
42
- char = pixel ? 'X' : '.'
43
- s += "#{char} "
44
- end
45
- s = s.strip + "\n"
38
+ # define presenter formats
39
+ %w(ascii html).each do |format|
40
+ define_method("to_#{format}") do
41
+ Tortoise::Presenter.new(self).send("to_#{format}")
46
42
  end
47
- s
48
- end
49
-
50
- def to_html
51
- <<-HTML
52
- <!DOCTYPE html>
53
- <html>
54
- #{html_head}
55
- <body>
56
- #{html_canvas}
57
- </body>
58
- </html>
59
- HTML
60
43
  end
61
44
 
62
45
  private
@@ -121,66 +104,5 @@ module Tortoise
121
104
  y = @size - 1 if y >= @size
122
105
  [x, y]
123
106
  end
124
-
125
- def oriented_canvas
126
- oriented = new_canvas
127
- @canvas.each_with_index do |column, i|
128
- column.each_with_index do |pixel, j|
129
- oriented[@size-1-j][i] = pixel
130
- end
131
- end
132
- oriented
133
- end
134
-
135
- def html_head
136
- pixel_size = 8
137
- <<-HTML
138
- <head>
139
- <title>Tortoise</title>
140
- <style type="text/css">
141
- * { margin: 0; padding: 0; }
142
-
143
- body { background: #555; }
144
-
145
- #canvas {
146
- overflow: hidden;
147
- border: #{pixel_size}px solid #000;
148
- width: #{@size * pixel_size}px;
149
- margin: 50px auto 10px auto;
150
- }
151
-
152
- .column {
153
- float: left;
154
- }
155
-
156
- .pixel {
157
- width: #{pixel_size}px;
158
- height: #{pixel_size}px;
159
- }
160
-
161
- .empty {
162
- background: #ddd;
163
- }
164
-
165
- .filled {
166
- background: #111;
167
- }
168
- </style>
169
- </head>
170
- HTML
171
- end
172
-
173
- def html_canvas
174
- html = "<div id='canvas'>"
175
- @canvas.each do |column|
176
- html += "<div class='column'>"
177
- column.reverse.each do |pixel|
178
- pixel_class = pixel ? 'filled' : 'empty'
179
- html += "<div class='pixel #{pixel_class}'></div>"
180
- end
181
- html += "</div>"
182
- end
183
- html += "</div>"
184
- end
185
107
  end
186
108
  end
@@ -0,0 +1,89 @@
1
+ module Tortoise
2
+ class Presenter
3
+ attr_reader :canvas
4
+
5
+ PIXEL_SIZE = 5
6
+
7
+ def initialize(interpreter)
8
+ @canvas = interpreter.canvas
9
+ end
10
+
11
+ def to_ascii
12
+ oriented_canvas.inject('') do |ascii, column|
13
+ column.each do |pixel|
14
+ char = pixel ? 'X' : '.'
15
+ ascii += "#{char} "
16
+ end
17
+ ascii = ascii.strip + "\n"
18
+ end
19
+ end
20
+
21
+ def to_html
22
+ <<-HTML
23
+ <!DOCTYPE html>
24
+ <html>
25
+ #{html_head}
26
+ <body>
27
+ #{html_canvas}
28
+ </body>
29
+ </html>
30
+ HTML
31
+ end
32
+
33
+ private
34
+
35
+ def new_canvas
36
+ Array.new(@canvas.size) { Array.new(@canvas.size) {false} }
37
+ end
38
+
39
+ def oriented_canvas
40
+ oriented = new_canvas
41
+ @canvas.each_with_index do |column, i|
42
+ column.each_with_index do |pixel, j|
43
+ oriented[@canvas.size-1-j][i] = pixel
44
+ end
45
+ end
46
+ oriented
47
+ end
48
+
49
+ def html_head
50
+ <<-HTML
51
+ <head>
52
+ <title>Tortoise</title>
53
+ <style type="text/css">
54
+ * { margin: 0; padding: 0; }
55
+
56
+ body { background: #555; }
57
+
58
+ .column { float: left; }
59
+
60
+ .empty { background: #ddd; }
61
+
62
+ .filled { background: #111; }
63
+
64
+ #canvas {
65
+ overflow: hidden;
66
+ border: #{PIXEL_SIZE}px solid #111;
67
+ width: #{@canvas.size * PIXEL_SIZE}px;
68
+ margin: 50px auto 10px auto; }
69
+
70
+ .pixel {
71
+ width: #{PIXEL_SIZE}px;
72
+ height: #{PIXEL_SIZE}px; }
73
+ </style>
74
+ </head>
75
+ HTML
76
+ end
77
+
78
+ def html_canvas
79
+ @canvas.inject("<div id='canvas'>") do |html, column|
80
+ html += "<div class='column'>"
81
+ column.reverse.each do |pixel|
82
+ pixel_class = pixel ? 'filled' : 'empty'
83
+ html += "<div class='pixel #{pixel_class}'></div>"
84
+ end
85
+ html += "</div>"
86
+ end + "</div>"
87
+ end
88
+ end
89
+ end
@@ -1,3 +1,3 @@
1
1
  module Tortoise
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
data/lib/tortoise.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'tortoise/interpreter'
2
+ require 'tortoise/presenter'
2
3
  require 'tortoise/version'
File without changes
@@ -3,7 +3,7 @@ require 'tortoise'
3
3
  describe Tortoise::Interpreter do
4
4
  it 'produces the expected ascii output' do
5
5
  input = File.new('./spec/data/simple.logo').read
6
- output = File.new('./spec/data/simple_out.txt').read
6
+ output = File.new('./spec/data/simple_out.ascii').read
7
7
 
8
8
  Tortoise::Interpreter.new(input).to_ascii.should == output
9
9
  end
@@ -11,11 +11,9 @@ describe Tortoise::Interpreter do
11
11
  it 'can be initialized with instructions' do
12
12
  instructions = <<-STEPS
13
13
  5
14
-
15
14
  REPEAT 2 [ RT 45 ]
16
15
  FD 1
17
16
  STEPS
18
-
19
17
  tortoise = Tortoise::Interpreter.new(instructions)
20
18
  tortoise.size.should == 5
21
19
  tortoise.direction.should == 90
@@ -51,7 +49,6 @@ describe Tortoise::Interpreter do
51
49
  RT 180
52
50
  FD 2
53
51
  STEPS
54
-
55
52
  tortoise.canvas.should == [
56
53
  [false, false, false, false, false],
57
54
  [false, false, false, false, false],
@@ -80,29 +77,6 @@ describe Tortoise::Interpreter do
80
77
  end
81
78
  end
82
79
 
83
- describe '#to_ascii' do
84
- it 'renders canvas to ascii' do
85
- tortoise = Tortoise::Interpreter.new(5)
86
- tortoise.draw <<-STEPS
87
- RT 90
88
- FD 1
89
- REPEAT 2 [ RT 45 ]
90
- FD 2
91
- REPEAT 2 [ LT 45 ]
92
- FD 1
93
- RT 180
94
- FD 2
95
- STEPS
96
-
97
- tortoise.to_ascii.should == "" +
98
- ". . . . .\n" +
99
- ". . . . .\n" +
100
- ". . X X .\n" +
101
- ". . . X .\n" +
102
- ". . X X X\n"
103
- end
104
- end
105
-
106
80
  describe '#rt' do
107
81
  it 'can rotate the tortoise 45 degrees to the right' do
108
82
  tortoise.rt(45)
@@ -464,4 +438,26 @@ describe Tortoise::Interpreter do
464
438
  tortoise.direction.should == 90
465
439
  end
466
440
  end
441
+
442
+ describe 'canvas rendering' do
443
+ before do
444
+ @presenter = stub('presenter')
445
+ Tortoise::Presenter.stub(:new => @presenter)
446
+ @interpreter = Tortoise::Interpreter.new(5)
447
+ end
448
+
449
+ describe '#to_ascii' do
450
+ it 'delegates rendering to presenter' do
451
+ @presenter.should_receive(:to_ascii)
452
+ @interpreter.to_ascii
453
+ end
454
+ end
455
+
456
+ describe '#to_html' do
457
+ it 'delegates rendering to presenter' do
458
+ @presenter.should_receive(:to_html)
459
+ @interpreter.to_html
460
+ end
461
+ end
462
+ end
467
463
  end
@@ -0,0 +1,48 @@
1
+ require 'tortoise/presenter.rb'
2
+
3
+ describe Tortoise::Presenter do
4
+ it 'saves the interpreter canvas' do
5
+ interpreter = stub(:size => stub, :canvas => stub)
6
+ presenter = Tortoise::Presenter.new(interpreter)
7
+ presenter.canvas.should == interpreter.canvas
8
+ end
9
+
10
+ describe 'rendering engines' do
11
+ before do
12
+ interpreter = Tortoise::Interpreter.new <<-STEPS
13
+ 5
14
+ RT 90
15
+ FD 1
16
+ REPEAT 2 [ RT 45 ]
17
+ FD 2
18
+ REPEAT 2 [ LT 45 ]
19
+ FD 1
20
+ RT 180
21
+ FD 2
22
+ STEPS
23
+ @presenter = Tortoise::Presenter.new(interpreter)
24
+ end
25
+
26
+ describe '#to_ascii' do
27
+ it 'renders ascii representation of canvas' do
28
+ @presenter.to_ascii.should == "" +
29
+ ". . . . .\n" +
30
+ ". . . . .\n" +
31
+ ". . X X .\n" +
32
+ ". . . X .\n" +
33
+ ". . X X X\n"
34
+ end
35
+ end
36
+
37
+ describe '#to_html' do
38
+ it 'renders html representation of canvas' do
39
+ html = @presenter.to_html
40
+ html.scan('html').size.should == 3
41
+ html.scan('body').size.should == 3
42
+ html.scan('canvas').size.should == 2
43
+ html.scan('column').size.should == 6
44
+ html.scan('pixel').size.should == 26
45
+ end
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tortoise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70219246665200 !ruby/object:Gem::Requirement
16
+ requirement: &70296698732460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70219246665200
24
+ version_requirements: *70296698732460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70219246663560 !ruby/object:Gem::Requirement
27
+ requirement: &70296698726480 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70219246663560
35
+ version_requirements: *70296698726480
36
36
  description: Tortoise is a Logo interpreter for ruby.
37
37
  email:
38
38
  - huntca@gmail.com
@@ -49,12 +49,14 @@ files:
49
49
  - bin/tortoise
50
50
  - lib/tortoise.rb
51
51
  - lib/tortoise/interpreter.rb
52
+ - lib/tortoise/presenter.rb
52
53
  - lib/tortoise/version.rb
53
54
  - spec/data/complex.logo
54
55
  - spec/data/simple.logo
55
- - spec/data/simple_out.txt
56
+ - spec/data/simple_out.ascii
56
57
  - spec/integration/tortoise_integration_spec.rb
57
58
  - spec/tortoise/interpreter_spec.rb
59
+ - spec/tortoise/presenter_spec.rb
58
60
  - tortoise.gemspec
59
61
  homepage: https://github.com/huntca/tortoise
60
62
  licenses: []
@@ -83,6 +85,7 @@ summary: Tortoise is a Logo interpreter for ruby.
83
85
  test_files:
84
86
  - spec/data/complex.logo
85
87
  - spec/data/simple.logo
86
- - spec/data/simple_out.txt
88
+ - spec/data/simple_out.ascii
87
89
  - spec/integration/tortoise_integration_spec.rb
88
90
  - spec/tortoise/interpreter_spec.rb
91
+ - spec/tortoise/presenter_spec.rb