rddd 0.1.7 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.DS_Store +0 -0
- data/lib/rddd/authorization/rule.rb +3 -2
- data/lib/rddd/configuration.rb +3 -3
- data/lib/rddd/repository.rb +11 -0
- data/lib/rddd/repository_factory.rb +6 -3
- data/lib/rddd/service.rb +19 -11
- data/lib/rddd/service_bus.rb +6 -7
- data/lib/rddd/service_factory.rb +9 -3
- data/lib/rddd/version.rb +1 -1
- data/spec/integration_spec.rb +23 -3
- data/spec/lib/repository_factory_spec.rb +30 -18
- data/spec/lib/repository_spec.rb +21 -0
- data/spec/lib/service_bus_spec.rb +0 -9
- data/spec/lib/service_factory_spec.rb +13 -1
- metadata +4 -2
data/.DS_Store
ADDED
Binary file
|
@@ -11,8 +11,8 @@ module Rddd
|
|
11
11
|
#
|
12
12
|
# Initialize.
|
13
13
|
#
|
14
|
-
# @param
|
15
|
-
# @param
|
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
|
data/lib/rddd/configuration.rb
CHANGED
@@ -8,14 +8,14 @@ module Rddd
|
|
8
8
|
class Configuration
|
9
9
|
include Singleton
|
10
10
|
|
11
|
-
attr_writer :service_creator, :
|
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
|
18
|
-
@
|
17
|
+
def repository_creator
|
18
|
+
@repository_creator
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/rddd/repository.rb
CHANGED
@@ -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
|
-
|
11
|
-
|
12
|
+
creator = Configuration.instance.repository_creator
|
13
|
+
|
14
|
+
raise CreatorNotGiven unless creator
|
12
15
|
|
13
16
|
begin
|
14
|
-
repository =
|
17
|
+
repository = creator.call(clazz)
|
15
18
|
rescue
|
16
19
|
raise NotExistingRepository unless repository
|
17
20
|
end
|
data/lib/rddd/service.rb
CHANGED
@@ -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.
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
22
|
+
def valid?
|
23
|
+
true
|
24
|
+
end
|
18
25
|
|
19
|
-
|
20
|
-
|
26
|
+
def execute
|
27
|
+
raise NotImplementedError
|
28
|
+
end
|
21
29
|
end
|
22
30
|
end
|
data/lib/rddd/service_bus.rb
CHANGED
@@ -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
|
40
|
-
# @param
|
41
|
-
# @param
|
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
|
-
|
42
|
+
service = build_service(service_name, attributes)
|
45
43
|
|
46
44
|
unless service.valid?
|
47
|
-
yield(service.errors)
|
45
|
+
yield(service.errors) if block_given?
|
46
|
+
return
|
48
47
|
end
|
49
48
|
|
50
49
|
service.execute
|
data/lib/rddd/service_factory.rb
CHANGED
@@ -2,14 +2,20 @@ require 'rddd/configuration'
|
|
2
2
|
|
3
3
|
module Rddd
|
4
4
|
class ServiceFactory
|
5
|
-
|
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
|
12
|
+
raise CreatorNotGiven unless creator
|
11
13
|
|
12
|
-
|
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
|
data/lib/rddd/version.rb
CHANGED
data/spec/integration_spec.rb
CHANGED
@@ -32,16 +32,36 @@ class ProjectsController
|
|
32
32
|
end
|
33
33
|
|
34
34
|
describe 'CreateProject' do
|
35
|
-
|
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
|
-
|
7
|
-
subject { Rddd::RepositoryFactory.build(Rddd::Entity) }
|
5
|
+
let(:project) { stub('project', :name => 'Foo', :class => stub(:name => 'Foo')) }
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
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
|
-
|
30
|
+
after do
|
31
|
+
Rddd.configure { |config| config.repository_creator = nil }
|
22
32
|
end
|
23
33
|
|
24
|
-
it 'should
|
25
|
-
|
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 '
|
30
|
-
it 'should raise
|
31
|
-
|
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
|
data/spec/lib/repository_spec.rb
CHANGED
@@ -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::
|
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.
|
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-
|
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:
|