smoke_monster 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/lib/smoke_monster/array_to_object.rb +1 -1
- data/lib/smoke_monster/cover.rb +14 -2
- data/lib/smoke_monster/lazy_cover.rb +9 -9
- data/lib/smoke_monster/version.rb +1 -1
- data/smoke_monster.gemspec +4 -0
- data/spec/smoke_monster/cover_spec.rb +3 -3
- data/spec/smoke_monster/lambda_to_object_spec.rb +0 -6
- data/spec/smoke_monster/lazy_cover_spec.rb +65 -0
- data/spec/spec_helper.rb +1 -0
- metadata +27 -3
data/Gemfile
CHANGED
data/lib/smoke_monster/cover.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
require 'blankslate'
|
2
|
+
|
1
3
|
module SmokeMonster
|
2
|
-
|
4
|
+
module CoverMethods
|
3
5
|
def initialize(subject)
|
4
6
|
@subject = subject
|
5
7
|
end
|
@@ -7,11 +9,21 @@ module SmokeMonster
|
|
7
9
|
def method_missing(meth, *args, &blk)
|
8
10
|
@subject.send(meth, *args, &blk)
|
9
11
|
rescue
|
10
|
-
SmokeMonster::Cover.new
|
12
|
+
SmokeMonster::Cover.new Object.new
|
11
13
|
end
|
12
14
|
|
13
15
|
def the_original_subject
|
14
16
|
@subject
|
15
17
|
end
|
16
18
|
end
|
19
|
+
|
20
|
+
module StrictCoverMethods
|
21
|
+
def method_missing(meth, *args, &blk)
|
22
|
+
@subject.send(meth, *args, &blk)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Cover < ::BlankSlate
|
27
|
+
include SmokeMonster::CoverMethods
|
28
|
+
end
|
17
29
|
end
|
@@ -1,27 +1,27 @@
|
|
1
1
|
module SmokeMonster
|
2
2
|
class LazyCover < Cover
|
3
|
+
include SmokeMonster::StrictCoverMethods
|
4
|
+
|
3
5
|
def initialize(block)
|
4
6
|
@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
7
|
end
|
10
8
|
|
11
|
-
alias :
|
9
|
+
alias :the_original_subject_from_the_base_class :the_original_subject
|
12
10
|
def the_original_subject
|
13
|
-
|
14
|
-
|
11
|
+
setup_the_subject
|
12
|
+
the_original_subject_from_the_base_class
|
15
13
|
end
|
16
14
|
|
17
15
|
private
|
18
16
|
|
19
17
|
alias :old_method_missing :method_missing
|
20
18
|
def method_missing(meth, *args, &blk)
|
21
|
-
|
22
|
-
@subject = @block.call
|
19
|
+
setup_the_subject
|
23
20
|
old_method_missing meth, *args, &blk
|
24
21
|
end
|
25
22
|
|
23
|
+
def setup_the_subject
|
24
|
+
@subject = @block.call if @subject.nil?
|
25
|
+
end
|
26
26
|
end
|
27
27
|
end
|
data/smoke_monster.gemspec
CHANGED
@@ -14,4 +14,8 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "smoke_monster"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = SmokeMonster::VERSION
|
17
|
+
|
18
|
+
# specify any dependencies here; for example:
|
19
|
+
gem.add_runtime_dependency 'blankslate'
|
20
|
+
gem.add_development_dependency 'mocha'
|
17
21
|
end
|
@@ -14,9 +14,9 @@ describe SmokeMonster::Cover do
|
|
14
14
|
@view_model.or_this_either
|
15
15
|
end
|
16
16
|
|
17
|
-
it "should return
|
18
|
-
@view_model.should_be_a_view_model.
|
19
|
-
@view_model.another_test.
|
17
|
+
it "should return an object if the method does not exist" do
|
18
|
+
@view_model.should_be_a_view_model.wont_be_nil
|
19
|
+
@view_model.another_test.wont_be_nil
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should return new view models each time on methods that do not exist" do
|
@@ -6,9 +6,6 @@ describe "lambda to object" do
|
|
6
6
|
before do
|
7
7
|
@value = -> { nil }.to_object
|
8
8
|
end
|
9
|
-
it "should return a LazyCover object" do
|
10
|
-
@value.class.must_equal SmokeMonster::LazyCover
|
11
|
-
end
|
12
9
|
|
13
10
|
it "should remember the original value is nil" do
|
14
11
|
@value.the_original_subject.must_equal nil
|
@@ -19,9 +16,6 @@ describe "lambda to object" do
|
|
19
16
|
before do
|
20
17
|
@value = -> { 1 }.to_object
|
21
18
|
end
|
22
|
-
it "should return a LazyCover object" do
|
23
|
-
@value.class.must_equal SmokeMonster::LazyCover
|
24
|
-
end
|
25
19
|
|
26
20
|
it "should remember the original value is 1" do
|
27
21
|
@value.the_original_subject.must_equal 1
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'mocha'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
|
4
|
+
class LazyCoverTest
|
5
|
+
class << self
|
6
|
+
attr_accessor :was_called
|
7
|
+
end
|
8
|
+
end
|
9
|
+
describe SmokeMonster::LazyCover do
|
10
|
+
|
11
|
+
it "should not call the block passed to it" do
|
12
|
+
lazy = SmokeMonster::LazyCover.new(-> { raise 'was called' })
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should call the block passed to it when things are references" do
|
16
|
+
LazyCoverTest.was_called = false
|
17
|
+
lazy = SmokeMonster::LazyCover.new(-> { LazyCoverTest.was_called = true })
|
18
|
+
LazyCoverTest.was_called.must_equal false
|
19
|
+
lazy.to_s
|
20
|
+
LazyCoverTest.was_called.must_equal true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should call the block passed to it only once" do
|
24
|
+
LazyCoverTest.was_called = 0
|
25
|
+
lazy = SmokeMonster::LazyCover.new(-> { LazyCoverTest.was_called = LazyCoverTest.was_called + 1 })
|
26
|
+
LazyCoverTest.was_called.must_equal 0
|
27
|
+
lazy.to_s
|
28
|
+
LazyCoverTest.was_called.must_equal 1
|
29
|
+
lazy.to_s
|
30
|
+
LazyCoverTest.was_called.must_equal 1
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return the same exceptions passed from its base class methods" do
|
34
|
+
|
35
|
+
object = Object.new
|
36
|
+
object.stubs(:defined_method).returns("yes")
|
37
|
+
|
38
|
+
lazy = SmokeMonster::LazyCover.new(-> { object })
|
39
|
+
lazy.defined_method.must_equal "yes"
|
40
|
+
was_called = false
|
41
|
+
begin
|
42
|
+
lazy.not_a_defined_method
|
43
|
+
rescue NoMethodError
|
44
|
+
was_called = true
|
45
|
+
end
|
46
|
+
was_called.must_equal true
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "the original subject" do
|
50
|
+
it "should call the block passed to it" do
|
51
|
+
SmokeMonster::LazyCover.new(-> { 0 }).the_original_subject.must_equal 0
|
52
|
+
SmokeMonster::LazyCover.new(-> { "x" }).the_original_subject.must_equal "x"
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should only call the block passed to it once" do
|
56
|
+
LazyCoverTest.was_called = 0
|
57
|
+
lazy = SmokeMonster::LazyCover.new(-> { LazyCoverTest.was_called += 1 })
|
58
|
+
lazy.the_original_subject
|
59
|
+
LazyCoverTest.was_called.must_equal 1
|
60
|
+
lazy.the_original_subject
|
61
|
+
LazyCoverTest.was_called.must_equal 1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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.3.
|
4
|
+
version: 0.3.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,30 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
13
|
-
dependencies:
|
12
|
+
date: 2012-07-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: blankslate
|
16
|
+
requirement: &70290441103460 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70290441103460
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mocha
|
27
|
+
requirement: &70290441102820 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70290441102820
|
14
36
|
description: Unexplained, dark magic.
|
15
37
|
email:
|
16
38
|
- darren@cauthon.com
|
@@ -35,6 +57,7 @@ files:
|
|
35
57
|
- spec/smoke_monster/array_to_object_spec.rb
|
36
58
|
- spec/smoke_monster/cover_spec.rb
|
37
59
|
- spec/smoke_monster/lambda_to_object_spec.rb
|
60
|
+
- spec/smoke_monster/lazy_cover_spec.rb
|
38
61
|
- spec/smoke_monster/safety_proc_spec.rb
|
39
62
|
- spec/spec_helper.rb
|
40
63
|
homepage: http://www.github.com/darrencauthon/smoke_monster
|
@@ -65,5 +88,6 @@ test_files:
|
|
65
88
|
- spec/smoke_monster/array_to_object_spec.rb
|
66
89
|
- spec/smoke_monster/cover_spec.rb
|
67
90
|
- spec/smoke_monster/lambda_to_object_spec.rb
|
91
|
+
- spec/smoke_monster/lazy_cover_spec.rb
|
68
92
|
- spec/smoke_monster/safety_proc_spec.rb
|
69
93
|
- spec/spec_helper.rb
|