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 CHANGED
@@ -70,7 +70,7 @@ Or install it yourself as:
70
70
 
71
71
  ## Usage
72
72
 
73
- TODO: Write usage instructions here
73
+ Read [Getting Started](https://github.com/rosylilly/dicer/wiki/Getting-started)
74
74
 
75
75
  ## Contributing
76
76
 
@@ -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
- @behaviors.concat(other.behaviors)
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
- @delegator ||= Dicer::Delegator.make(@described_class, @behaviors)
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 ? description.delegator : nil
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
@@ -10,7 +10,15 @@ module Dicer
10
10
  end
11
11
  end
12
12
 
13
- def as(*behaviors, &block)
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
@@ -5,7 +5,7 @@ module Dicer
5
5
 
6
6
  included do
7
7
  subject do
8
- extend_to.as(described_class)
8
+ extend_to.behaves_like(described_class)
9
9
  end
10
10
  end
11
11
 
data/lib/dicer/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dicer
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -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 have(1).behavior }
20
- it { should include(Cleaner) }
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 '#as' do
23
- subject { entity.as(Cleaner) }
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.as(Cleaner) do |cleaner|
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.1.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-17 00:00:00.000000000 Z
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: 2972979481225296162
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: 2972979481225296162
242
+ hash: 654783733716943402
243
243
  requirements: []
244
244
  rubyforge_project:
245
245
  rubygems_version: 1.8.23