monkey-lib 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,15 @@
1
- module Monkey
2
- module Backend
3
- module Facets
4
- def setup
5
- # Actually, facets has Kernel#tap, but it behaves different if the block takes no argument.
6
- require "monkey/backend/common/tap"
7
- require "monkey/backend/common/extract_options"
8
- require "monkey/backend/common/parent"
9
- require "core/facets/kernel/meta_class"
10
- require "core/facets/kernel/constant"
11
- ::String.class_eval do
12
- def constantize
13
- constant modulize
14
- end
15
- alias to_const_string camelcase
16
- alias to_const_path snakecase
17
- alias underscore snakecase
18
- end
1
+ Monkey::Backend.new :Facets do
2
+ def self.setup
3
+ load_libs :kernel => [:meta_class, :constant], :string => [:camelcase, :snakecase]
4
+ # Actually, facets has Kernel#tap, but it behaves different if the block takes no argument.
5
+ missing :tap, :extract_options, :parent
6
+ ::String.class_eval do
7
+ def constantize
8
+ constant to_const_string
19
9
  end
10
+ alias to_const_string upper_camelcase
11
+ alias to_const_path snakecase
12
+ alias underscore snakecase
20
13
  end
21
14
  end
22
15
  end
data/lib/monkey/ext.rb CHANGED
@@ -1,35 +1,43 @@
1
1
  module Monkey
2
2
  module Ext
3
-
3
+
4
4
  module ExtDSL
5
-
5
+
6
6
  def core_class(klass = nil)
7
7
  if klass
8
8
  @core_class = klass
9
9
  klass.send :include, self
10
+ @core_class.class_eval <<-EOS
11
+ def method_missing(meth, *args, &blk)
12
+ return super if Monkey::Backend.setup?
13
+ Monkey::Backend.setup
14
+ __send__(meth, *args, &blk)
15
+ end
16
+ EOS
10
17
  end
11
18
  return @core_class
12
19
  end
13
-
20
+
14
21
  def expects(*list)
15
22
  list.each do |name|
16
- unless instance_method name
17
- # Note: Ruby < 1.8.7 does not support { |*a, &b| } syntax.
18
- class_eval "def #{name}(*a, &b); Monkey::Backend.call(#{core_class.inspect}, #{name.to_s.inspect}, *a, &b); end"
19
- end
23
+ Monkey::Ext.expectations[core_class] << name.to_s
20
24
  end
21
25
  end
22
-
26
+
23
27
  end
24
-
28
+
29
+ def self.expectations
30
+ @expectations ||= Hash.new { |h,k| h[k] = [] }
31
+ end
32
+
25
33
  Dir[File.dirname(__FILE__) + "/ext/*.rb"].sort.each do |path|
26
34
  filename = File.basename(path, '.rb')
27
35
  class_name = filename.capitalize
28
36
  extension = class_eval "module #{class_name}; self; end"
29
37
  extension.extend ExtDSL
30
- extension.core_class = Object.const_get class_name
31
- require "ext/monkey/#{filename}"
38
+ extension.core_class Object.const_get(class_name)
39
+ require "monkey/ext/#{filename}"
32
40
  end
33
-
41
+
34
42
  end
35
43
  end
@@ -1,24 +1,8 @@
1
1
  module Monkey
2
2
  module Ext
3
3
  module Module
4
-
5
4
  # Defined by backend.
6
5
  expects :parent
7
-
8
- def nested_method_missing(mod, meth, *args, &block)
9
- # Triggers method_missing chain inside mod.
10
- raise Monkey::Watcher::NestedMethodMissingNotHandled
11
- end
12
-
13
- def class_added(klass)
14
- end
15
-
16
- def module_added(mod)
17
- return if self == parent
18
- class_added mod if mod.is_a? Class
19
- parent.module_added mod
20
- end
21
-
22
6
  end
23
7
  end
24
8
  end
@@ -1,8 +1,155 @@
1
+ require "pathname"
2
+
1
3
  module Monkey
2
4
  module Ext
3
5
  module String
4
6
  # Defined by backend.
5
7
  expects :constantize, :to_const_string, :to_const_path, :underscore, :camelcase
8
+
9
+ def atime
10
+ Pathname(self).atime
11
+ end
12
+
13
+ def absolute_path?
14
+ Pathname(self).absolute?
15
+ end
16
+
17
+ def basename
18
+ Pathname(self).basename.to_s
19
+ end
20
+
21
+ def blockdev?
22
+ Pathname(self).blockdev?
23
+ end
24
+
25
+ def chardev?
26
+ Pathname(self).chardev?
27
+ end
28
+
29
+ def cleanpath
30
+ Pathname(self).cleanpath.to_s
31
+ end
32
+
33
+ def ctime
34
+ Pathname(self).ctime
35
+ end
36
+
37
+ def directory_children
38
+ Pathname(self).children
39
+ end
40
+
41
+ def directory?
42
+ Pathname(self).directory?
43
+ end
44
+
45
+ def dirname
46
+ Pathname(self).dirname.to_s
47
+ end
48
+
49
+ def expand_path
50
+ Pathname(self).expand_path.to_s
51
+ end
52
+
53
+ def extname
54
+ Pathname(self).extname.to_s
55
+ end
56
+
57
+ def file?
58
+ Pathname(self).file?
59
+ end
60
+
61
+ def file_exist?
62
+ Pathname(self).exist?
63
+ end
64
+
65
+ alias file_exists? file_exist?
66
+
67
+ def file_grpowned?
68
+ Pathname(self).grpowned?
69
+ end
70
+
71
+ def file_owned?
72
+ Pathname(self).owned?
73
+ end
74
+
75
+ def file_size?
76
+ Pathname(self).size?
77
+ end
78
+
79
+ def file_sticky?
80
+ Pathname(self).sticky?
81
+ end
82
+
83
+ def file_executable?
84
+ Pathname(self).executable?
85
+ end
86
+
87
+ def file_executable_real?
88
+ Pathname(self).executable_real?
89
+ end
90
+
91
+ def file_join(other)
92
+ Pathname(self).join(other).to_s
93
+ end
94
+
95
+ alias / file_join
96
+
97
+ def file_readable?
98
+ Pathname(self).readable?
99
+ end
100
+
101
+ def file_readable_real?
102
+ Pathname(self).readable_real?
103
+ end
104
+
105
+ def file_relative?
106
+ Pathname(self).relative?
107
+ end
108
+
109
+ def file_writable?
110
+ Pathname(self).writable?
111
+ end
112
+
113
+ def file_writable_real?
114
+ Pathname(self).writable_real?
115
+ end
116
+
117
+ def file_zero?
118
+ Pathname(self).zero?
119
+ end
120
+
121
+ def ftype
122
+ Pathname(self).ftype.to_s
123
+ end
124
+
125
+ def mountpoint?
126
+ Pathname(self).mountpoint?
127
+ end
128
+
129
+ def mtime
130
+ Pathname(self).mtime
131
+ end
132
+
133
+ def pipe?
134
+ Pathname(self).pipe?
135
+ end
136
+
137
+ def realpath
138
+ Pathname(self).realpath.to_s
139
+ end
140
+
141
+ def root?
142
+ Pathname(self).root?
143
+ end
144
+
145
+ def socket?
146
+ Pathname(self).socket?
147
+ end
148
+
149
+ def symlink?
150
+ Pathname(self).symlink?
151
+ end
152
+
6
153
  end
7
154
  end
8
155
  end
@@ -1,18 +1,17 @@
1
- require __FILE__.sub("monkey/engine_spec.rb", "spec_helper")
2
- require "monkey/engine"
1
+ require __FILE__.sub(%r{monkey/.*$}, "spec_helper")
3
2
 
4
3
  describe Monkey::Engine do
5
-
4
+
6
5
  it "defines RUBY_VERSION" do
7
6
  defined?(RUBY_VERSION).should == "constant"
8
7
  end
9
-
8
+
10
9
  it "defines RUBY_ENGINE_VERSION" do
11
10
  defined?(RUBY_ENGINE_VERSION).should == "constant"
12
11
  end
13
-
12
+
14
13
  it "defines RUBY_DESCRIPTION" do
15
14
  defined?(RUBY_DESCRIPTION).should == "constant"
16
15
  end
17
-
16
+
18
17
  end
@@ -0,0 +1,18 @@
1
+ require __FILE__.sub(%r{monkey/.*$}, "spec_helper")
2
+
3
+ describe Monkey::Ext::Array do
4
+ describe "backend expectations" do
5
+ # expects :extract_options!
6
+ it "imports extract_options! from backend" do
7
+ [:foo, {:x => 10}].extract_options!.should == {:x => 10}
8
+ [:foo].extract_options!.should == {}
9
+ [{:x => 10}, :foo].extract_options!.should == {}
10
+ ary1 = [:foo, {:x => 10}]
11
+ ary1.extract_options!
12
+ ary1.should == [:foo]
13
+ ary2 = [{:x => 10}, :foo]
14
+ ary2.extract_options!
15
+ ary2.should == [{:x => 10}, :foo]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ require __FILE__.sub(%r{monkey/.*$}, "spec_helper")
2
+
3
+ describe Monkey::Ext::Module do
4
+ describe "backend expectations" do
5
+ # expects :parent
6
+ it "imports parent from backend" do
7
+ Monkey::Ext::Module.parent.should == Monkey::Ext
8
+ Monkey::Ext.parent.should == Monkey
9
+ Monkey.parent.should == Object
10
+ Object.parent.should == Object
11
+ Foo = Monkey::Ext::Module
12
+ Foo.parent.should == Monkey::Ext
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,53 @@
1
+ require __FILE__.sub(%r{monkey/.*$}, "spec_helper")
2
+
3
+ describe Monkey::Ext::Object do
4
+
5
+ before do
6
+ @obj = Object.new
7
+ end
8
+
9
+ describe "backend expectations" do
10
+
11
+ # expects :tap
12
+ it "imports tap from backend" do
13
+ @obj.tap { |o| o.should == @obj }
14
+ 42.tap { 23 }.should == 42
15
+ end
16
+
17
+ # expects :metaclass
18
+ it "imports metaclass from backend" do
19
+ @obj.metaclass.should == (class << @obj; self; end)
20
+ end
21
+
22
+ end
23
+
24
+ describe :instance_yield do
25
+
26
+ before do
27
+ @obj.stub! :foo
28
+ end
29
+
30
+ it "calls a block if block takes at least one argument" do
31
+ foo = nil
32
+ @obj.should_not_receive :foo
33
+ @obj.send :instance_yield do |x|
34
+ foo
35
+ end
36
+ end
37
+
38
+ it "passes object as first argument to blog" do
39
+ @obj.send :instance_yield do |x|
40
+ x.should == @obj
41
+ end
42
+ end
43
+
44
+ it "passes the block to instance_eval if block doesn't take arguments" do
45
+ @obj.should_receive :foo
46
+ @obj.send :instance_yield do
47
+ foo
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,331 @@
1
+ require __FILE__.sub(%r{monkey/.*$}, "spec_helper")
2
+
3
+ describe Monkey::Ext::String do
4
+
5
+ describe "backend expectations" do
6
+
7
+ # expects :constantize :underscore, :camelcase
8
+ it "imports constantize from backend" do
9
+ "Object".constantize.should == Object
10
+ "Monkey::Ext".constantize.should == Monkey::Ext
11
+ end
12
+
13
+ # expects :to_const_string
14
+ it "imports to_const_string from backend" do
15
+ "object".to_const_string.should == "Object"
16
+ "monkey/ext".to_const_string.should == "Monkey::Ext"
17
+ end
18
+
19
+ # expects :to_const_path
20
+ it "imports to_const_path from backend" do
21
+ "Object".to_const_path.should == "object"
22
+ "Monkey::Ext".to_const_path.should == "monkey/ext"
23
+ end
24
+
25
+ # expects :underscore
26
+ it "imports underscore from backend" do
27
+ "FooBar".underscore.should == "foo_bar"
28
+ end
29
+
30
+ # expects :camelcase
31
+ it "imports camelcase from backend" do
32
+ ["FooBar", "fooBar"].should include("foo_bar".camelcase)
33
+ end
34
+
35
+ end
36
+
37
+ describe "like Pathname" do
38
+
39
+ before do
40
+ @strings = ["/usr/bin/ruby", ".", "..", ENV["HOME"]]
41
+ @methods = Pathname.instance_methods(false).reject do |m|
42
+ String.methods.include? m or m.to_s == "to_path"
43
+ end
44
+ end
45
+
46
+ it "imports Pathname's cleanpath to String" do
47
+ @strings.each do |s|
48
+ s.cleanpath.should == Pathname(s).cleanpath.to_s
49
+ end
50
+ end
51
+
52
+ it "imports Pathname's realpath to String" do
53
+ @strings.each do |s|
54
+ s.realpath.should == Pathname(s).realpath.to_s
55
+ end
56
+ end
57
+
58
+ it "imports Pathname's mountpoint? to String" do
59
+ @strings.each do |s|
60
+ s.mountpoint?.should == Pathname(s).mountpoint?
61
+ end
62
+ end
63
+
64
+ it "imports Pathname's root? to String" do
65
+ @strings.each do |s|
66
+ s.root?.should == Pathname(s).root?
67
+ end
68
+ end
69
+
70
+ it "imports Pathname's absolute_path? to String" do
71
+ @strings.each do |s|
72
+ s.absolute_path?.should == Pathname(s).absolute?
73
+ end
74
+ end
75
+
76
+ it "imports Pathname's file_relative? to String" do
77
+ @strings.each do |s|
78
+ s.file_relative?.should == Pathname(s).relative?
79
+ end
80
+ end
81
+
82
+ it "imports Pathname's file_join to String" do
83
+ @strings.each do |s|
84
+ s.file_join("foo").should == Pathname(s).join("foo").to_s
85
+ end
86
+ end
87
+
88
+ it "imports Pathname's atime to String" do
89
+ @strings.each do |s|
90
+ s.atime.should == Pathname(s).atime
91
+ end
92
+ end
93
+
94
+ it "imports Pathname's ctime to String" do
95
+ @strings.each do |s|
96
+ s.ctime.should == Pathname(s).ctime
97
+ end
98
+ end
99
+
100
+ it "imports Pathname's mtime to String" do
101
+ @strings.each do |s|
102
+ s.mtime.should == Pathname(s).mtime
103
+ end
104
+ end
105
+
106
+ it "imports Pathname's ftype to String" do
107
+ @strings.each do |s|
108
+ s.ftype.should == Pathname(s).ftype.to_s
109
+ end
110
+ end
111
+
112
+ it "imports Pathname's basename to String" do
113
+ @strings.each do |s|
114
+ s.basename.should == Pathname(s).basename.to_s
115
+ end
116
+ end
117
+
118
+ it "imports Pathname's dirname to String" do
119
+ @strings.each do |s|
120
+ s.dirname.should == Pathname(s).dirname.to_s
121
+ end
122
+ end
123
+
124
+ it "imports Pathname's extname to String" do
125
+ @strings.each do |s|
126
+ s.extname.should == Pathname(s).extname.to_s
127
+ end
128
+ end
129
+
130
+ it "imports Pathname's expand_path to String" do
131
+ @strings.each do |s|
132
+ s.expand_path.should == Pathname(s).expand_path.to_s
133
+ end
134
+ end
135
+
136
+ it "imports Pathname's blockdev? to String" do
137
+ @strings.each do |s|
138
+ s.blockdev?.should == Pathname(s).blockdev?
139
+ end
140
+ end
141
+
142
+ it "imports Pathname's chardev? to String" do
143
+ @strings.each do |s|
144
+ s.chardev?.should == Pathname(s).chardev?
145
+ end
146
+ end
147
+
148
+ it "imports Pathname's file_executable? to String" do
149
+ @strings.each do |s|
150
+ s.file_executable?.should == Pathname(s).executable?
151
+ end
152
+ end
153
+
154
+ it "imports Pathname's file_executable_real? to String" do
155
+ @strings.each do |s|
156
+ s.file_executable_real?.should == Pathname(s).executable_real?
157
+ end
158
+ end
159
+
160
+ it "imports Pathname's atime to String" do
161
+ @strings.each do |s|
162
+ s.atime.should == Pathname(s).atime
163
+ end
164
+ end
165
+
166
+ it "imports Pathname's ctime to String" do
167
+ @strings.each do |s|
168
+ s.ctime.should == Pathname(s).ctime
169
+ end
170
+ end
171
+
172
+ it "imports Pathname's mtime to String" do
173
+ @strings.each do |s|
174
+ s.mtime.should == Pathname(s).mtime
175
+ end
176
+ end
177
+
178
+ it "imports Pathname's ftype to String" do
179
+ @strings.each do |s|
180
+ s.ftype.should == Pathname(s).ftype.to_s
181
+ end
182
+ end
183
+
184
+ it "imports Pathname's basename to String" do
185
+ @strings.each do |s|
186
+ s.basename.should == Pathname(s).basename.to_s
187
+ end
188
+ end
189
+
190
+ it "imports Pathname's dirname to String" do
191
+ @strings.each do |s|
192
+ s.dirname.should == Pathname(s).dirname.to_s
193
+ end
194
+ end
195
+
196
+ it "imports Pathname's extname to String" do
197
+ @strings.each do |s|
198
+ s.extname.should == Pathname(s).extname.to_s
199
+ end
200
+ end
201
+
202
+ it "imports Pathname's expand_path to String" do
203
+ @strings.each do |s|
204
+ s.expand_path.should == Pathname(s).expand_path.to_s
205
+ end
206
+ end
207
+
208
+ it "imports Pathname's blockdev? to String" do
209
+ @strings.each do |s|
210
+ s.blockdev?.should == Pathname(s).blockdev?
211
+ end
212
+ end
213
+
214
+ it "imports Pathname's chardev? to String" do
215
+ @strings.each do |s|
216
+ s.chardev?.should == Pathname(s).chardev?
217
+ end
218
+ end
219
+
220
+ it "imports Pathname's file_executable? to String" do
221
+ @strings.each do |s|
222
+ s.file_executable?.should == Pathname(s).executable?
223
+ end
224
+ end
225
+
226
+ it "imports Pathname's file_executable_real? to String" do
227
+ @strings.each do |s|
228
+ s.file_executable_real?.should == Pathname(s).executable_real?
229
+ end
230
+ end
231
+
232
+ it "imports Pathname's directory? to String" do
233
+ @strings.each do |s|
234
+ s.directory?.should == Pathname(s).directory?
235
+ end
236
+ end
237
+
238
+ it "imports Pathname's file? to String" do
239
+ @strings.each do |s|
240
+ s.file?.should == Pathname(s).file?
241
+ end
242
+ end
243
+
244
+ it "imports Pathname's pipe? to String" do
245
+ @strings.each do |s|
246
+ s.pipe?.should == Pathname(s).pipe?
247
+ end
248
+ end
249
+
250
+ it "imports Pathname's socket? to String" do
251
+ @strings.each do |s|
252
+ s.socket?.should == Pathname(s).socket?
253
+ end
254
+ end
255
+
256
+ it "imports Pathname's file_owned? to String" do
257
+ @strings.each do |s|
258
+ s.file_owned?.should == Pathname(s).owned?
259
+ end
260
+ end
261
+
262
+ it "imports Pathname's file_readable? to String" do
263
+ @strings.each do |s|
264
+ s.file_readable?.should == Pathname(s).readable?
265
+ end
266
+ end
267
+
268
+ it "imports Pathname's file_readable_real? to String" do
269
+ @strings.each do |s|
270
+ s.file_readable_real?.should == Pathname(s).readable_real?
271
+ end
272
+ end
273
+
274
+ it "imports Pathname's symlink? to String" do
275
+ @strings.each do |s|
276
+ s.symlink?.should == Pathname(s).symlink?
277
+ end
278
+ end
279
+
280
+ it "imports Pathname's file_writable? to String" do
281
+ @strings.each do |s|
282
+ s.file_writable?.should == Pathname(s).writable?
283
+ end
284
+ end
285
+
286
+ it "imports Pathname's file_writable_real? to String" do
287
+ @strings.each do |s|
288
+ s.file_writable_real?.should == Pathname(s).writable_real?
289
+ end
290
+ end
291
+
292
+ it "imports Pathname's file_zero? to String" do
293
+ @strings.each do |s|
294
+ s.file_zero?.should == Pathname(s).zero?
295
+ end
296
+ end
297
+
298
+ it "imports Pathname's file_exist? to String" do
299
+ @strings.each do |s|
300
+ s.file_exist?.should == Pathname(s).exist?
301
+ s.file_exists?.should == Pathname(s).exist?
302
+ end
303
+ end
304
+
305
+ it "imports Pathname's file_grpowned? to String" do
306
+ @strings.each do |s|
307
+ s.file_grpowned?.should == Pathname(s).grpowned?
308
+ end
309
+ end
310
+
311
+ it "imports Pathname's file_size? to String" do
312
+ @strings.each do |s|
313
+ s.file_size?.should == Pathname(s).size?
314
+ end
315
+ end
316
+
317
+ it "imports Pathname's file_sticky? to String" do
318
+ @strings.each do |s|
319
+ s.file_sticky?.should == Pathname(s).sticky?
320
+ end
321
+ end
322
+
323
+ it "imports Pathname's directory_children to String" do
324
+ @strings.each do |s|
325
+ s.directory_children.should == Pathname(s).children if s.directory?
326
+ end
327
+ end
328
+
329
+ end
330
+
331
+ end