basic_assumption 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
data/lib/basic_assumption.rb
CHANGED
@@ -57,16 +57,25 @@ module BasicAssumption
|
|
57
57
|
#
|
58
58
|
# In both cases, the result is memoized and returned directly for
|
59
59
|
# subsequent calls.
|
60
|
-
|
60
|
+
#
|
61
|
+
# +assume+ will also create an attribute writer method that will allow the
|
62
|
+
# value returned by the instance method (the reader, from this point of view)
|
63
|
+
# to be overriden.
|
64
|
+
def assume(name, strategy={}, &block)
|
61
65
|
define_method(name) do
|
62
66
|
@basic_assumptions ||= {}
|
63
67
|
@basic_assumptions[name] ||= if block_given?
|
64
68
|
instance_eval(&block)
|
65
69
|
else
|
66
|
-
|
70
|
+
which = strategy[:with] || self.class
|
71
|
+
block = DefaultAssumption.resolve(which)
|
67
72
|
instance_exec(name, &block)
|
68
73
|
end
|
69
74
|
end
|
75
|
+
define_method("#{name}=") do |value|
|
76
|
+
@basic_assumptions ||= {}
|
77
|
+
@basic_assumptions[name] = value
|
78
|
+
end
|
70
79
|
after_assumption(name)
|
71
80
|
end
|
72
81
|
|
@@ -4,10 +4,19 @@ require 'lib/basic_assumption/default_assumption/simple_rails'
|
|
4
4
|
describe BasicAssumption::DefaultAssumption do
|
5
5
|
let(:mod) { BasicAssumption::DefaultAssumption }
|
6
6
|
describe "::register and ::resolve" do
|
7
|
-
|
7
|
+
before do
|
8
8
|
mod.should_receive(:strategy).with(:behavior).and_return(:block)
|
9
|
-
|
10
|
-
|
9
|
+
end
|
10
|
+
context "when a class is passed to resolve" do
|
11
|
+
it "maps a class to a proc according to an internal strategy" do
|
12
|
+
mod.register(Class, :behavior)
|
13
|
+
mod.resolve(Class).should eql(:block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
context "when a symbol is passed to resolve" do
|
17
|
+
it "returns the strategy-determined proc for that symbol" do
|
18
|
+
mod.resolve(:behavior).should eql(:block)
|
19
|
+
end
|
11
20
|
end
|
12
21
|
end
|
13
22
|
describe "::strategy" do
|
@@ -41,6 +41,16 @@ describe BasicAssumption do
|
|
41
41
|
extender_instance.by_default.should be_nil
|
42
42
|
end
|
43
43
|
|
44
|
+
context "when a strategy is passed to #assume" do
|
45
|
+
it "looks up the strategy from the existing defaults to provide the behavior of the instance method" do
|
46
|
+
extender_class.class_eval do
|
47
|
+
assume :looked_up, :with => :strategy
|
48
|
+
end
|
49
|
+
BasicAssumption::DefaultAssumption.should_receive(:resolve).with(:strategy).and_return(Proc.new {})
|
50
|
+
extender_instance.looked_up
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
44
54
|
context "when the default is overridden" do
|
45
55
|
it "returns the result of the overriding block" do
|
46
56
|
extender_class.class_eval do
|
@@ -91,6 +101,21 @@ describe BasicAssumption do
|
|
91
101
|
extender_instance.random_once.should eql(extender_instance.random_once)
|
92
102
|
end
|
93
103
|
end
|
104
|
+
|
105
|
+
context "#assume creates an attribute writer" do
|
106
|
+
before do
|
107
|
+
extender_class.class_eval do
|
108
|
+
assume(:writeable) { 'written' }
|
109
|
+
end
|
110
|
+
end
|
111
|
+
it "declares an attribute writer method of the given name" do
|
112
|
+
expect { extender_instance.writeable = 'overwritten' }.to_not raise_error
|
113
|
+
end
|
114
|
+
it "overrides the value returned by the created instance method" do
|
115
|
+
extender_instance.writeable = 'overwritten'
|
116
|
+
extender_instance.writeable.should eql('overwritten')
|
117
|
+
end
|
118
|
+
end
|
94
119
|
end
|
95
120
|
|
96
121
|
context "within Rails" do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 7
|
9
|
+
version: 0.3.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Matt Yoho
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-02 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|