goofy 1.0.2

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.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/.gems +4 -0
  3. data/.gitignore +2 -0
  4. data/CHANGELOG +47 -0
  5. data/CONTRIBUTING +19 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +23 -0
  8. data/README.md +67 -0
  9. data/app/.rspec +2 -0
  10. data/app/Gemfile +13 -0
  11. data/app/app/controllers/application_controller.rb +3 -0
  12. data/app/app/services/.keep +0 -0
  13. data/app/config.ru +4 -0
  14. data/app/config/environment.rb +9 -0
  15. data/app/config/initializers/.keep +0 -0
  16. data/app/config/routes.rb +7 -0
  17. data/app/config/settings.rb +0 -0
  18. data/app/spec/helpers.rb +2 -0
  19. data/app/spec/helpers/goofy.rb +11 -0
  20. data/app/spec/spec_helper.rb +107 -0
  21. data/benchmark/measure.rb +35 -0
  22. data/bin/check_its_goofy.rb +15 -0
  23. data/bin/goofy +61 -0
  24. data/bin/goofy_generator.rb +357 -0
  25. data/bin/goofy_instance_creator.rb +40 -0
  26. data/examples/config.ru +18 -0
  27. data/examples/measure.rb +17 -0
  28. data/examples/rack-response.ru +21 -0
  29. data/examples/views/home.mote +7 -0
  30. data/examples/views/layout.mote +11 -0
  31. data/goofy.gemspec +26 -0
  32. data/lib/goofy.rb +405 -0
  33. data/lib/goofy/capybara.rb +13 -0
  34. data/lib/goofy/controller.rb +14 -0
  35. data/lib/goofy/controller/base.rb +21 -0
  36. data/lib/goofy/controller/callbacks.rb +19 -0
  37. data/lib/goofy/render.rb +63 -0
  38. data/lib/goofy/router.rb +9 -0
  39. data/lib/goofy/safe.rb +23 -0
  40. data/lib/goofy/safe/csrf.rb +47 -0
  41. data/lib/goofy/safe/secure_headers.rb +40 -0
  42. data/lib/goofy/test.rb +11 -0
  43. data/makefile +4 -0
  44. data/test/accept.rb +32 -0
  45. data/test/captures.rb +162 -0
  46. data/test/composition.rb +69 -0
  47. data/test/controller.rb +29 -0
  48. data/test/cookie.rb +34 -0
  49. data/test/csrf.rb +139 -0
  50. data/test/extension.rb +21 -0
  51. data/test/helper.rb +11 -0
  52. data/test/host.rb +29 -0
  53. data/test/integration.rb +114 -0
  54. data/test/match.rb +86 -0
  55. data/test/middleware.rb +46 -0
  56. data/test/number.rb +36 -0
  57. data/test/on.rb +157 -0
  58. data/test/param.rb +66 -0
  59. data/test/path.rb +86 -0
  60. data/test/plugin.rb +68 -0
  61. data/test/rack.rb +22 -0
  62. data/test/redirect.rb +21 -0
  63. data/test/render.rb +128 -0
  64. data/test/root.rb +83 -0
  65. data/test/run.rb +23 -0
  66. data/test/safe.rb +74 -0
  67. data/test/segment.rb +45 -0
  68. data/test/session.rb +21 -0
  69. data/test/settings.rb +52 -0
  70. data/test/views/about.erb +1 -0
  71. data/test/views/about.str +1 -0
  72. data/test/views/content-yield.erb +1 -0
  73. data/test/views/custom/abs_path.mote +1 -0
  74. data/test/views/frag.mote +1 -0
  75. data/test/views/home.erb +2 -0
  76. data/test/views/home.mote +1 -0
  77. data/test/views/home.str +2 -0
  78. data/test/views/layout-alternative.erb +2 -0
  79. data/test/views/layout-yield.erb +3 -0
  80. data/test/views/layout.erb +2 -0
  81. data/test/views/layout.mote +2 -0
  82. data/test/views/layout.str +2 -0
  83. data/test/views/test.erb +1 -0
  84. data/test/with.rb +42 -0
  85. metadata +271 -0
@@ -0,0 +1,357 @@
1
+ module GoofyGenerator
2
+
3
+ class Base
4
+
5
+ def initialize(type,name)
6
+ begin
7
+ @type = type || (raise ArgumentError.new("You must specify type for generator, for example: goofy g resource"))
8
+ @name = name.split(/\//).delete_if { |x| x == "" } if name
9
+ raise ArgumentError.new("You must specify name for generator, for example: goofy g resource home") unless name
10
+ rescue ArgumentError => e
11
+ puts e
12
+ abort
13
+ end
14
+ end
15
+
16
+ def execute!
17
+ type_mapper
18
+ end
19
+
20
+ def type_mapper
21
+ begin
22
+ case @type
23
+ when "controller"
24
+ GoofyGenerator::Controller.new(@name).execute!
25
+ when "service"
26
+ GoofyGenerator::Service.new(@name).execute!
27
+ when "resource"
28
+ GoofyGenerator::Controller.new(@name).execute!
29
+ GoofyGenerator::Service.new(@name).execute!
30
+ when "initializer"
31
+ GoofyGenerator::Initializer.new(@name).execute!
32
+ else
33
+ raise ArgumentError.new("Invalid generator, Valid generators resource / controller / service")
34
+ end
35
+ rescue ArgumentError => e
36
+ puts e
37
+ abort
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ class Initializer
44
+
45
+ def initialize(name)
46
+ @name = name
47
+ @init_path = 'config/initializers'
48
+ @config_path = 'config/'
49
+ end
50
+
51
+ def execute!
52
+ FileUtils.mkdir_p(@init_path)
53
+ create_files
54
+ end
55
+
56
+ def create_files
57
+ create_init(
58
+ file_name: @name.first,
59
+ path: @init_path
60
+ )
61
+ create_config(
62
+ file_name: @name.first,
63
+ path: @config_path
64
+ )
65
+ end
66
+
67
+ def ask_to_rewrite(file_path, &block)
68
+ if File.exist?(file_path)
69
+ puts "Already exists: #{file_path}"
70
+ else
71
+ block.call
72
+ puts "File created: #{file_path}"
73
+ end
74
+ end
75
+
76
+ def create_init(hash = {})
77
+ file_name = hash[:file_name] + '.rb'
78
+ file_path = if hash[:path][-1] == '/'
79
+ (hash[:path] + file_name)
80
+ else
81
+ (hash[:path] + '/' + file_name)
82
+ end
83
+ ask_to_rewrite(file_path) do
84
+ file = File.open(file_path, "a+")
85
+ file.puts("# require 'yaml'")
86
+ file.puts("# YAML::load_file(File.join(ENV[\"PWD\"], \"/config\",\"/#{hash[:file_name]}.yml\"))")
87
+ file.close()
88
+ end
89
+ end
90
+
91
+ def create_config(hash = {})
92
+ file_name = hash[:file_name] + '.yml'
93
+ file_path = if hash[:path][-1] == '/'
94
+ (hash[:path] + file_name)
95
+ else
96
+ (hash[:path] + '/' + file_name)
97
+ end
98
+ ask_to_rewrite(file_path) do
99
+ file = File.open(file_path, "a+")
100
+ file.puts("# automatically generated config file for #{hash[:file_name]} initializer")
101
+ file.close()
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ class Controller
108
+
109
+ def initialize(name)
110
+ @name = name
111
+ @path = 'app/controllers/'
112
+ end
113
+
114
+ def execute!
115
+ FileUtils.mkdir_p(@path + @name[0..-2].join('/')) if @name.size > 1
116
+ create_files
117
+ end
118
+
119
+ def create_files
120
+ @name.each_with_index do |item, index|
121
+ if index == 0
122
+ create_class(
123
+ file_name: item,
124
+ path: @path,
125
+ class_name: gen_class_name([item]),
126
+ super_class_name: 'ApplicationController'
127
+ )
128
+ else
129
+ create_class_with_module(
130
+ file_name: item,
131
+ path: @path + @name[0,index].join('/'),
132
+ class_name: gen_class_name(@name[0, index + 1]),
133
+ file_header: generate_module_name_header(@name[0,index]),
134
+ file_footer: generate_module_name_footer(@name[0,index]),
135
+ super_class_name: gen_class_name(@name[0,(index)]),
136
+ name_index: @name[0,index]
137
+ )
138
+ end
139
+ end
140
+ end
141
+
142
+ def generate_module_name_header(list)
143
+ str = ""
144
+ list.each_with_index do |item, index|
145
+ if index == 0
146
+ str += "module #{item.capitalize}\n"
147
+ else
148
+ str += (" " * (2 * index)) + "module #{item.capitalize}\n"
149
+ end
150
+ end
151
+ str
152
+ end
153
+
154
+ def generate_module_name_footer(list)
155
+ arr = (0..list.size - 1).to_a.reverse
156
+ str = ""
157
+ arr.each do |item|
158
+ if item == arr.last
159
+ str += "end\n"
160
+ else
161
+ str += (" " * (2 * item)) + "end\n"
162
+ end
163
+ end
164
+ str
165
+ end
166
+
167
+ def ask_to_rewrite(file_path, &block)
168
+ if File.exist?(file_path)
169
+ puts "Already exists: #{file_path}"
170
+ else
171
+ block.call
172
+ puts "File created: #{file_path}"
173
+ end
174
+ end
175
+
176
+ def create_class(hash = {})
177
+ file_name = hash[:file_name] + '_controller.rb'
178
+ file_path = if hash[:path][-1] == '/'
179
+ (hash[:path] + file_name)
180
+ else
181
+ (hash[:path] + '/' + file_name)
182
+ end
183
+ ask_to_rewrite(file_path) do
184
+ file = File.open(file_path, "a+")
185
+ file.puts("class #{hash[:class_name]} < #{hash[:super_class_name]}")
186
+ file.puts(" # def response")
187
+ file.puts(" # end")
188
+ file.puts("end")
189
+ file.close()
190
+ end
191
+ end
192
+
193
+ def create_class_with_module(hash = {})
194
+ file_name = hash[:file_name] + '_controller.rb'
195
+ file_path = if hash[:path][-1] == '/'
196
+ (hash[:path] + file_name)
197
+ else
198
+ (hash[:path] + '/' + file_name)
199
+ end
200
+ ask_to_rewrite(file_path) do
201
+ file = File.open(file_path, "a+")
202
+ file.puts(hash[:file_header]) if hash[:file_header]
203
+ file.puts(correct_indent("class #{hash[:class_name]} < #{hash[:super_class_name]}", hash[:name_index]))
204
+ file.puts(correct_indent(" # def response", hash[:name_index]))
205
+ file.puts(correct_indent(" # end", hash[:name_index]))
206
+ file.puts(correct_indent("end", hash[:name_index]))
207
+ file.puts(hash[:file_footer]) if hash[:file_footer]
208
+ file.close()
209
+ end
210
+ end
211
+
212
+ def correct_indent(string, name)
213
+ size = name.size
214
+ (" " * (2 * size)) + string
215
+ end
216
+
217
+ def gen_class_name(name)
218
+ name.last.capitalize + 'Controller'
219
+ end
220
+
221
+ def gen_module_name(name)
222
+ count = name.count - 1
223
+ name.map.with_index do |item, index|
224
+ if index == count
225
+ item.capitalize
226
+ else
227
+ item.capitalize + '::'
228
+ end
229
+ end
230
+ end
231
+
232
+ end
233
+
234
+ class Service
235
+
236
+ def initialize(name)
237
+ @name = name
238
+ @path = 'app/services/'
239
+ end
240
+
241
+ def execute!
242
+ FileUtils.mkdir_p(@path + @name[0..-2].join('/')) if @name.count > 1
243
+ create_files
244
+ end
245
+
246
+ def create_files
247
+ @name.each_with_index do |item, index|
248
+ if @name.size == 1
249
+ create_class(
250
+ file_name: item,
251
+ path: @path,
252
+ class_name: gen_class_name([item]),
253
+ )
254
+ elsif index == (@name.size - 1)
255
+ create_class_with_module(
256
+ file_name: item,
257
+ path: @path + @name[0,index].join('/'),
258
+ class_name: gen_class_name(@name[0, index + 1]),
259
+ file_header: generate_module_name_header(@name[0,index]),
260
+ file_footer: generate_module_name_footer(@name[0,index]),
261
+ name_index: @name[0,index]
262
+ )
263
+ end
264
+ end
265
+ end
266
+
267
+ def generate_module_name_header(list)
268
+ str = ""
269
+ list.each_with_index do |item, index|
270
+ if index == 0
271
+ str += "module #{item.capitalize}\n"
272
+ else
273
+ str += (" " * (2 * index)) + "module #{item.capitalize}\n"
274
+ end
275
+ end
276
+ str
277
+ end
278
+
279
+ def generate_module_name_footer(list)
280
+ arr = (0..list.size - 1).to_a.reverse
281
+ str = ""
282
+ arr.each do |item|
283
+ if item == arr.last
284
+ str += "end\n"
285
+ else
286
+ str += (" " * (2 * item)) + "end\n"
287
+ end
288
+ end
289
+ str
290
+ end
291
+
292
+ def ask_to_rewrite(file_path, &block)
293
+ if File.exist?(file_path)
294
+ puts "Already exists: #{file_path}"
295
+ else
296
+ block.call
297
+ puts "File created: #{file_path}"
298
+ end
299
+ end
300
+
301
+ def create_class(hash = {})
302
+ file_name = hash[:file_name] + '_service.rb'
303
+ file_path = if hash[:path][-1] == '/'
304
+ (hash[:path] + file_name)
305
+ else
306
+ (hash[:path] + '/' + file_name)
307
+ end
308
+ ask_to_rewrite(file_path) do
309
+ file = File.open(file_path, "a+")
310
+ file.puts("class #{hash[:class_name]}")
311
+ file.puts(" include Wisper::Publisher")
312
+ file.puts("end")
313
+ file.close()
314
+ end
315
+ end
316
+
317
+ def create_class_with_module(hash = {})
318
+ file_name = hash[:file_name] + '_service.rb'
319
+ file_path = if hash[:path][-1] == '/'
320
+ (hash[:path] + file_name)
321
+ else
322
+ (hash[:path] + '/' + file_name)
323
+ end
324
+ ask_to_rewrite(file_path) do
325
+ file = File.open(file_path, "a+")
326
+ file.puts(hash[:file_header]) if hash[:file_header]
327
+ file.puts(correct_indent("class #{hash[:class_name]}", hash[:name_index]))
328
+ file.puts(correct_indent(" include Wisper::Publisher", hash[:name_index]))
329
+ file.puts(correct_indent("end", hash[:name_index]))
330
+ file.puts(hash[:file_footer]) if hash[:file_footer]
331
+ file.close()
332
+ end
333
+ end
334
+
335
+ def correct_indent(string, name)
336
+ size = name.size
337
+ (" " * (2 * size)) + string
338
+ end
339
+
340
+ def gen_class_name(name)
341
+ name.last.capitalize + 'Service'
342
+ end
343
+
344
+ def gen_module_name(name)
345
+ count = name.count - 1
346
+ name.map.with_index do |item, index|
347
+ if index == count
348
+ item.capitalize
349
+ else
350
+ item.capitalize + '::'
351
+ end
352
+ end
353
+ end
354
+
355
+ end
356
+
357
+ end
@@ -0,0 +1,40 @@
1
+ class GoofyInstanceCreator
2
+
3
+ def initialize(app_name = nil,app_dir,current_dir)
4
+ begin
5
+ @app_name = app_name || (raise ArgumentError.new("No value provided for required arguments 'app_path'"))
6
+ @app_dir = app_dir
7
+ @current_dir = current_dir
8
+ @target_dir = (current_dir + '/' + app_name)
9
+ rewrite() if File.directory?(@target_dir)
10
+ rescue ArgumentError => e
11
+ puts e
12
+ abort
13
+ end
14
+ end
15
+
16
+ def execute!(mkdir = true)
17
+ FileUtils.mkdir(@app_name) if mkdir
18
+ FileUtils.copy_entry @app_dir, @target_dir
19
+
20
+ Bundler.with_clean_env do
21
+ Dir.chdir("#{@target_dir}") do
22
+ system "bundle"
23
+ end
24
+ end
25
+ end
26
+
27
+ def rewrite
28
+ puts "Do you want to rewrite '#{@app_name}'?(yes/no)"
29
+ rs = STDIN.gets.chomp
30
+ if rs == "yes"
31
+ execute!(false)
32
+ abort
33
+ elsif rs == "no"
34
+ abort
35
+ else
36
+ rewrite()
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,18 @@
1
+ require "../lib/Goofy"
2
+ require "goofy/contrib"
3
+
4
+ Goofy.plugin Goofy::Mote
5
+
6
+ ITEMS = ("A".."Z").to_a
7
+
8
+ Goofy.define do
9
+ def mote_vars(content)
10
+ { content: content }
11
+ end
12
+
13
+ on default do
14
+ res.write view("home", list: ITEMS)
15
+ end
16
+ end
17
+
18
+ run Goofy
@@ -0,0 +1,17 @@
1
+ require "benchmark"
2
+ require "rack"
3
+
4
+ Benchmark.bmbm do |x|
5
+
6
+ x.report "Rack::HeaderHash" do
7
+ 1000.times do
8
+ Rack::Utils::HeaderHash.new("Content-Type" => "text/html")
9
+ end
10
+ end
11
+
12
+ x.report "Hash" do
13
+ 1000.times do
14
+ { "Content-Type" => "text/html" }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ require "../lib/Goofy"
2
+ require "goofy/contrib"
3
+
4
+ Goofy.plugin Goofy::Mote
5
+
6
+ ITEMS = ("A".."Z").to_a
7
+
8
+ Goofy.send :remove_const, :Response
9
+ Goofy::Response = Rack::Response
10
+
11
+ Goofy.define do
12
+ def mote_vars(content)
13
+ { content: content }
14
+ end
15
+
16
+ on default do
17
+ res.write view("home", list: ITEMS)
18
+ end
19
+ end
20
+
21
+ run Goofy