dicer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/dicer/context/description.rb +33 -4
- data/lib/dicer/context.rb +6 -2
- data/lib/dicer/contextable.rb +9 -1
- data/lib/dicer/railtie/contextable.rb +5 -0
- data/lib/dicer/rspec/behavior_example_group.rb +1 -1
- data/lib/dicer/version.rb +1 -1
- data/spec/dicer/context/description_spec.rb +17 -2
- data/spec/dicer/contextable_spec.rb +3 -3
- metadata +4 -4
data/README.md
CHANGED
@@ -5,22 +5,51 @@ module Dicer
|
|
5
5
|
class Description
|
6
6
|
def initialize(described_class, &block)
|
7
7
|
@described_class = described_class
|
8
|
-
@behaviors =
|
8
|
+
@behaviors = {}
|
9
|
+
@behaviors.default = []
|
10
|
+
@delegators = {}
|
9
11
|
|
10
12
|
instance_eval(&block) if block_given?
|
11
13
|
end
|
12
14
|
attr_reader :described_class, :behaviors
|
13
15
|
|
14
16
|
def it_behaves_like(behavior)
|
15
|
-
@behaviors << behavior
|
17
|
+
@behaviors[@role] << behavior
|
18
|
+
end
|
19
|
+
|
20
|
+
def role(name, &block)
|
21
|
+
@role = name.to_s.to_sym
|
22
|
+
@behaviors[@role] = [] unless @behaviors.has_key?(@role)
|
23
|
+
instance_eval(&block) if block_given?
|
24
|
+
@role = nil
|
16
25
|
end
|
17
26
|
|
18
27
|
def merge!(other)
|
19
|
-
|
28
|
+
other.behaviors.each_pair do |role, behaviors|
|
29
|
+
if @behaviors.has_key?(role)
|
30
|
+
@behaviors[role] = @behaviors[role] | behaviors
|
31
|
+
else
|
32
|
+
@behaviors[role] = behaviors
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
@behaviors.default = @behaviors.default | other.behaviors.default
|
20
37
|
end
|
21
38
|
|
22
39
|
def delegator
|
23
|
-
@
|
40
|
+
@delegators.default ||= Dicer::Delegator.make(
|
41
|
+
@described_class,
|
42
|
+
@behaviors.default
|
43
|
+
)
|
44
|
+
end
|
45
|
+
|
46
|
+
def delegator_for(role)
|
47
|
+
role = role.to_s.to_sym
|
48
|
+
|
49
|
+
@delegators[role] ||= Dicer::Delegator.make(
|
50
|
+
@described_class,
|
51
|
+
@behaviors[role] | @behaviors.default
|
52
|
+
)
|
24
53
|
end
|
25
54
|
end
|
26
55
|
end
|
data/lib/dicer/context.rb
CHANGED
@@ -20,9 +20,13 @@ module Dicer
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def supply(object)
|
23
|
+
def supply(object, role = nil)
|
24
24
|
description = self.class.descriptions[object.class]
|
25
|
-
delegator = description ?
|
25
|
+
delegator = description ?
|
26
|
+
(role.nil? ?
|
27
|
+
description.delegator :
|
28
|
+
description.delegator_for(role)) :
|
29
|
+
nil
|
26
30
|
|
27
31
|
delegator ? delegator.new(object) : object
|
28
32
|
end
|
data/lib/dicer/contextable.rb
CHANGED
@@ -10,7 +10,15 @@ module Dicer
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def as(
|
13
|
+
def as(role, context, &block)
|
14
|
+
if block.nil?
|
15
|
+
context.supply(self, role)
|
16
|
+
else
|
17
|
+
block.call(context.supply(self, role))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def behaves_like(*behaviors, &block)
|
14
22
|
delegator = Dicer::Delegator.make(self.class, behaviors)
|
15
23
|
|
16
24
|
block.nil? ? delegator.new(self) : block.call(delegator.new(self))
|
@@ -6,5 +6,10 @@ module Dicer
|
|
6
6
|
in_context_without_dicer(context, &block)
|
7
7
|
end
|
8
8
|
alias_method_chain :in_context, :dicer
|
9
|
+
|
10
|
+
def as_with_dicer(role, context = Dicer::Context.current, &block)
|
11
|
+
as_without_dicer(role, context, &block)
|
12
|
+
end
|
13
|
+
alias_method_chain :as, :dicer
|
9
14
|
end
|
10
15
|
end
|
data/lib/dicer/version.rb
CHANGED
@@ -4,6 +4,10 @@ describe Dicer::Context::Description do
|
|
4
4
|
subject(:description) do
|
5
5
|
Dicer::Context::Description.new(Entity) do
|
6
6
|
it_behaves_like Cleaner
|
7
|
+
|
8
|
+
role :cleaner do
|
9
|
+
it_behaves_like Cleaner
|
10
|
+
end
|
7
11
|
end
|
8
12
|
end
|
9
13
|
|
@@ -16,8 +20,19 @@ describe Dicer::Context::Description do
|
|
16
20
|
describe '#behaviors' do
|
17
21
|
subject { description.behaviors }
|
18
22
|
|
19
|
-
it { should
|
20
|
-
|
23
|
+
it { should be_a(Hash) }
|
24
|
+
|
25
|
+
describe '#default' do
|
26
|
+
subject { description.behaviors.default }
|
27
|
+
|
28
|
+
it { should include(Cleaner) }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '[:cleaner]' do
|
32
|
+
subject { description.behaviors[:cleaner] }
|
33
|
+
|
34
|
+
it { should include(Cleaner) }
|
35
|
+
end
|
21
36
|
end
|
22
37
|
|
23
38
|
describe '#delegator' do
|
@@ -19,14 +19,14 @@ describe Dicer::Contextable do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
describe '#
|
23
|
-
subject { entity.
|
22
|
+
describe '#behaves_like' do
|
23
|
+
subject { entity.behaves_like(Cleaner) }
|
24
24
|
|
25
25
|
it { should respond_to(:clean) }
|
26
26
|
|
27
27
|
context 'with block' do
|
28
28
|
it do
|
29
|
-
entity.
|
29
|
+
entity.behaves_like(Cleaner) do |cleaner|
|
30
30
|
cleaner.should respond_to(:clean)
|
31
31
|
end
|
32
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dicer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -230,7 +230,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
230
230
|
version: '0'
|
231
231
|
segments:
|
232
232
|
- 0
|
233
|
-
hash:
|
233
|
+
hash: 654783733716943402
|
234
234
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
235
235
|
none: false
|
236
236
|
requirements:
|
@@ -239,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
239
239
|
version: '0'
|
240
240
|
segments:
|
241
241
|
- 0
|
242
|
-
hash:
|
242
|
+
hash: 654783733716943402
|
243
243
|
requirements: []
|
244
244
|
rubyforge_project:
|
245
245
|
rubygems_version: 1.8.23
|