smoke_monster 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/smoke_monster/{view_model.rb → cover.rb} +6 -2
- data/lib/smoke_monster/lambda_to_object.rb +5 -0
- data/lib/smoke_monster/lazy_cover.rb +27 -0
- data/lib/smoke_monster/version.rb +1 -1
- data/spec/smoke_monster/{view_model_spec.rb → cover_spec.rb} +18 -6
- data/spec/smoke_monster/lambda_to_object_spec.rb +45 -0
- metadata +8 -4
@@ -1,5 +1,5 @@
|
|
1
1
|
module SmokeMonster
|
2
|
-
class
|
2
|
+
class Cover
|
3
3
|
def initialize(subject)
|
4
4
|
@subject = subject
|
5
5
|
end
|
@@ -7,7 +7,11 @@ module SmokeMonster
|
|
7
7
|
def method_missing(meth, *args, &blk)
|
8
8
|
@subject.send(meth, *args, &blk)
|
9
9
|
rescue
|
10
|
-
SmokeMonster::
|
10
|
+
SmokeMonster::Cover.new nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def the_original_subject
|
14
|
+
@subject
|
11
15
|
end
|
12
16
|
end
|
13
17
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module SmokeMonster
|
2
|
+
class LazyCover < Cover
|
3
|
+
def initialize(block)
|
4
|
+
@block = block
|
5
|
+
self.public_methods.select{ |x| x.to_s.include?("the_original_subject") == false }.each do |method|
|
6
|
+
remove_method method
|
7
|
+
end
|
8
|
+
@can_allow_method_missing_to_work_now = true
|
9
|
+
end
|
10
|
+
|
11
|
+
alias :old_the_original_subject :the_original_subject
|
12
|
+
def the_original_subject
|
13
|
+
@subject = @block.call
|
14
|
+
old_the_original_subject
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
alias :old_method_missing :method_missing
|
20
|
+
def method_missing(meth, *args, &blk)
|
21
|
+
return nil unless @can_allow_method_missing_to_work_now
|
22
|
+
@subject = @block.call
|
23
|
+
old_method_missing meth, *args, &blk
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
|
-
describe SmokeMonster::
|
3
|
+
describe SmokeMonster::Cover do
|
4
4
|
before do
|
5
5
|
end
|
6
6
|
|
7
7
|
describe "when given an Object" do
|
8
8
|
before do
|
9
|
-
@view_model = SmokeMonster::
|
9
|
+
@view_model = SmokeMonster::Cover.new Object.new
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should not throw an exception on methods that do not exist" do
|
@@ -15,8 +15,8 @@ describe SmokeMonster::ViewModel do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should return a view model if the method does not exist" do
|
18
|
-
@view_model.should_be_a_view_model.must_be_instance_of SmokeMonster::
|
19
|
-
@view_model.another_test.must_be_instance_of SmokeMonster::
|
18
|
+
@view_model.should_be_a_view_model.must_be_instance_of SmokeMonster::Cover
|
19
|
+
@view_model.another_test.must_be_instance_of SmokeMonster::Cover
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should return new view models each time on methods that do not exist" do
|
@@ -36,7 +36,7 @@ describe SmokeMonster::ViewModel do
|
|
36
36
|
|
37
37
|
before do
|
38
38
|
@person = Person.new
|
39
|
-
@view_model = SmokeMonster::
|
39
|
+
@view_model = SmokeMonster::Cover.new @person
|
40
40
|
end
|
41
41
|
|
42
42
|
[["John", "Galt"], ["Howard", "Roark"]].each do |name|
|
@@ -69,7 +69,7 @@ describe SmokeMonster::ViewModel do
|
|
69
69
|
|
70
70
|
before do
|
71
71
|
@thing = Thing.new
|
72
|
-
@view_model = SmokeMonster::
|
72
|
+
@view_model = SmokeMonster::Cover.new @thing
|
73
73
|
end
|
74
74
|
|
75
75
|
it "should call the block" do
|
@@ -79,4 +79,16 @@ describe SmokeMonster::ViewModel do
|
|
79
79
|
Thing.test_value.must_equal true
|
80
80
|
end
|
81
81
|
end
|
82
|
+
|
83
|
+
[1, "abc", Object.new].each do |subject|
|
84
|
+
describe "#the_original_subject" do
|
85
|
+
before do
|
86
|
+
@cover = SmokeMonster::Cover.new subject
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should return the original" do
|
90
|
+
@cover.the_original_subject.must_equal subject
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
82
94
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "lambda to object" do
|
4
|
+
|
5
|
+
describe "to_object on a lambda that returns nil" do
|
6
|
+
before do
|
7
|
+
@value = -> { nil }.to_object
|
8
|
+
end
|
9
|
+
it "should return a LazyCover object" do
|
10
|
+
@value.class.must_equal SmokeMonster::LazyCover
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should remember the original value is nil" do
|
14
|
+
@value.the_original_subject.must_equal nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "to_object on a lambda that returns an integer" do
|
19
|
+
before do
|
20
|
+
@value = -> { 1 }.to_object
|
21
|
+
end
|
22
|
+
it "should return a LazyCover object" do
|
23
|
+
@value.class.must_equal SmokeMonster::LazyCover
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should remember the original value is 1" do
|
27
|
+
@value.the_original_subject.must_equal 1
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be able to use the value as an integer" do
|
31
|
+
(@value + 1).must_equal 2
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "to_object on a lambda, but never accessing the variable" do
|
36
|
+
before do
|
37
|
+
@was_called = false
|
38
|
+
@value = -> { raise 'it was called' }.to_object
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not be called" do
|
42
|
+
#will throw an exception if it was called above
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smoke_monster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -26,11 +26,14 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- lib/smoke_monster.rb
|
28
28
|
- lib/smoke_monster/array_to_object.rb
|
29
|
+
- lib/smoke_monster/cover.rb
|
30
|
+
- lib/smoke_monster/lambda_to_object.rb
|
31
|
+
- lib/smoke_monster/lazy_cover.rb
|
29
32
|
- lib/smoke_monster/version.rb
|
30
|
-
- lib/smoke_monster/view_model.rb
|
31
33
|
- smoke_monster.gemspec
|
32
34
|
- spec/smoke_monster/array_to_object_spec.rb
|
33
|
-
- spec/smoke_monster/
|
35
|
+
- spec/smoke_monster/cover_spec.rb
|
36
|
+
- spec/smoke_monster/lambda_to_object_spec.rb
|
34
37
|
- spec/spec_helper.rb
|
35
38
|
homepage: http://www.github.com/darrencauthon/smoke_monster
|
36
39
|
licenses: []
|
@@ -58,5 +61,6 @@ specification_version: 3
|
|
58
61
|
summary: Unexplained, dark magic.
|
59
62
|
test_files:
|
60
63
|
- spec/smoke_monster/array_to_object_spec.rb
|
61
|
-
- spec/smoke_monster/
|
64
|
+
- spec/smoke_monster/cover_spec.rb
|
65
|
+
- spec/smoke_monster/lambda_to_object_spec.rb
|
62
66
|
- spec/spec_helper.rb
|