wrap 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +390 -0
- data/lib/wrap.rb +216 -0
- data/test/testing.rb +195 -0
- data/test/wrap_test.rb +71 -0
- data/wrap.gemspec +37 -0
- metadata +60 -0
data/Rakefile
ADDED
@@ -0,0 +1,390 @@
|
|
1
|
+
This.rubyforge_project = 'codeforpeople'
|
2
|
+
This.author = "Ara T. Howard"
|
3
|
+
This.email = "ara.t.howard@gmail.com"
|
4
|
+
This.homepage = "https://github.com/ahoward/#{ This.lib }"
|
5
|
+
|
6
|
+
|
7
|
+
task :default do
|
8
|
+
puts((Rake::Task.tasks.map{|task| task.name.gsub(/::/,':')} - ['default']).sort)
|
9
|
+
end
|
10
|
+
|
11
|
+
task :test do
|
12
|
+
run_tests!
|
13
|
+
end
|
14
|
+
|
15
|
+
namespace :test do
|
16
|
+
task(:unit){ run_tests!(:unit) }
|
17
|
+
task(:functional){ run_tests!(:functional) }
|
18
|
+
task(:integration){ run_tests!(:integration) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def run_tests!(which = nil)
|
22
|
+
which ||= '**'
|
23
|
+
test_dir = File.join(This.dir, "test")
|
24
|
+
test_glob ||= File.join(test_dir, "#{ which }/**_test.rb")
|
25
|
+
test_rbs = Dir.glob(test_glob).sort
|
26
|
+
|
27
|
+
div = ('=' * 119)
|
28
|
+
line = ('-' * 119)
|
29
|
+
|
30
|
+
test_rbs.each_with_index do |test_rb, index|
|
31
|
+
testno = index + 1
|
32
|
+
command = "#{ This.ruby } -I ./lib -I ./test/lib #{ test_rb }"
|
33
|
+
|
34
|
+
puts
|
35
|
+
say(div, :color => :cyan, :bold => true)
|
36
|
+
say("@#{ testno } => ", :bold => true, :method => :print)
|
37
|
+
say(command, :color => :cyan, :bold => true)
|
38
|
+
say(line, :color => :cyan, :bold => true)
|
39
|
+
|
40
|
+
system(command)
|
41
|
+
|
42
|
+
say(line, :color => :cyan, :bold => true)
|
43
|
+
|
44
|
+
status = $?.exitstatus
|
45
|
+
|
46
|
+
if status.zero?
|
47
|
+
say("@#{ testno } <= ", :bold => true, :color => :white, :method => :print)
|
48
|
+
say("SUCCESS", :color => :green, :bold => true)
|
49
|
+
else
|
50
|
+
say("@#{ testno } <= ", :bold => true, :color => :white, :method => :print)
|
51
|
+
say("FAILURE", :color => :red, :bold => true)
|
52
|
+
end
|
53
|
+
say(line, :color => :cyan, :bold => true)
|
54
|
+
|
55
|
+
exit(status) unless status.zero?
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
task :gemspec do
|
61
|
+
ignore_extensions = ['git', 'svn', 'tmp', /sw./, 'bak', 'gem']
|
62
|
+
ignore_directories = ['pkg']
|
63
|
+
ignore_files = ['test/log', 'a.rb'] + Dir['db/*'] + %w'db'
|
64
|
+
|
65
|
+
shiteless =
|
66
|
+
lambda do |list|
|
67
|
+
list.delete_if do |entry|
|
68
|
+
next unless test(?e, entry)
|
69
|
+
extension = File.basename(entry).split(%r/[.]/).last
|
70
|
+
ignore_extensions.any?{|ext| ext === extension}
|
71
|
+
end
|
72
|
+
list.delete_if do |entry|
|
73
|
+
next unless test(?d, entry)
|
74
|
+
dirname = File.expand_path(entry)
|
75
|
+
ignore_directories.any?{|dir| File.expand_path(dir) == dirname}
|
76
|
+
end
|
77
|
+
list.delete_if do |entry|
|
78
|
+
next unless test(?f, entry)
|
79
|
+
filename = File.expand_path(entry)
|
80
|
+
ignore_files.any?{|file| File.expand_path(file) == filename}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
lib = This.lib
|
85
|
+
object = This.object
|
86
|
+
version = This.version
|
87
|
+
files = shiteless[Dir::glob("**/**")]
|
88
|
+
executables = shiteless[Dir::glob("bin/*")].map{|exe| File.basename(exe)}
|
89
|
+
#has_rdoc = true #File.exist?('doc')
|
90
|
+
test_files = test(?e, "test/#{ lib }.rb") ? "test/#{ lib }.rb" : nil
|
91
|
+
summary = object.respond_to?(:summary) ? object.summary : "summary: #{ lib } kicks the ass"
|
92
|
+
description = object.respond_to?(:description) ? object.description : "description: #{ lib } kicks the ass"
|
93
|
+
|
94
|
+
if This.extensions.nil?
|
95
|
+
This.extensions = []
|
96
|
+
extensions = This.extensions
|
97
|
+
%w( Makefile configure extconf.rb ).each do |ext|
|
98
|
+
extensions << ext if File.exists?(ext)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
extensions = [extensions].flatten.compact
|
102
|
+
|
103
|
+
# TODO
|
104
|
+
if This.dependencies.nil?
|
105
|
+
dependencies = []
|
106
|
+
else
|
107
|
+
case This.dependencies
|
108
|
+
when Hash
|
109
|
+
dependencies = This.dependencies.values
|
110
|
+
when Array
|
111
|
+
dependencies = This.dependencies
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
template =
|
116
|
+
if test(?e, 'gemspec.erb')
|
117
|
+
Template{ IO.read('gemspec.erb') }
|
118
|
+
else
|
119
|
+
Template {
|
120
|
+
<<-__
|
121
|
+
## <%= lib %>.gemspec
|
122
|
+
#
|
123
|
+
|
124
|
+
Gem::Specification::new do |spec|
|
125
|
+
spec.name = <%= lib.inspect %>
|
126
|
+
spec.version = <%= version.inspect %>
|
127
|
+
spec.platform = Gem::Platform::RUBY
|
128
|
+
spec.summary = <%= lib.inspect %>
|
129
|
+
spec.description = <%= description.inspect %>
|
130
|
+
|
131
|
+
spec.files =\n<%= files.sort.pretty_inspect %>
|
132
|
+
spec.executables = <%= executables.inspect %>
|
133
|
+
|
134
|
+
spec.require_path = "lib"
|
135
|
+
|
136
|
+
spec.test_files = <%= test_files.inspect %>
|
137
|
+
|
138
|
+
<% dependencies.each do |lib_version| %>
|
139
|
+
spec.add_dependency(*<%= Array(lib_version).flatten.inspect %>)
|
140
|
+
<% end %>
|
141
|
+
|
142
|
+
spec.extensions.push(*<%= extensions.inspect %>)
|
143
|
+
|
144
|
+
spec.rubyforge_project = <%= This.rubyforge_project.inspect %>
|
145
|
+
spec.author = <%= This.author.inspect %>
|
146
|
+
spec.email = <%= This.email.inspect %>
|
147
|
+
spec.homepage = <%= This.homepage.inspect %>
|
148
|
+
end
|
149
|
+
__
|
150
|
+
}
|
151
|
+
end
|
152
|
+
|
153
|
+
Fu.mkdir_p(This.pkgdir)
|
154
|
+
gemspec = "#{ lib }.gemspec"
|
155
|
+
open(gemspec, "w"){|fd| fd.puts(template)}
|
156
|
+
This.gemspec = gemspec
|
157
|
+
end
|
158
|
+
|
159
|
+
task :gem => [:clean, :gemspec] do
|
160
|
+
Fu.mkdir_p(This.pkgdir)
|
161
|
+
before = Dir['*.gem']
|
162
|
+
cmd = "gem build #{ This.gemspec }"
|
163
|
+
`#{ cmd }`
|
164
|
+
after = Dir['*.gem']
|
165
|
+
gem = ((after - before).first || after.first) or abort('no gem!')
|
166
|
+
Fu.mv(gem, This.pkgdir)
|
167
|
+
This.gem = File.join(This.pkgdir, File.basename(gem))
|
168
|
+
end
|
169
|
+
|
170
|
+
task :readme do
|
171
|
+
samples = ''
|
172
|
+
prompt = '~ > '
|
173
|
+
lib = This.lib
|
174
|
+
version = This.version
|
175
|
+
|
176
|
+
Dir['sample*/*'].sort.each do |sample|
|
177
|
+
samples << "\n" << " <========< #{ sample } >========>" << "\n\n"
|
178
|
+
|
179
|
+
cmd = "cat #{ sample }"
|
180
|
+
samples << Util.indent(prompt + cmd, 2) << "\n\n"
|
181
|
+
samples << Util.indent(`#{ cmd }`, 4) << "\n"
|
182
|
+
|
183
|
+
cmd = "ruby #{ sample }"
|
184
|
+
samples << Util.indent(prompt + cmd, 2) << "\n\n"
|
185
|
+
|
186
|
+
cmd = "ruby -e'STDOUT.sync=true; exec %(ruby -I ./lib #{ sample })'"
|
187
|
+
samples << Util.indent(`#{ cmd } 2>&1`, 4) << "\n"
|
188
|
+
end
|
189
|
+
|
190
|
+
template =
|
191
|
+
if test(?e, 'readme.erb')
|
192
|
+
Template{ IO.read('readme.erb') }
|
193
|
+
else
|
194
|
+
Template {
|
195
|
+
<<-__
|
196
|
+
NAME
|
197
|
+
#{ lib }
|
198
|
+
|
199
|
+
DESCRIPTION
|
200
|
+
|
201
|
+
INSTALL
|
202
|
+
gem install #{ lib }
|
203
|
+
|
204
|
+
SAMPLES
|
205
|
+
#{ samples }
|
206
|
+
__
|
207
|
+
}
|
208
|
+
end
|
209
|
+
|
210
|
+
open("README", "w"){|fd| fd.puts template}
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
task :clean do
|
215
|
+
Dir[File.join(This.pkgdir, '**/**')].each{|entry| Fu.rm_rf(entry)}
|
216
|
+
end
|
217
|
+
|
218
|
+
|
219
|
+
task :release => [:clean, :gemspec, :gem] do
|
220
|
+
gems = Dir[File.join(This.pkgdir, '*.gem')].flatten
|
221
|
+
raise "which one? : #{ gems.inspect }" if gems.size > 1
|
222
|
+
raise "no gems?" if gems.size < 1
|
223
|
+
|
224
|
+
cmd = "gem push #{ This.gem }"
|
225
|
+
puts cmd
|
226
|
+
puts
|
227
|
+
system(cmd)
|
228
|
+
abort("cmd(#{ cmd }) failed with (#{ $?.inspect })") unless $?.exitstatus.zero?
|
229
|
+
|
230
|
+
cmd = "rubyforge login && rubyforge add_release #{ This.rubyforge_project } #{ This.lib } #{ This.version } #{ This.gem }"
|
231
|
+
puts cmd
|
232
|
+
puts
|
233
|
+
system(cmd)
|
234
|
+
abort("cmd(#{ cmd }) failed with (#{ $?.inspect })") unless $?.exitstatus.zero?
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
BEGIN {
|
242
|
+
# support for this rakefile
|
243
|
+
#
|
244
|
+
$VERBOSE = nil
|
245
|
+
|
246
|
+
require 'ostruct'
|
247
|
+
require 'erb'
|
248
|
+
require 'fileutils'
|
249
|
+
require 'rbconfig'
|
250
|
+
require 'pp'
|
251
|
+
|
252
|
+
# fu shortcut
|
253
|
+
#
|
254
|
+
Fu = FileUtils
|
255
|
+
|
256
|
+
# cache a bunch of stuff about this rakefile/environment
|
257
|
+
#
|
258
|
+
This = OpenStruct.new
|
259
|
+
|
260
|
+
This.file = File.expand_path(__FILE__)
|
261
|
+
This.dir = File.dirname(This.file)
|
262
|
+
This.pkgdir = File.join(This.dir, 'pkg')
|
263
|
+
|
264
|
+
# grok lib
|
265
|
+
#
|
266
|
+
lib = ENV['LIB']
|
267
|
+
unless lib
|
268
|
+
lib = File.basename(Dir.pwd).sub(/[-].*$/, '')
|
269
|
+
end
|
270
|
+
This.lib = lib
|
271
|
+
|
272
|
+
# grok version
|
273
|
+
#
|
274
|
+
version = ENV['VERSION']
|
275
|
+
unless version
|
276
|
+
require "./lib/#{ This.lib }"
|
277
|
+
This.name = lib.capitalize
|
278
|
+
This.object = eval(This.name)
|
279
|
+
version = This.object.send(:version)
|
280
|
+
end
|
281
|
+
This.version = version
|
282
|
+
|
283
|
+
# see if dependencies are export by the module
|
284
|
+
#
|
285
|
+
if This.object.respond_to?(:dependencies)
|
286
|
+
This.dependencies = This.object.dependencies
|
287
|
+
end
|
288
|
+
|
289
|
+
# we need to know the name of the lib an it's version
|
290
|
+
#
|
291
|
+
abort('no lib') unless This.lib
|
292
|
+
abort('no version') unless This.version
|
293
|
+
|
294
|
+
# discover full path to this ruby executable
|
295
|
+
#
|
296
|
+
c = RbConfig::CONFIG
|
297
|
+
bindir = c["bindir"] || c['BINDIR']
|
298
|
+
ruby_install_name = c['ruby_install_name'] || c['RUBY_INSTALL_NAME'] || 'ruby'
|
299
|
+
ruby_ext = c['EXEEXT'] || ''
|
300
|
+
ruby = File.join(bindir, (ruby_install_name + ruby_ext))
|
301
|
+
This.ruby = ruby
|
302
|
+
|
303
|
+
# some utils
|
304
|
+
#
|
305
|
+
module Util
|
306
|
+
def indent(s, n = 2)
|
307
|
+
s = unindent(s)
|
308
|
+
ws = ' ' * n
|
309
|
+
s.gsub(%r/^/, ws)
|
310
|
+
end
|
311
|
+
|
312
|
+
def unindent(s)
|
313
|
+
indent = nil
|
314
|
+
s.each_line do |line|
|
315
|
+
next if line =~ %r/^\s*$/
|
316
|
+
indent = line[%r/^\s*/] and break
|
317
|
+
end
|
318
|
+
indent ? s.gsub(%r/^#{ indent }/, "") : s
|
319
|
+
end
|
320
|
+
extend self
|
321
|
+
end
|
322
|
+
|
323
|
+
# template support
|
324
|
+
#
|
325
|
+
class Template
|
326
|
+
def initialize(&block)
|
327
|
+
@block = block
|
328
|
+
@template = block.call.to_s
|
329
|
+
end
|
330
|
+
def expand(b=nil)
|
331
|
+
ERB.new(Util.unindent(@template)).result((b||@block).binding)
|
332
|
+
end
|
333
|
+
alias_method 'to_s', 'expand'
|
334
|
+
end
|
335
|
+
def Template(*args, &block) Template.new(*args, &block) end
|
336
|
+
|
337
|
+
# colored console output support
|
338
|
+
#
|
339
|
+
This.ansi = {
|
340
|
+
:clear => "\e[0m",
|
341
|
+
:reset => "\e[0m",
|
342
|
+
:erase_line => "\e[K",
|
343
|
+
:erase_char => "\e[P",
|
344
|
+
:bold => "\e[1m",
|
345
|
+
:dark => "\e[2m",
|
346
|
+
:underline => "\e[4m",
|
347
|
+
:underscore => "\e[4m",
|
348
|
+
:blink => "\e[5m",
|
349
|
+
:reverse => "\e[7m",
|
350
|
+
:concealed => "\e[8m",
|
351
|
+
:black => "\e[30m",
|
352
|
+
:red => "\e[31m",
|
353
|
+
:green => "\e[32m",
|
354
|
+
:yellow => "\e[33m",
|
355
|
+
:blue => "\e[34m",
|
356
|
+
:magenta => "\e[35m",
|
357
|
+
:cyan => "\e[36m",
|
358
|
+
:white => "\e[37m",
|
359
|
+
:on_black => "\e[40m",
|
360
|
+
:on_red => "\e[41m",
|
361
|
+
:on_green => "\e[42m",
|
362
|
+
:on_yellow => "\e[43m",
|
363
|
+
:on_blue => "\e[44m",
|
364
|
+
:on_magenta => "\e[45m",
|
365
|
+
:on_cyan => "\e[46m",
|
366
|
+
:on_white => "\e[47m"
|
367
|
+
}
|
368
|
+
def say(phrase, *args)
|
369
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
370
|
+
options[:color] = args.shift.to_s.to_sym unless args.empty?
|
371
|
+
keys = options.keys
|
372
|
+
keys.each{|key| options[key.to_s.to_sym] = options.delete(key)}
|
373
|
+
|
374
|
+
color = options[:color]
|
375
|
+
bold = options.has_key?(:bold)
|
376
|
+
|
377
|
+
parts = [phrase]
|
378
|
+
parts.unshift(This.ansi[color]) if color
|
379
|
+
parts.unshift(This.ansi[:bold]) if bold
|
380
|
+
parts.push(This.ansi[:clear]) if parts.size > 1
|
381
|
+
|
382
|
+
method = options[:method] || :puts
|
383
|
+
|
384
|
+
Kernel.send(method, parts.join)
|
385
|
+
end
|
386
|
+
|
387
|
+
# always run out of the project dir
|
388
|
+
#
|
389
|
+
Dir.chdir(This.dir)
|
390
|
+
}
|
data/lib/wrap.rb
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
##
|
2
|
+
#
|
3
|
+
module Wrap; end
|
4
|
+
|
5
|
+
##
|
6
|
+
#
|
7
|
+
class << Wrap
|
8
|
+
Version = '0.4.2' unless defined?(Version)
|
9
|
+
|
10
|
+
def version
|
11
|
+
Version
|
12
|
+
end
|
13
|
+
|
14
|
+
def dependencies
|
15
|
+
{
|
16
|
+
'map' => [ 'map' , ' >= 4.7.1' ]
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def libdir(*args, &block)
|
21
|
+
@libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
|
22
|
+
args.empty? ? @libdir : File.join(@libdir, *args)
|
23
|
+
ensure
|
24
|
+
if block
|
25
|
+
begin
|
26
|
+
$LOAD_PATH.unshift(@libdir)
|
27
|
+
block.call()
|
28
|
+
ensure
|
29
|
+
$LOAD_PATH.shift()
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def load(*libs)
|
35
|
+
libs = libs.join(' ').scan(/[^\s+]+/)
|
36
|
+
Wrap.libdir{ libs.each{|lib| Kernel.load(lib) } }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
#
|
42
|
+
begin
|
43
|
+
require 'rubygems'
|
44
|
+
rescue LoadError
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
#
|
50
|
+
require 'map'
|
51
|
+
|
52
|
+
##
|
53
|
+
#
|
54
|
+
module Wrap
|
55
|
+
def Wrap.included(other)
|
56
|
+
super
|
57
|
+
ensure
|
58
|
+
other.send(:instance_eval, &ClassMethods)
|
59
|
+
other.send(:class_eval, &InstanceMethods)
|
60
|
+
end
|
61
|
+
|
62
|
+
def Wrap.code_for(name)
|
63
|
+
<<-__
|
64
|
+
def #{ name }(*args, &block)
|
65
|
+
if running_callbacks?(#{ name.inspect })
|
66
|
+
return wrapped_#{ name }(*args, &block)
|
67
|
+
end
|
68
|
+
|
69
|
+
running_callbacks(#{ name.inspect }) do
|
70
|
+
begin
|
71
|
+
run_callbacks(:before, #{ name.inspect })
|
72
|
+
wrapped_#{ name }(*args, &block)
|
73
|
+
ensure
|
74
|
+
run_callbacks(:after, #{ name.inspect }) unless $!
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
__
|
79
|
+
end
|
80
|
+
|
81
|
+
ClassMethods = proc do
|
82
|
+
def wrap(name, *args, &block)
|
83
|
+
define_method(name){} unless method_defined?(name)
|
84
|
+
|
85
|
+
wrap!(name)
|
86
|
+
end
|
87
|
+
|
88
|
+
def method_added(name)
|
89
|
+
return super if wrapping?
|
90
|
+
|
91
|
+
begin
|
92
|
+
super
|
93
|
+
ensure
|
94
|
+
rewrap!(name) if wrapped?(name)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def wrapped?(name)
|
99
|
+
method_defined?("wrapped_#{ name }")
|
100
|
+
end
|
101
|
+
|
102
|
+
def wrap!(name)
|
103
|
+
wrapping! name do
|
104
|
+
name = name.to_s
|
105
|
+
wrapped_name = "wrapped_#{ name }"
|
106
|
+
|
107
|
+
method = instance_method(name)
|
108
|
+
|
109
|
+
remove_method(wrapped_name) if method_defined?(wrapped_name)
|
110
|
+
|
111
|
+
alias_method(wrapped_name, name)
|
112
|
+
|
113
|
+
module_eval(Wrap.code_for(name))
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def rewrap!(name)
|
118
|
+
wrap!(name)
|
119
|
+
end
|
120
|
+
|
121
|
+
def wrapping!(name, &block)
|
122
|
+
name = name.to_s
|
123
|
+
@wrapping ||= []
|
124
|
+
|
125
|
+
return if @wrapping.last == name
|
126
|
+
|
127
|
+
@wrapping.push(name)
|
128
|
+
|
129
|
+
begin
|
130
|
+
block.call
|
131
|
+
ensure
|
132
|
+
@wrapping.pop
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def wrapping?(*name)
|
137
|
+
@wrapping ||= []
|
138
|
+
|
139
|
+
if name.empty?
|
140
|
+
!@wrapping.empty?
|
141
|
+
else
|
142
|
+
@wrapping.last == name.last.to_s
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def callbacks
|
147
|
+
@callbacks ||= Map.new
|
148
|
+
end
|
149
|
+
|
150
|
+
def initialize_callbacks!(name)
|
151
|
+
callbacks[name] ||= Map[
|
152
|
+
:before, [],
|
153
|
+
:after, []
|
154
|
+
]
|
155
|
+
callbacks[name]
|
156
|
+
end
|
157
|
+
|
158
|
+
def before(name, *args, &block)
|
159
|
+
cb = initialize_callbacks!(name)
|
160
|
+
cb.before.push(args.shift || block)
|
161
|
+
end
|
162
|
+
|
163
|
+
def after(name, *args, &block)
|
164
|
+
cb = initialize_callbacks!(name)
|
165
|
+
cb.after.push(args.shift || block)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
InstanceMethods = proc do
|
170
|
+
def running_callbacks(name, &block)
|
171
|
+
name = name.to_s
|
172
|
+
@running_callbacks ||= []
|
173
|
+
return block.call() if @running_callbacks.last == name
|
174
|
+
|
175
|
+
@running_callbacks.push(name)
|
176
|
+
|
177
|
+
begin
|
178
|
+
block.call()
|
179
|
+
ensure
|
180
|
+
@running_callbacks.pop
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def running_callbacks?(*name)
|
185
|
+
@running_callbacks ||= []
|
186
|
+
|
187
|
+
if name.empty?
|
188
|
+
@running_callbacks.last
|
189
|
+
else
|
190
|
+
@running_callbacks.last == name.last.to_s
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def run_callbacks(which, name)
|
195
|
+
which = which.to_s.to_sym
|
196
|
+
name = name.to_s
|
197
|
+
list = []
|
198
|
+
|
199
|
+
self.class.ancestors.each do |ancestor|
|
200
|
+
break unless ancestor.respond_to?(:callbacks)
|
201
|
+
|
202
|
+
if ancestor.callbacks.is_a?(Map) and ancestor.callbacks[name].is_a?(Map)
|
203
|
+
callbacks = ancestor.callbacks[name][which]
|
204
|
+
list.unshift(*callbacks) if callbacks.is_a?(Array)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
#p :which => list
|
209
|
+
list.each do |callback|
|
210
|
+
cb = callback.respond_to?(:call) ? callback : proc{ send(callback.to_s.to_sym) }
|
211
|
+
#p :cb => cb
|
212
|
+
instance_eval(&cb)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
data/test/testing.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
testdir = File.expand_path(File.dirname(__FILE__))
|
4
|
+
rootdir = File.dirname(testdir)
|
5
|
+
libdir = File.join(rootdir, 'lib')
|
6
|
+
|
7
|
+
STDOUT.sync = true
|
8
|
+
|
9
|
+
$:.unshift(testdir) unless $:.include?(testdir)
|
10
|
+
$:.unshift(libdir) unless $:.include?(libdir)
|
11
|
+
$:.unshift(rootdir) unless $:.include?(rootdir)
|
12
|
+
|
13
|
+
class Testing
|
14
|
+
class Slug < ::String
|
15
|
+
def Slug.for(*args)
|
16
|
+
string = args.flatten.compact.join('-')
|
17
|
+
words = string.to_s.scan(%r/\w+/)
|
18
|
+
words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
|
19
|
+
words.delete_if{|word| word.nil? or word.strip.empty?}
|
20
|
+
new(words.join('-').downcase)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Context
|
25
|
+
attr_accessor :name
|
26
|
+
|
27
|
+
def initialize(name, *args)
|
28
|
+
@name = name
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
Slug.for(name)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def Testing(*args, &block)
|
38
|
+
Class.new(::Test::Unit::TestCase) do
|
39
|
+
|
40
|
+
## class methods
|
41
|
+
#
|
42
|
+
class << self
|
43
|
+
def contexts
|
44
|
+
@contexts ||= []
|
45
|
+
end
|
46
|
+
|
47
|
+
def context(*args, &block)
|
48
|
+
return contexts.last if(args.empty? and block.nil?)
|
49
|
+
|
50
|
+
context = Testing::Context.new(*args)
|
51
|
+
contexts.push(context)
|
52
|
+
|
53
|
+
begin
|
54
|
+
block.call(context)
|
55
|
+
ensure
|
56
|
+
contexts.pop
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def slug_for(*args)
|
61
|
+
string = [context, args].flatten.compact.join('-')
|
62
|
+
words = string.to_s.scan(%r/\w+/)
|
63
|
+
words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
|
64
|
+
words.delete_if{|word| word.nil? or word.strip.empty?}
|
65
|
+
words.join('-').downcase.sub(/_$/, '')
|
66
|
+
end
|
67
|
+
|
68
|
+
def name() const_get(:Name) end
|
69
|
+
|
70
|
+
def testno()
|
71
|
+
'%05d' % (@testno ||= 0)
|
72
|
+
ensure
|
73
|
+
@testno += 1
|
74
|
+
end
|
75
|
+
|
76
|
+
def testing(*args, &block)
|
77
|
+
method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_')
|
78
|
+
define_method(method, &block)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test(*args, &block)
|
82
|
+
testing(*args, &block)
|
83
|
+
end
|
84
|
+
|
85
|
+
def setup(&block)
|
86
|
+
define_method(:setup, &block) if block
|
87
|
+
end
|
88
|
+
|
89
|
+
def teardown(&block)
|
90
|
+
define_method(:teardown, &block) if block
|
91
|
+
end
|
92
|
+
|
93
|
+
def prepare(&block)
|
94
|
+
@prepare ||= []
|
95
|
+
@prepare.push(block) if block
|
96
|
+
@prepare
|
97
|
+
end
|
98
|
+
|
99
|
+
def cleanup(&block)
|
100
|
+
@cleanup ||= []
|
101
|
+
@cleanup.push(block) if block
|
102
|
+
@cleanup
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
## configure the subclass!
|
107
|
+
#
|
108
|
+
const_set(:Testno, '0')
|
109
|
+
slug = slug_for(*args).gsub(%r/-/,'_')
|
110
|
+
name = ['TESTING', '%03d' % const_get(:Testno), slug].delete_if{|part| part.empty?}.join('_')
|
111
|
+
name = name.upcase!
|
112
|
+
const_set(:Name, name)
|
113
|
+
const_set(:Missing, Object.new.freeze)
|
114
|
+
|
115
|
+
## instance methods
|
116
|
+
#
|
117
|
+
alias_method('__assert__', 'assert')
|
118
|
+
|
119
|
+
def assert(*args, &block)
|
120
|
+
if args.size == 1 and args.first.is_a?(Hash)
|
121
|
+
options = args.first
|
122
|
+
expected = getopt(:expected, options){ missing }
|
123
|
+
actual = getopt(:actual, options){ missing }
|
124
|
+
if expected == missing and actual == missing
|
125
|
+
actual, expected, *ignored = options.to_a.flatten
|
126
|
+
end
|
127
|
+
expected = expected.call() if expected.respond_to?(:call)
|
128
|
+
actual = actual.call() if actual.respond_to?(:call)
|
129
|
+
assert_equal(expected, actual)
|
130
|
+
end
|
131
|
+
|
132
|
+
if block
|
133
|
+
label = "assert(#{ args.join(' ') })"
|
134
|
+
result = nil
|
135
|
+
assert_nothing_raised{ result = block.call }
|
136
|
+
__assert__(result, label)
|
137
|
+
result
|
138
|
+
else
|
139
|
+
result = args.shift
|
140
|
+
label = "assert(#{ args.join(' ') })"
|
141
|
+
__assert__(result, label)
|
142
|
+
result
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def missing
|
147
|
+
self.class.const_get(:Missing)
|
148
|
+
end
|
149
|
+
|
150
|
+
def getopt(opt, hash, options = nil, &block)
|
151
|
+
[opt.to_s, opt.to_s.to_sym].each do |key|
|
152
|
+
return hash[key] if hash.has_key?(key)
|
153
|
+
end
|
154
|
+
default =
|
155
|
+
if block
|
156
|
+
block.call
|
157
|
+
else
|
158
|
+
options.is_a?(Hash) ? options[:default] : nil
|
159
|
+
end
|
160
|
+
return default
|
161
|
+
end
|
162
|
+
|
163
|
+
def subclass_of exception
|
164
|
+
class << exception
|
165
|
+
def ==(other) super or self > other end
|
166
|
+
end
|
167
|
+
exception
|
168
|
+
end
|
169
|
+
|
170
|
+
##
|
171
|
+
#
|
172
|
+
module_eval(&block)
|
173
|
+
|
174
|
+
self.setup()
|
175
|
+
self.prepare.each{|b| b.call()}
|
176
|
+
|
177
|
+
at_exit{
|
178
|
+
self.teardown()
|
179
|
+
self.cleanup.each{|b| b.call()}
|
180
|
+
}
|
181
|
+
|
182
|
+
self
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
if $0 == __FILE__
|
188
|
+
|
189
|
+
Testing 'Testing' do
|
190
|
+
testing('foo'){ assert true }
|
191
|
+
test{ assert true }
|
192
|
+
p instance_methods.grep(/test/)
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
data/test/wrap_test.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
|
2
|
+
Testing Wrap do
|
3
|
+
|
4
|
+
testing 'that wrap can be included in a class' do
|
5
|
+
assert do
|
6
|
+
Class.new do
|
7
|
+
include Wrap
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
testing 'that a method can be wrapped before it is defined' do
|
13
|
+
assert do
|
14
|
+
wrapped_class do
|
15
|
+
assert_raises(NoMethodError){ new.foo() }
|
16
|
+
|
17
|
+
wrap :foo
|
18
|
+
|
19
|
+
assert_nothing_raised{ new.foo() }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
testing 'that a method can be wrapped after it is defined' do
|
25
|
+
assert do
|
26
|
+
wrapped_class do
|
27
|
+
def foo() 42 end
|
28
|
+
|
29
|
+
assert{ new.foo() == 42 }
|
30
|
+
|
31
|
+
wrap :foo
|
32
|
+
|
33
|
+
assert{ new.foo() == 42 }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def wrapped_class(&block)
|
40
|
+
tc = self
|
41
|
+
|
42
|
+
c =
|
43
|
+
Class.new do
|
44
|
+
include Wrap
|
45
|
+
|
46
|
+
const_set(:TC, tc)
|
47
|
+
|
48
|
+
def self.method_missing(method, *args, &block)
|
49
|
+
case method.to_s
|
50
|
+
when /^\Aassert/
|
51
|
+
const_get(:TC).send(method, *args, &block)
|
52
|
+
else
|
53
|
+
super
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
module_eval(&block)
|
58
|
+
end
|
59
|
+
|
60
|
+
c
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
BEGIN {
|
66
|
+
testdir = File.dirname(File.expand_path(__FILE__))
|
67
|
+
rootdir = File.dirname(testdir)
|
68
|
+
libdir = File.join(rootdir, 'lib')
|
69
|
+
require File.join(libdir, 'wrap')
|
70
|
+
require File.join(testdir, 'testing')
|
71
|
+
}
|
data/wrap.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
## wrap.gemspec
|
2
|
+
#
|
3
|
+
|
4
|
+
Gem::Specification::new do |spec|
|
5
|
+
spec.name = "wrap"
|
6
|
+
spec.version = "0.4.2"
|
7
|
+
spec.platform = Gem::Platform::RUBY
|
8
|
+
spec.summary = "wrap"
|
9
|
+
spec.description = "description: wrap kicks the ass"
|
10
|
+
|
11
|
+
spec.files =
|
12
|
+
["Rakefile",
|
13
|
+
"lib",
|
14
|
+
"lib/wrap",
|
15
|
+
"lib/wrap.rb",
|
16
|
+
"test",
|
17
|
+
"test/testing.rb",
|
18
|
+
"test/wrap_test.rb",
|
19
|
+
"wrap.gemspec"]
|
20
|
+
|
21
|
+
spec.executables = []
|
22
|
+
|
23
|
+
spec.require_path = "lib"
|
24
|
+
|
25
|
+
spec.test_files = nil
|
26
|
+
|
27
|
+
|
28
|
+
spec.add_dependency(*["map", " >= 4.7.1"])
|
29
|
+
|
30
|
+
|
31
|
+
spec.extensions.push(*[])
|
32
|
+
|
33
|
+
spec.rubyforge_project = "codeforpeople"
|
34
|
+
spec.author = "Ara T. Howard"
|
35
|
+
spec.email = "ara.t.howard@gmail.com"
|
36
|
+
spec.homepage = "https://github.com/ahoward/wrap"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wrap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ara T. Howard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: map
|
16
|
+
requirement: &70233826961320 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.7.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70233826961320
|
25
|
+
description: ! 'description: wrap kicks the ass'
|
26
|
+
email: ara.t.howard@gmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- Rakefile
|
32
|
+
- lib/wrap.rb
|
33
|
+
- test/testing.rb
|
34
|
+
- test/wrap_test.rb
|
35
|
+
- wrap.gemspec
|
36
|
+
homepage: https://github.com/ahoward/wrap
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project: codeforpeople
|
56
|
+
rubygems_version: 1.8.11
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: wrap
|
60
|
+
test_files: []
|