rddd 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -11,8 +11,8 @@ module Rddd
11
11
  #
12
12
  # Initialize.
13
13
  #
14
- # @param {Symbol} Action to apply rule to.
15
- # @param {Block} Block to be evaluated during authorization.
14
+ # @param [Symbol] Action to apply rule to.
15
+ # @param [Block] Block to be evaluated during authorization.
16
16
  #
17
17
  def initialize(action, &block)
18
18
  # Block should take two arguments - user and params.
@@ -26,6 +26,7 @@ module Rddd
26
26
  # Is the Rule for given action?
27
27
  #
28
28
  # @param {Symbol} Action to be matched.
29
+ # @private
29
30
  #
30
31
  def is_for?(action)
31
32
  @action == action
@@ -8,14 +8,14 @@ module Rddd
8
8
  class Configuration
9
9
  include Singleton
10
10
 
11
- attr_writer :service_creator, :repositories_namespace
11
+ attr_writer :service_creator, :repository_creator
12
12
 
13
13
  def service_creator
14
14
  @service_creator
15
15
  end
16
16
 
17
- def repositories_namespace
18
- @repositories_namespace || Object
17
+ def repository_creator
18
+ @repository_creator
19
19
  end
20
20
  end
21
21
  end
@@ -2,4 +2,15 @@
2
2
  # Repository base class.
3
3
  #
4
4
  class Rddd::Repository
5
+ def create(subject)
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def update(subject)
10
+ raise NotImplementedError
11
+ end
12
+
13
+ def delete(subject)
14
+ raise NotImplementedError
15
+ end
5
16
  end
@@ -6,12 +6,15 @@ module Rddd
6
6
  end
7
7
 
8
8
  class RepositoryFactory
9
+ CreatorNotGiven = Class.new(RuntimeError)
10
+
9
11
  def self.build(clazz)
10
- repository_name = "#{clazz.name.split('::').last}Repository"
11
- ns = Configuration.instance.repositories_namespace
12
+ creator = Configuration.instance.repository_creator
13
+
14
+ raise CreatorNotGiven unless creator
12
15
 
13
16
  begin
14
- repository = ns.const_get(repository_name)
17
+ repository = creator.call(clazz)
15
18
  rescue
16
19
  raise NotExistingRepository unless repository
17
20
  end
@@ -4,19 +4,27 @@
4
4
  # cooperation of multiple entities, in a given order defined by use case.
5
5
  #
6
6
  # Service takes a list of attributes, which are necessary to execute the given
7
- # use case. At the end nothing is returned, so service represent the command to
8
- # the domain, but doesn't return any data back.
7
+ # use case. You shouldn't need to create Service directly. Instead use ServiceBus.
9
8
  #
10
- class Rddd::Service
11
- def initialize(attributes = {})
12
- @attributes = attributes
13
- end
9
+ # Its good practice to leave your service return-less. Although if you *really* need
10
+ # something back from the service for other usage, be sure you return just data in
11
+ # form of DTO (Hash, Array of Hashes), but no Entities and AggregateRoot or other
12
+ # domain objects, so you dont leak business out.
13
+ #
14
+ # Remember dummy data goes in, dummy data goes out.
15
+ #
16
+ module Rddd
17
+ class Service
18
+ def initialize(attributes = {})
19
+ @attributes = attributes
20
+ end
14
21
 
15
- def valid?
16
- true
17
- end
22
+ def valid?
23
+ true
24
+ end
18
25
 
19
- def execute
20
- raise NotImplementedError
26
+ def execute
27
+ raise NotImplementedError
28
+ end
21
29
  end
22
30
  end
@@ -1,8 +1,6 @@
1
1
  require 'rddd/service_factory'
2
2
 
3
3
  module Rddd
4
- class InvalidService < RuntimeError; end
5
-
6
4
  #
7
5
  # Service bus is the central entry point for execution of service within the
8
6
  # domain layer. Unless you have a very good reason, services should not be
@@ -36,15 +34,16 @@ module Rddd
36
34
  #
37
35
  # Execute the given service.
38
36
  #
39
- # @param {Symbol} Service to be executed.
40
- # @param {Hash} Attributes to be passed to the service call.
41
- # @param {Block} Optional error callback block.
37
+ # @param [Symbol] service to be executed.
38
+ # @param [Hash] attributes to be passed to the service call.
39
+ # @param [Block] optional error callback block.
42
40
  #
43
41
  def execute_service(service_name, attributes = {})
44
- raise InvalidService unless service = build_service(service_name, attributes)
42
+ service = build_service(service_name, attributes)
45
43
 
46
44
  unless service.valid?
47
- yield(service.errors) and return if block_given?
45
+ yield(service.errors) if block_given?
46
+ return
48
47
  end
49
48
 
50
49
  service.execute
@@ -2,14 +2,20 @@ require 'rddd/configuration'
2
2
 
3
3
  module Rddd
4
4
  class ServiceFactory
5
- ServiceCreatorNotGiven = Class.new(RuntimeError)
5
+ CreatorNotGiven = Class.new(RuntimeError)
6
+
7
+ InvalidService = Class.new(RuntimeError)
6
8
 
7
9
  def self.build(name, attributes)
8
10
  creator = Configuration.instance.service_creator
9
11
 
10
- raise ServiceCreatorNotGiven unless creator
12
+ raise CreatorNotGiven unless creator
11
13
 
12
- creator.call(name).new(attributes)
14
+ begin
15
+ creator.call(name).new(attributes)
16
+ rescue
17
+ raise Rddd::ServiceFactory::InvalidService
18
+ end
13
19
  end
14
20
  end
15
21
  end
@@ -1,3 +1,3 @@
1
1
  module Rddd
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -32,16 +32,36 @@ class ProjectsController
32
32
  end
33
33
 
34
34
  describe 'CreateProject' do
35
- it 'should call project save' do
35
+ let(:repository_creator) do
36
+ lambda do |name|
37
+ class_name = "#{name.to_s.camel_case}Repository"
38
+ Object.const_get(class_name.to_sym)
39
+ end
40
+ end
41
+
42
+ before do
36
43
  Rddd.configure do |config|
44
+ config.repository_creator = repository_creator
45
+
37
46
  config.service_creator = lambda do |name|
38
47
  class_name = "#{name.to_s.camel_case}Service"
39
48
  Object.const_get(class_name.to_sym)
40
49
  end
41
50
  end
42
-
51
+ end
52
+
53
+ it 'should call project save' do
43
54
  ProjectRepository.any_instance.expects(:create)
44
55
 
45
56
  ProjectsController.new.create(:id => 1, :name => 'Rddd')
46
- end
57
+ end
58
+ end
59
+
60
+ describe 'NotExistingService' do
61
+ it 'should raise' do
62
+ controller = Object.new
63
+ controller.extend Rddd::ServiceBus
64
+
65
+ expect { controller.execute_service(:foo) }.to raise_exception Rddd::ServiceFactory::InvalidService
66
+ end
47
67
  end
@@ -1,34 +1,46 @@
1
1
  require 'spec_helper'
2
- require 'rddd/entity'
3
2
  require 'rddd/repository_factory'
4
3
 
5
4
  describe Rddd::RepositoryFactory do
6
- describe '.build' do
7
- subject { Rddd::RepositoryFactory.build(Rddd::Entity) }
5
+ let(:project) { stub('project', :name => 'Foo', :class => stub(:name => 'Foo')) }
8
6
 
9
- context 'with existing repository' do
10
- before do
11
- Rddd.const_set(:Repositories, Module.new)
12
- Rddd::Repositories.const_set(:EntityRepository, Class.new)
7
+ let(:repository_creator) do
8
+ lambda do |clazz|
9
+ class_name = "#{clazz.name.to_s.camel_case}Repository"
10
+ Rddd::Repositories.const_get(class_name.to_sym)
11
+ end
12
+ end
13
13
 
14
- Rddd.configure { |config| config.repositories_namespace = Rddd::Repositories }
14
+ before do
15
+ Rddd.const_set(:Repositories, Module.new)
16
+ Rddd::Repositories.const_set(:FooRepository, Class.new)
17
+ end
18
+
19
+ after do
20
+ Rddd::Repositories.class_eval { remove_const(:FooRepository) }
21
+ Rddd.class_eval { remove_const(:Repositories) }
22
+ end
23
+
24
+ describe '.build' do
25
+ context 'configuration repository_creator given' do
26
+ before do
27
+ Rddd.configure { |config| config.repository_creator = repository_creator }
15
28
  end
16
-
17
- after do
18
- Rddd::Repositories.class_eval {remove_const(:EntityRepository)}
19
- Rddd.class_eval {remove_const(:Repositories)}
20
29
 
21
- Rddd.configure { |config| config.repositories_namespace = Object }
30
+ after do
31
+ Rddd.configure { |config| config.repository_creator = nil }
22
32
  end
23
33
 
24
- it 'should return instance of repository' do
25
- should be_kind_of Rddd::Repositories::EntityRepository
34
+ it 'should call the appropriate service' do
35
+ Rddd::Repositories::FooRepository.expects(:new)
36
+
37
+ Rddd::RepositoryFactory.build(project.class)
26
38
  end
27
39
  end
28
40
 
29
- context 'with not existing repository' do
30
- it 'should raise NotExistingRepository' do
31
- lambda { subject }.should raise_exception Rddd::NotExistingRepository
41
+ context 'configuration repository_creator not given' do
42
+ it 'should raise exception' do
43
+ expect { Rddd::RepositoryFactory.build(project.class) }.to raise_exception Rddd::RepositoryFactory::CreatorNotGiven
32
44
  end
33
45
  end
34
46
  end
@@ -2,4 +2,25 @@ require 'spec_helper'
2
2
  require 'rddd/repository'
3
3
 
4
4
  describe Rddd::Repository do
5
+ let(:repository) { Rddd::Repository.new }
6
+
7
+ let(:aggregate_root) { stub('aggregate_root') }
8
+
9
+ describe '#create' do
10
+ subject { repository.create(aggregate_root) }
11
+
12
+ it { expect { subject }.to raise_exception NotImplementedError }
13
+ end
14
+
15
+ describe '#update' do
16
+ subject { repository.update(aggregate_root) }
17
+
18
+ it { expect { subject }.to raise_exception NotImplementedError }
19
+ end
20
+
21
+ describe '#delete' do
22
+ subject { repository.delete(aggregate_root) }
23
+
24
+ it { expect { subject }.to raise_exception NotImplementedError }
25
+ end
5
26
  end
@@ -22,15 +22,6 @@ describe Rddd::ServiceBus do
22
22
  end
23
23
  end
24
24
 
25
- context 'not-existing service' do
26
- it do
27
- Rddd::ServiceFactory.expects(:build).with(:foo, attributes).returns(nil)
28
- service.expects(:execute).never
29
-
30
- lambda { subject }.should raise_exception Rddd::InvalidService
31
- end
32
- end
33
-
34
25
  context 'not valid call to service' do
35
26
  context 'without block' do
36
27
  before { service.stubs(:valid?).returns(false) }
@@ -38,9 +38,21 @@ describe Rddd::ServiceFactory do
38
38
  end
39
39
  end
40
40
 
41
+ context 'not existing service' do
42
+ before do
43
+ Rddd.configure { |config| config.service_creator = service_creator }
44
+ end
45
+
46
+ after do
47
+ Rddd.configure { |config| config.service_creator = nil }
48
+ end
49
+
50
+ it { expect { Rddd::ServiceFactory.build(:foo, attributes) }.to raise_exception Rddd::ServiceFactory::InvalidService }
51
+ end
52
+
41
53
  context 'configuration service_creator not given' do
42
54
  it 'should raise exception' do
43
- expect { Rddd::ServiceFactory.build(:create_project, attributes) }.to raise_exception Rddd::ServiceFactory::ServiceCreatorNotGiven
55
+ expect { Rddd::ServiceFactory.build(:create_project, attributes) }.to raise_exception Rddd::ServiceFactory::CreatorNotGiven
44
56
  end
45
57
  end
46
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rddd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
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: 2012-12-16 00:00:00.000000000 Z
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby DDD framework
15
15
  email:
@@ -18,6 +18,7 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - .DS_Store
21
22
  - .gitignore
22
23
  - .rspec
23
24
  - .rvmrc
@@ -96,3 +97,4 @@ test_files:
96
97
  - spec/lib/service_factory_spec.rb
97
98
  - spec/lib/service_spec.rb
98
99
  - spec/spec_helper.rb
100
+ has_rdoc: