Skip to content

Commit df85d2b

Browse files
authored
Merge pull request #19 from ruby-processing/revert_refinements
revert refinements
2 parents 09ae79b + b9af372 commit df85d2b

File tree

2 files changed

+81
-77
lines changed

2 files changed

+81
-77
lines changed

lib/jruby_art/creators/creator.rb

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22

33
BASIC = <<-CODE
44
def setup
5-
sketch_title '%s'
5+
sketch_title '%s'
66
end
77
88
def draw
99
1010
end
1111
1212
def settings
13-
size %s, %s
14-
# pixel_density(2) # here for hi-dpi displays only
15-
# smooth # here
13+
size %s, %s
14+
# pixel_density(2) # here for hi-dpi displays only
15+
# smooth # here
1616
end
1717
1818
CODE
1919

2020
BASIC_MODE = <<-CODE
2121
def setup
22-
sketch_title '%s'
22+
sketch_title '%s'
2323
end
2424
2525
def draw
2626
2727
end
2828
2929
def settings
30-
size %s, %s, %s
31-
# smooth # here
30+
size %s, %s, %s
31+
# smooth # here
3232
end
3333
3434
CODE
@@ -37,19 +37,19 @@ def settings
3737
# encoding: utf-8
3838
# frozen_string_literal: false
3939
class %s < Processing::App
40-
def setup
41-
sketch_title '%s'
42-
end
40+
def setup
41+
sketch_title '%s'
42+
end
4343
44-
def draw
44+
def draw
4545
46-
end
46+
end
4747
48-
def settings
49-
size %s, %s
50-
# pixel_density(2) # here for hi-dpi displays only
51-
# smooth # here
52-
end
48+
def settings
49+
size %s, %s
50+
# pixel_density(2) # here for hi-dpi displays only
51+
# smooth # here
52+
end
5353
end
5454
CODE
5555

@@ -59,21 +59,21 @@ def settings
5959
require 'jruby_art'
6060
require 'jruby_art/app'
6161
62-
Processing::App::SKETCH_PATH = __FILE__
62+
Processing::App::SKETCH_PATH = __FILE__.freeze
6363
6464
class %s < Processing::App
65-
def setup
66-
sketch_title '%s'
67-
end
65+
def setup
66+
sketch_title '%s'
67+
end
6868
69-
def draw
69+
def draw
7070
71-
end
71+
end
7272
73-
def settings
74-
size %s, %s
75-
# smooth # here
76-
end
73+
def settings
74+
size %s, %s
75+
# smooth # here
76+
end
7777
end
7878
7979
%s.new unless defined? $app
@@ -83,18 +83,18 @@ def settings
8383
# encoding: utf-8
8484
# frozen_string_literal: false
8585
class %s < Processing::App
86-
def setup
87-
sketch_title '%s'
88-
end
86+
def setup
87+
sketch_title '%s'
88+
end
8989
90-
def draw
90+
def draw
9191
92-
end
92+
end
9393
94-
def settings
95-
size %s, %s, %s
96-
# smooth # here
97-
end
94+
def settings
95+
size %s, %s, %s
96+
# smooth # here
97+
end
9898
end
9999
CODE
100100

@@ -104,21 +104,21 @@ def settings
104104
require 'jruby_art'
105105
require 'jruby_art/app'
106106
107-
Processing::App::SKETCH_PATH = __FILE__
107+
Processing::App::SKETCH_PATH = __FILE__.freeze
108108
109109
class %s < Processing::App
110-
def setup
111-
sketch_title '%s'
112-
end
110+
def setup
111+
sketch_title '%s'
112+
end
113113
114-
def draw
114+
def draw
115115
116-
end
116+
end
117117
118-
def settings
119-
size %s, %s, %s
120-
# smooth # here
121-
end
118+
def settings
119+
size %s, %s, %s
120+
# smooth # here
121+
end
122122
end
123123
124124
%s.new unless defined? $app
@@ -128,21 +128,20 @@ def settings
128128
# encoding: utf-8
129129
# frozen_string_literal: false
130130
class %s
131-
include Processing::Proxy
131+
include Processing::Proxy
132132
133133
end
134134
CODE
135135

136136
# creator wrapper module using StringExtra
137137
module Creator
138138
require_relative '../helpers/string_extra'
139-
using StringExtra
140139
# Write file to disk
141140
class SketchWriter
142141
attr_reader :file
143142

144143
def initialize(path)
145-
underscore = path.underscore
144+
underscore = StringExtra.new(path).underscore
146145
@file = "#{File.dirname(path)}/#{underscore}.rb"
147146
end
148147

@@ -157,7 +156,7 @@ def save(template)
157156
class Base
158157
ALL_DIGITS = /\A\d+\Z/
159158
def already_exist(path)
160-
underscore = path.underscore
159+
underscore = StringExtra.new(path).underscore
161160
new_file = "#{File.dirname(path)}/#{underscore}.rb"
162161
return if !FileTest.exist?(path) && !FileTest.exist?(new_file)
163162
puts 'That file already exists!'
@@ -197,7 +196,7 @@ def create!(path, args)
197196
already_exist(path)
198197
main_file = File.basename(path, '.rb') # allow uneeded extension input
199198
writer = SketchWriter.new(main_file)
200-
@title = main_file.titleize
199+
@title = StringExtra.new(main_file).titleize
201200
@width = args[0]
202201
@height = args[1]
203202
@mode = args[2].upcase unless args[2].nil?
@@ -222,9 +221,9 @@ def create!(path, args)
222221
main_file = File.basename(path, '.rb') # allow uneeded extension input
223222
# Check to make sure that the main file doesn't exist already
224223
already_exist(path)
225-
@name = main_file.camelize
224+
@name = StringExtra.new(main_file).camelize
226225
writer = SketchWriter.new(main_file)
227-
@title = main_file.titleize
226+
@title = StringExtra.new(main_file).titleize
228227
@width, @height = args[0], args[1]
229228
@mode = args[2].upcase unless args[2].nil?
230229
template = @mode.nil? ? class_template : class_template_mode
@@ -247,9 +246,9 @@ def create!(path, args)
247246
main_file = File.basename(path, '.rb') # allow uneeded extension input
248247
# Check to make sure that the main file doesn't exist already
249248
already_exist(path)
250-
@name = main_file.camelize
249+
@name = StringExtra.new(main_file).camelize
251250
writer = SketchWriter.new(main_file)
252-
@title = main_file.titleize
251+
@title = StringExtra.new(main_file).titleize
253252
@width, @height = args[0], args[1]
254253
@mode = args[2].upcase unless args[2].nil?
255254
template = @mode.nil? ? emacs_template : emacs_template_mode

lib/jruby_art/helpers/string_extra.rb

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# frozen_string_literal: false
2-
# String refinement for sketch creator
3-
module StringExtra
4-
refine String do
5-
def titleize
6-
underscore
7-
.humanize
8-
.gsub(/\b([a-z])/) { Regexp.last_match[1].capitalize }
9-
end
2+
require 'forwardable'
3+
# reverting to StringExtra class, from a module with String refinements
4+
class StringExtra
5+
extend Forwardable
6+
def_delegators(:@str, :upcase, :capitalize, :length, :downcase, :gsub)
7+
def initialize(str)
8+
@str = str
9+
end
1010

11-
def humanize
12-
gsub(/_id$/, '').tr('_', ' ').capitalize
13-
end
11+
def titleize
12+
underscore
13+
.gsub(/_id$/, '').tr('_', ' ').capitalize
14+
.gsub(/\b([a-z])/) { Regexp.last_match[1].capitalize }
15+
end
1416

15-
def camelize
16-
gsub(%r{/\/(.?)/}) { '::' + Regexp.last_match[1].upcase }
17-
.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
18-
end
17+
def humanize
18+
gsub(/_id$/, '').tr('_', ' ').capitalize
19+
end
20+
21+
def camelize
22+
gsub(%r{/\/(.?)/}) { '::' + Regexp.last_match[1].upcase }
23+
.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
24+
end
1925

20-
def underscore
21-
gsub(/::/, '/')
22-
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
23-
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
24-
.tr('-', '_')
25-
.downcase
26-
end
26+
def underscore
27+
gsub(/::/, '/')
28+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
29+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
30+
.tr('-', '_')
31+
.downcase
2732
end
2833
end

0 commit comments

Comments
 (0)