rkh-monkey-lib 0.1.5 → 0.1.6
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.
- data/Rakefile +1 -1
- data/lib/monkey/object/instance_yield.rb +38 -0
- data/spec/monkey/object/instance_yield_spec.rb +32 -0
- data/spec/monkey/string/like_pathname_spec.rb +10 -10
- metadata +5 -2
data/Rakefile
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Monkey
|
2
|
+
module Object
|
3
|
+
|
4
|
+
# Adds the private method instance_eval which is like instance_eval
|
5
|
+
# and yield depending on whether a block takes an argument or not.
|
6
|
+
#
|
7
|
+
# class Foo
|
8
|
+
#
|
9
|
+
# def foo
|
10
|
+
# 42
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# def bar(&block)
|
14
|
+
# instance_yield block
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# conf = Foo.new
|
20
|
+
# conf.bar { foo }
|
21
|
+
# conf.bar { |c| c.foo }
|
22
|
+
module InstanceYield
|
23
|
+
|
24
|
+
::Object.send :include, self
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# See InstanceYield description.
|
29
|
+
def instance_yield(block = nil, &alternate_block)
|
30
|
+
raise ArgumentError, "too many blocks given" if block && alternate_block
|
31
|
+
block ||= alternate_block
|
32
|
+
raise LocalJumpError, "no block given (yield)" unless block
|
33
|
+
block.arity > 0 ? yield(self) : instance_eval(&block)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require __FILE__.sub("monkey/object/instance_yield_spec.rb", "spec_helper")
|
2
|
+
require "monkey/object/instance_yield"
|
3
|
+
|
4
|
+
describe Monkey::Object::InstanceYield do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@obj = Object.new
|
8
|
+
@obj.stub! :foo
|
9
|
+
end
|
10
|
+
|
11
|
+
it "calls a block if block takes at least one argument" do
|
12
|
+
foo = nil
|
13
|
+
@obj.should_not_receive :foo
|
14
|
+
@obj.send :instance_yield do |x|
|
15
|
+
foo
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "passes object as first argument to blog" do
|
20
|
+
@obj.send :instance_yield do |x|
|
21
|
+
x.should == @obj
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it "passes the block to instance_eval if block doesn't take arguments" do
|
26
|
+
@obj.should_receive :foo
|
27
|
+
@obj.send :instance_yield do
|
28
|
+
foo
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -10,10 +10,10 @@ describe Monkey::String::LikePathname do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
it "exposes sub_ext to String" do
|
13
|
+
it "exposes sub_ext to String if in ruby 1.9" do
|
14
14
|
@strings.each do |s|
|
15
15
|
s.sub_ext(".png").should == Pathname(s).sub_ext(".png").to_s
|
16
|
-
end
|
16
|
+
end if RUBY_VERSION >= "1.9"
|
17
17
|
end
|
18
18
|
|
19
19
|
it "exposes cleanpath to String" do
|
@@ -58,10 +58,10 @@ describe Monkey::String::LikePathname do
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
it "exposes each_filename to String" do
|
61
|
+
it "exposes each_filename to String if in ruby 1.9" do
|
62
62
|
@strings.each do |s|
|
63
63
|
s.each_filename.to_a.should == Pathname(s).each_filename.to_a
|
64
|
-
end
|
64
|
+
end if RUBY_VERSION >= "1.9"
|
65
65
|
end
|
66
66
|
|
67
67
|
it "exposes join to String" do
|
@@ -197,10 +197,10 @@ describe Monkey::String::LikePathname do
|
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
200
|
-
it "exposes world_readable? to String" do
|
200
|
+
it "exposes world_readable? to String in ruby 1.9" do
|
201
201
|
@strings.each do |s|
|
202
202
|
s.world_readable?.should == Pathname(s).world_readable?
|
203
|
-
end
|
203
|
+
end if RUBY_VERSION >= "1.9"
|
204
204
|
end
|
205
205
|
|
206
206
|
it "exposes readable_real? to String" do
|
@@ -245,10 +245,10 @@ describe Monkey::String::LikePathname do
|
|
245
245
|
end
|
246
246
|
end
|
247
247
|
|
248
|
-
it "exposes world_writable? to String" do
|
248
|
+
it "exposes world_writable? to String in ruby 1.9" do
|
249
249
|
@strings.each do |s|
|
250
250
|
s.world_writable?.should == Pathname(s).world_writable?
|
251
|
-
end
|
251
|
+
end if RUBY_VERSION >= "1.9"
|
252
252
|
end
|
253
253
|
|
254
254
|
it "exposes writable_real? to String" do
|
@@ -263,10 +263,10 @@ describe Monkey::String::LikePathname do
|
|
263
263
|
end
|
264
264
|
end
|
265
265
|
|
266
|
-
it "exposes entries to String" do
|
266
|
+
it "exposes entries to String in ruby 1.9" do
|
267
267
|
@strings.each do |s|
|
268
268
|
s.entries.should == Pathname(s).entries if s.directory?
|
269
|
-
end
|
269
|
+
end if RUBY_VERSION >= "1.9"
|
270
270
|
end
|
271
271
|
|
272
272
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rkh-monkey-lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Konstantin Haase
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-22 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -35,6 +35,7 @@ extra_rdoc_files:
|
|
35
35
|
- lib/monkey/hash/sub_hash.rb
|
36
36
|
- lib/monkey/hash.rb
|
37
37
|
- lib/monkey/object/backports.rb
|
38
|
+
- lib/monkey/object/instance_yield.rb
|
38
39
|
- lib/monkey/object.rb
|
39
40
|
- lib/monkey/string/like_pathname.rb
|
40
41
|
- lib/monkey/string.rb
|
@@ -48,6 +49,7 @@ files:
|
|
48
49
|
- lib/monkey/hash/sub_hash.rb
|
49
50
|
- lib/monkey/hash.rb
|
50
51
|
- lib/monkey/object/backports.rb
|
52
|
+
- lib/monkey/object/instance_yield.rb
|
51
53
|
- lib/monkey/object.rb
|
52
54
|
- lib/monkey/string/like_pathname.rb
|
53
55
|
- lib/monkey/string.rb
|
@@ -56,6 +58,7 @@ files:
|
|
56
58
|
- spec/monkey/engine_spec.rb
|
57
59
|
- spec/monkey/hash/sub_hash_spec.rb
|
58
60
|
- spec/monkey/object/backports_spec.rb
|
61
|
+
- spec/monkey/object/instance_yield_spec.rb
|
59
62
|
- spec/monkey/string/like_pathname_spec.rb
|
60
63
|
- spec/spec_helper.rb
|
61
64
|
has_rdoc: false
|