ddc 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Rakefile +6 -0
- data/ddc.gemspec +1 -0
- data/lib/ddc/controller_builder.rb +4 -4
- data/lib/ddc/response_builder.rb +11 -9
- data/lib/ddc/service_builder.rb +23 -9
- data/lib/ddc/version.rb +1 -1
- data/lib/ddc.rb +2 -0
- data/spec/controller_builder_spec.rb +14 -14
- data/spec/service_builder_spec.rb +80 -0
- data/spec/spec_helper.rb +1 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: beb0a92f622097983064f83fe5b0cb85a4e10361
|
4
|
+
data.tar.gz: e1ae80d57029d1b62605d28313555e597138c7f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8af2e9a626edd66add56d57db5f196c8217eb22f8582894a7263ff435896b2a1477f365ae598853f50bc54f43052727da7da415e735c67d1e7d3c4045f4269c
|
7
|
+
data.tar.gz: 0549ba0e5c2f500909b946a286ae511ada100f6a5d6d0f9d2098d472f81b3e1b574bde7c21d7c9eba3dcfbc11db173aed7e67882416a85b070afcec5ef26b4ac
|
data/CHANGELOG.md
ADDED
data/Rakefile
CHANGED
data/ddc.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "pry"
|
24
25
|
spec.add_dependency "actionpack", "~> 4.1"
|
25
26
|
spec.add_dependency "activesupport", "~> 4.1"
|
26
27
|
|
@@ -9,7 +9,7 @@ module DDC
|
|
9
9
|
error: 500
|
10
10
|
}
|
11
11
|
class << self
|
12
|
-
def
|
12
|
+
def build(controller_name, config)
|
13
13
|
klass = find_or_create_class(controller_name)
|
14
14
|
setup_before_actions!(klass, config)
|
15
15
|
setup_actions!(controller_name, klass, config)
|
@@ -39,18 +39,18 @@ module DDC
|
|
39
39
|
raise "Must specify actions" if actions.blank?
|
40
40
|
|
41
41
|
actions.each do |action, action_desc|
|
42
|
-
setup_action! controller_name, klass, action, action_desc
|
42
|
+
setup_action! controller_name, klass, action, action_desc, config
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
def setup_action!(controller_name, klass, action, action_desc)
|
46
|
+
def setup_action!(controller_name, klass, action, action_desc, config)
|
47
47
|
raise "Must specify a service for each action" unless action_desc[:service].present?
|
48
48
|
raise "Must specify a context for each action" unless action_desc[:context].present?
|
49
49
|
proc_klass, proc_method = parse_class_and_method(action_desc[:service])
|
50
50
|
context_klass, context_method = parse_class_and_method(action_desc[:context])
|
51
51
|
|
52
52
|
klass.send :define_method, action do
|
53
|
-
context_params = (action_desc[:
|
53
|
+
context_params = (action_desc[:context_params] || config[:context_params] || DEFAULT_CONTEXT_PARAMS).inject({}) do |h, param|
|
54
54
|
h[param] = send param
|
55
55
|
h
|
56
56
|
end
|
data/lib/ddc/response_builder.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module DDC
|
2
|
+
module ResponseBuilder
|
3
|
+
def not_found
|
4
|
+
{status: :not_found}
|
5
|
+
end
|
6
|
+
def ok(obj)
|
7
|
+
{status: :ok, object: obj}
|
8
|
+
end
|
9
|
+
def created(obj)
|
10
|
+
{status: :created, object: obj}
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
data/lib/ddc/service_builder.rb
CHANGED
@@ -1,19 +1,27 @@
|
|
1
1
|
module DDC
|
2
2
|
class ServiceBuilder
|
3
3
|
def self.build(model_type)
|
4
|
-
Class.new
|
5
|
-
include ResponseBuilder
|
4
|
+
Class.new do
|
5
|
+
include DDC::ResponseBuilder
|
6
6
|
class << self
|
7
|
-
attr_accessor :model_type, :ar_model
|
7
|
+
attr_accessor :model_type, :ar_model, :finder
|
8
8
|
end
|
9
9
|
|
10
10
|
@model_type = model_type
|
11
11
|
ar_class_name = model_type.to_s.camelize
|
12
12
|
@ar_model = Object.const_get(ar_class_name)
|
13
13
|
|
14
|
+
@finder = nil
|
15
|
+
begin
|
16
|
+
@finder = Object.const_get("#{ar_class_name}Finder")
|
17
|
+
rescue NameError
|
18
|
+
# we use the AR Model as a fallback
|
19
|
+
end
|
20
|
+
|
14
21
|
def find(context)
|
15
|
-
id = context
|
16
|
-
me =
|
22
|
+
id = context[:id]
|
23
|
+
me = custom_finder ? custom_finder.find(context) :
|
24
|
+
ar_model.where(id: id)
|
17
25
|
if me.present?
|
18
26
|
ok(me)
|
19
27
|
else
|
@@ -21,8 +29,9 @@ module DDC
|
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
24
|
-
def find_all(context)
|
25
|
-
mes =
|
32
|
+
def find_all(context={})
|
33
|
+
mes = custom_finder ? custom_finder.find_all(context) :
|
34
|
+
ar_model.all
|
26
35
|
ok(mes)
|
27
36
|
end
|
28
37
|
|
@@ -45,9 +54,14 @@ module DDC
|
|
45
54
|
end
|
46
55
|
|
47
56
|
private
|
48
|
-
def
|
49
|
-
|
57
|
+
def custom_finder
|
58
|
+
self.class.finder
|
50
59
|
end
|
60
|
+
|
61
|
+
def ar_model
|
62
|
+
self.class.ar_model
|
63
|
+
end
|
64
|
+
|
51
65
|
end
|
52
66
|
end
|
53
67
|
end
|
data/lib/ddc/version.rb
CHANGED
data/lib/ddc.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe DDC::ControllerBuilder do
|
4
4
|
subject { described_class }
|
5
5
|
|
6
|
-
describe '.
|
6
|
+
describe '.build' do
|
7
7
|
let(:json_format) {
|
8
8
|
format = double('json format')
|
9
9
|
expect(format).to receive(:json).and_yield
|
@@ -20,9 +20,9 @@ describe DDC::ControllerBuilder do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'defines the controller class' do
|
23
|
-
subject.
|
23
|
+
subject.build :foo, actions: {
|
24
24
|
index: {
|
25
|
-
|
25
|
+
context_params: [:current_user, :params],
|
26
26
|
context: 'foo_context_builder#bar',
|
27
27
|
service: 'baz_service#qux'
|
28
28
|
}
|
@@ -31,19 +31,19 @@ describe DDC::ControllerBuilder do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'raises if there are no actions defined' do
|
34
|
-
expect(->{subject.
|
35
|
-
expect(->{subject.
|
34
|
+
expect(->{subject.build :foo, actions: {}}).to raise_exception
|
35
|
+
expect(->{subject.build :foop, {}}).to raise_exception
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'raises if an action is missing context' do
|
39
|
-
expect(->{subject.
|
40
|
-
|
39
|
+
expect(->{subject.build :foo, actions: {foo: {
|
40
|
+
context_params: [:current_user, :params],
|
41
41
|
service: 'baz_service#qux'
|
42
42
|
}}}).to raise_exception
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'raises if an action is missing service' do
|
46
|
-
expect(->{subject.
|
46
|
+
expect(->{subject.build :foo, actions: {foo: {
|
47
47
|
context: 'foo_context_builder#bar',
|
48
48
|
}}}).to raise_exception
|
49
49
|
end
|
@@ -54,7 +54,7 @@ describe DDC::ControllerBuilder do
|
|
54
54
|
end
|
55
55
|
|
56
56
|
expect(FooController).to receive(:before_action).with(:my_before_action)
|
57
|
-
subject.
|
57
|
+
subject.build :foo,
|
58
58
|
before_actions: [:my_before_action],
|
59
59
|
actions: {
|
60
60
|
index: {
|
@@ -93,9 +93,9 @@ describe DDC::ControllerBuilder do
|
|
93
93
|
{ object: :some_obj, status: :ok }
|
94
94
|
end
|
95
95
|
|
96
|
-
subject.
|
96
|
+
subject.build :foo, actions: {
|
97
97
|
index: {
|
98
|
-
|
98
|
+
context_params: [:current_user, :params],
|
99
99
|
context: 'foo_context_builder#bar',
|
100
100
|
service: 'baz_service#qux'
|
101
101
|
}
|
@@ -113,9 +113,9 @@ describe DDC::ControllerBuilder do
|
|
113
113
|
def respond_to; end
|
114
114
|
end
|
115
115
|
|
116
|
-
subject.
|
116
|
+
subject.build :foo, actions: {
|
117
117
|
index: {
|
118
|
-
|
118
|
+
context_params: [:current_user, :params],
|
119
119
|
context: 'foo_context_builder#bar',
|
120
120
|
service: 'baz_service#qux'
|
121
121
|
}
|
@@ -151,7 +151,7 @@ describe DDC::ControllerBuilder do
|
|
151
151
|
def some_user; end
|
152
152
|
def render(args); end
|
153
153
|
end
|
154
|
-
subject.
|
154
|
+
subject.build :foo,
|
155
155
|
params: [:current_user, :params],
|
156
156
|
actions: {
|
157
157
|
index: {
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DDC::ServiceBuilder do
|
4
|
+
subject { described_class }
|
5
|
+
|
6
|
+
describe '.build' do
|
7
|
+
before do
|
8
|
+
Object.send(:remove_const, :FooFinder) if defined? FooFinder
|
9
|
+
end
|
10
|
+
|
11
|
+
class Foo
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'defines a class that responds to CRUD' do
|
15
|
+
foo_service_klass = subject.build :foo
|
16
|
+
service = foo_service_klass.new
|
17
|
+
expect(service).to respond_to(:find)
|
18
|
+
expect(service).to respond_to(:find_all)
|
19
|
+
expect(service).to respond_to(:update)
|
20
|
+
expect(service).to respond_to(:create)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#find' do
|
24
|
+
it 'uses the AR model by default and returns DDC status hash' do
|
25
|
+
foo_service_klass = subject.build :foo
|
26
|
+
service = foo_service_klass.new
|
27
|
+
|
28
|
+
expect(Foo).to receive(:where).with(id: 'monkey').and_return :myfoo
|
29
|
+
found_data = service.find({id: 'monkey'})
|
30
|
+
expect(found_data[:object]).to eq(:myfoo)
|
31
|
+
expect(found_data[:status]).to eq(:ok)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns a not_found status' do
|
35
|
+
foo_service_klass = subject.build :foo
|
36
|
+
service = foo_service_klass.new
|
37
|
+
|
38
|
+
expect(Foo).to receive(:where).with(id: 'monkey').and_return nil
|
39
|
+
found_data = service.find({id: 'monkey'})
|
40
|
+
expect(found_data[:object]).to eq(nil)
|
41
|
+
expect(found_data[:status]).to eq(:not_found)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'uses the FooFinder if present' do
|
45
|
+
FooFinder = Class.new
|
46
|
+
foo_service_klass = subject.build :foo
|
47
|
+
service = foo_service_klass.new
|
48
|
+
|
49
|
+
expect(FooFinder).to receive(:find).with(id: 'monkey').and_return :myfoo
|
50
|
+
found_data = service.find({id: 'monkey'})
|
51
|
+
expect(found_data[:object]).to eq(:myfoo)
|
52
|
+
expect(found_data[:status]).to eq(:ok)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#find_all' do
|
57
|
+
it 'uses the AR model by default and returns DDC status hash' do
|
58
|
+
foo_service_klass = subject.build :foo
|
59
|
+
service = foo_service_klass.new
|
60
|
+
|
61
|
+
expect(Foo).to receive(:all).and_return :myfoo
|
62
|
+
found_data = service.find_all
|
63
|
+
expect(found_data[:object]).to eq(:myfoo)
|
64
|
+
expect(found_data[:status]).to eq(:ok)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'uses the FooFinder if present' do
|
68
|
+
FooFinder = Class.new
|
69
|
+
foo_service_klass = subject.build :foo
|
70
|
+
service = foo_service_klass.new
|
71
|
+
|
72
|
+
expect(FooFinder).to receive(:find_all).with(user: 'monkey').and_return :myfoo
|
73
|
+
found_data = service.find_all({user: 'monkey'})
|
74
|
+
expect(found_data[:object]).to eq(:myfoo)
|
75
|
+
expect(found_data[:status]).to eq(:ok)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ddc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shawn Anderson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: actionpack
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,6 +102,7 @@ extensions: []
|
|
88
102
|
extra_rdoc_files: []
|
89
103
|
files:
|
90
104
|
- ".gitignore"
|
105
|
+
- CHANGELOG.md
|
91
106
|
- Gemfile
|
92
107
|
- LICENSE.txt
|
93
108
|
- README.md
|
@@ -99,6 +114,7 @@ files:
|
|
99
114
|
- lib/ddc/service_builder.rb
|
100
115
|
- lib/ddc/version.rb
|
101
116
|
- spec/controller_builder_spec.rb
|
117
|
+
- spec/service_builder_spec.rb
|
102
118
|
- spec/spec_helper.rb
|
103
119
|
homepage: ''
|
104
120
|
licenses:
|
@@ -126,5 +142,6 @@ specification_version: 4
|
|
126
142
|
summary: Data Driven Controllers for Rails
|
127
143
|
test_files:
|
128
144
|
- spec/controller_builder_spec.rb
|
145
|
+
- spec/service_builder_spec.rb
|
129
146
|
- spec/spec_helper.rb
|
130
147
|
has_rdoc:
|