ddc 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3a6ee066211a5a1664cda8df754f8dd14d6bc07
4
- data.tar.gz: 23a9ecdcb2c841d6b78e51e1dce499f90197e2ea
3
+ metadata.gz: beb0a92f622097983064f83fe5b0cb85a4e10361
4
+ data.tar.gz: e1ae80d57029d1b62605d28313555e597138c7f9
5
5
  SHA512:
6
- metadata.gz: dffde5c7259d6cc663a5efcb354b4a4549009a6ea360fedeaf58ff48f4e7a0dd17700deb71fa124e5770bd9e9550cb925c575329d111407abc9501cfce4db85a
7
- data.tar.gz: fb195d4f69292e9481af00a52d03fe8e2cb8c56cfd4f9fdc713397512448b154e80b6fbea2af0704fdef5a2c803af96760fb900cfa26c7a03751730823888826
6
+ metadata.gz: d8af2e9a626edd66add56d57db5f196c8217eb22f8582894a7263ff435896b2a1477f365ae598853f50bc54f43052727da7da415e735c67d1e7d3c4045f4269c
7
+ data.tar.gz: 0549ba0e5c2f500909b946a286ae511ada100f6a5d6d0f9d2098d472f81b3e1b574bde7c21d7c9eba3dcfbc11db173aed7e67882416a85b070afcec5ef26b4ac
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # DDC Changelog
2
+
3
+ ### 0.1.0
4
+
5
+ * API updates to be more consistent
6
+ * Add ServiceBuilder that delegats to AR Model or XxxxxFinder
7
+
8
+ ### 0.0.1
9
+
10
+ * Initial release
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
7
+ rescue LoadError
8
+ end
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 build_controller(controller_name, config)
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[:params] || DEFAULT_CONTEXT_PARAMS).inject({}) do |h, param|
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
@@ -1,11 +1,13 @@
1
- module ResponseBuilder
2
- def not_found
3
- {status: :not_found}
4
- end
5
- def ok(obj)
6
- {status: :ok, object: obj}
7
- end
8
- def created(obj)
9
- {status: :created, object: obj}
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
@@ -1,19 +1,27 @@
1
1
  module DDC
2
2
  class ServiceBuilder
3
3
  def self.build(model_type)
4
- Class.new do
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.values_at :id
16
- me = self.class.ar_model.where id: id
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 = self.class.ar_model.all
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 find_for_user(user, id)
49
- return nil if id.nil? || !UUIDUtil.valid?(id)
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
@@ -1,3 +1,3 @@
1
1
  module Ddc
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/ddc.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "ddc/version"
2
+ require "ddc/response_builder"
2
3
  require "ddc/controller_builder"
4
+ require "ddc/service_builder"
3
5
 
4
6
  module Ddc
5
7
  # Your code goes here...
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe DDC::ControllerBuilder do
4
4
  subject { described_class }
5
5
 
6
- describe '.build_controller' do
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.build_controller :foo, actions: {
23
+ subject.build :foo, actions: {
24
24
  index: {
25
- params: [:current_user, :params],
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.build_controller :foo, actions: {}}).to raise_exception
35
- expect(->{subject.build_controller :foop, {}}).to raise_exception
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.build_controller :foo, actions: {foo: {
40
- params: [:current_user, :params],
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.build_controller :foo, actions: {foo: {
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.build_controller :foo,
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.build_controller :foo, actions: {
96
+ subject.build :foo, actions: {
97
97
  index: {
98
- params: [:current_user, :params],
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.build_controller :foo, actions: {
116
+ subject.build :foo, actions: {
117
117
  index: {
118
- params: [:current_user, :params],
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.build_controller :foo,
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
@@ -4,5 +4,6 @@ require 'action_controller'
4
4
  class ApplicationController < ActionController::Base
5
5
  end
6
6
 
7
+ require 'pry'
7
8
  require_relative '../lib/ddc'
8
9
 
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.1
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-23 00:00:00.000000000 Z
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: