Skip to content

Commit 93f31fb

Browse files
committed
refactor sketch creator and tests
1 parent e6d91de commit 93f31fb

3 files changed

Lines changed: 30 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
**v1.3.0** Not strictly semantic versioning in the sense that the significant internal refactoring of launcher.rb should not be evident to user. Bump to jruby-9.1.8.0 which should be last before jruby-9.2.x.x series (ruby-2.4 here we come).
1+
**v1.3.0** Not strictly semantic versioning in the sense that the significant internal refactoring of `launcher.rb` (and `sketch_writer.rb`) should not be evident to user. Bump to jruby-9.1.8.0 which should be last before jruby-9.2.x.x series (ruby-2.4 here we come).
22

33
**v1.2.9** Grid method now in java (and corrected for step greater than 1). Update to processing-3.3. Update examples to 2.0, which features WOVNS examples that make use of `grid` method and in part caused me to look again a its implementation.
44

lib/jruby_art/creators/sketch_writer.rb

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,34 @@ def initialize(path, args)
3333
end
3434

3535
def write
36-
template = SketchTemplate.template
37-
sketch = template.code(param)
36+
sketch = SketchFactory.create(param)
3837
File.open(file, 'w+') { |f| f.write sketch.join("\n") }
3938
end
4039
end
4140

4241
# Implements methods and class_methods omits blank line after draw
4342
# uses private method_lines to format method lines
4443
class Sketch
44+
attr_reader :param, :lines
45+
46+
def initialize(param)
47+
@param = param
48+
self
49+
end
50+
4551
BLANK ||= ''.freeze
4652
INDENT ||= ' '.freeze
4753

48-
def methods(param, indent)
54+
def methods(indent)
4955
lines = []
5056
lines.concat method_lines('settings', param.sketch_size, indent)
5157
lines.concat method_lines('setup', param.sketch_title, indent)
5258
lines.concat method_lines('draw', BLANK, indent)
5359
end
5460

55-
def class_methods(param)
61+
def class_methods
5662
lines = [format('class %s < Processing::App', param.class_name)]
57-
lines.concat methods(param, INDENT)
63+
lines.concat methods(INDENT)
5864
lines << 'end'
5965
end
6066

@@ -70,37 +76,37 @@ def method_lines(name, content, indent)
7076
end
7177

7278
# Switch templates on config
73-
class SketchTemplate
74-
def self.template
79+
class SketchFactory
80+
def self.create(param)
7581
case Processing::RP_CONFIG.fetch('template', 'bare')
7682
when /bare/
77-
return BareSketch.new
83+
BareSketch
7884
when /class/
79-
return ClassSketch.new
85+
ClassSketch
8086
when /emacs/
81-
return EmacsSketch.new
82-
end
87+
EmacsSketch
88+
end.new(param).code
8389
end
8490
end
8591

8692
# The sketch class creates an array of formatted sketch lines
8793
class BareSketch < Sketch
88-
def code(param)
89-
methods(param, BLANK)
94+
def code
95+
methods(BLANK)
9096
end
9197
end
9298

9399
# A simple class wrapped sketch
94100
class ClassSketch < Sketch
95-
def code(param)
101+
def code
96102
lines = ['# frozen_string_literal: false', BLANK]
97-
lines.concat class_methods(param)
103+
lines.concat class_methods
98104
end
99105
end
100106

101107
# A sketch that will run with jruby, for emacs etc
102108
class EmacsSketch < Sketch
103-
def code(param)
109+
def code
104110
lines = [
105111
'# frozen_string_literal: false',
106112
"require 'jruby_art'",
@@ -109,7 +115,7 @@ def code(param)
109115
'Processing::App::SKETCH_PATH = __FILE__.freeze',
110116
BLANK
111117
]
112-
lines.concat class_methods(param)
118+
lines.concat class_methods
113119
lines << BLANK
114120
lines << format('%s.new unless defined? $app', param.class_name)
115121
end

test/create_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
gem 'minitest' # don't use bundled minitest
1+
gem 'minitest' # don't use bundled minitest
22
require 'minitest/autorun'
33
require 'minitest/pride'
44

@@ -87,24 +87,24 @@ def test_parameter_new
8787

8888
def test_bare
8989
result = BARE.split(/\n/, -1)
90-
sketch = BareSketch.new.code(@param)
90+
sketch = BareSketch.new(@param).code
9191
sketch.each_with_index do |line, i|
9292
assert_equal result[i], line
9393
end
9494
end
9595

9696
def test_class
9797
result = CLASS_SKETCH.split(/\n/, -1)
98-
class_lines = ClassSketch.new.code(@param)
99-
class_lines.each_with_index do |line, i|
98+
sketch = ClassSketch.new(@param).code
99+
sketch.each_with_index do |line, i|
100100
assert_equal result[i], line
101101
end
102102
end
103103

104104
def test_emacs
105105
result = EMACS.split(/\n/, -1)
106-
class_lines = EmacsSketch.new.code(@param)
107-
class_lines.each_with_index do |line, i|
106+
sketch = EmacsSketch.new(@param).code
107+
sketch.each_with_index do |line, i|
108108
assert_equal result[i], line
109109
end
110110
end

0 commit comments

Comments
 (0)