azeroth 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/Dockerfile +1 -1
  4. data/README.md +1 -1
  5. data/azeroth.gemspec +2 -2
  6. data/config/yardstick.yml +1 -0
  7. data/lib/azeroth.rb +4 -1
  8. data/lib/azeroth/request_handler.rb +93 -0
  9. data/lib/azeroth/request_handler/create.rb +26 -0
  10. data/lib/azeroth/request_handler/destroy.rb +22 -0
  11. data/lib/azeroth/request_handler/edit.rb +14 -0
  12. data/lib/azeroth/request_handler/index.rb +21 -0
  13. data/lib/azeroth/request_handler/new.rb +25 -0
  14. data/lib/azeroth/request_handler/show.rb +24 -0
  15. data/lib/azeroth/request_handler/update.rb +27 -0
  16. data/lib/azeroth/resourceable.rb +0 -14
  17. data/lib/azeroth/resourceable/builder.rb +0 -8
  18. data/lib/azeroth/routes_builder.rb +14 -38
  19. data/lib/azeroth/version.rb +1 -1
  20. data/spec/controllers/documents_controller_spec.rb +69 -22
  21. data/spec/dummy/db/schema.rb +2 -2
  22. data/spec/lib/azeroth/request_handler/create_spec.rb +27 -0
  23. data/spec/lib/azeroth/request_handler/destroy_spec.rb +20 -0
  24. data/spec/lib/azeroth/request_handler/edit_spec.rb +13 -0
  25. data/spec/lib/azeroth/request_handler/index_spec.rb +12 -0
  26. data/spec/lib/azeroth/request_handler/new_spec.rb +12 -0
  27. data/spec/lib/azeroth/request_handler/show_spec.rb +13 -0
  28. data/spec/lib/azeroth/request_handler/update_spec.rb +32 -0
  29. data/spec/lib/azeroth/request_handler_spec.rb +68 -0
  30. data/spec/lib/azeroth/resourceable/builder_spec.rb +1 -3
  31. data/spec/lib/azeroth/routes_builder_spec.rb +36 -8
  32. data/spec/support/app/controllers/request_handler_controller.rb +15 -0
  33. data/spec/support/shared_examples/request_handler.rb +53 -0
  34. metadata +30 -9
  35. data/lib/azeroth/resource_route_builder.rb +0 -124
  36. data/spec/lib/azeroth/resource_route_builder_spec.rb +0 -22
  37. data/spec/support/app/controllers/routes_builder_controller.rb +0 -23
  38. data/spec/support/shared_examples/resource_routes.rb +0 -90
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe08d469b1d7f7de1b5e506ecfb28bf25d0529f12cb0ed44e738373685ff2f86
4
- data.tar.gz: f947cca246ca569dfad592f4a25d806bdc32f4c5071a409719590828f2fb0b36
3
+ metadata.gz: afd378d756eaf524cab794de457c654237f287bee671be9e6409702528695247
4
+ data.tar.gz: 6dabd7110587bb3df0dcf7239a179ea1ceaee017692262f2366a2ac8b73fe0fc
5
5
  SHA512:
6
- metadata.gz: ffd2677abcd53391963db5e8d4fb40cf35f3c0d86ad41a82bea3b9149a894bd6d8c7737e499c75cbb410106611f1c1c897c174069b9f5e74c8081f72e47fd258
7
- data.tar.gz: 998012e33fe8abc53f81633a85d54181b4db805bb578b619b97c2a3974f1509ee06c260e0ef81e728b4965a9457bb805237cf711b334faded65ce399a25d07e3
6
+ metadata.gz: b434a935b247bce958b72abefc6dc82063df57c9ad331e3b087c246e95cebea9dc2a619964081c54c65fe5c2775b62f5fac1b58ed4122a3776850922b24096b7
7
+ data.tar.gz: d12697602efd61c4932b8fddb3e6b83b273dc51c441b4f9c5a1cf07a5325933e6b3c0629f594b06921d06a8cd4f37998f75ccedbf39c3a4950f7d9353fc3bfaf
data/.circleci/config.yml CHANGED
@@ -2,7 +2,7 @@ version: 2
2
2
  jobs:
3
3
  build:
4
4
  docker:
5
- - image: darthjee/circleci_ruby_gems:0.2.3
5
+ - image: darthjee/circleci_rails_gems:0.2.3
6
6
  environment:
7
7
  PROJECT: azeroth
8
8
  steps:
data/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM darthjee/ruby_gems:0.2.3 as base
1
+ FROM darthjee/rails_gems:0.2.3 as base
2
2
  FROM darthjee/scripts:0.1.2 as scripts
3
3
 
4
4
  ######################################
data/README.md CHANGED
@@ -9,4 +9,4 @@ Azeroth
9
9
 
10
10
  Yard Documentation
11
11
  -------------------
12
- https://www.rubydoc.info/gems/azeroth/0.0.7
12
+ https://www.rubydoc.info/gems/azeroth/0.1.0
data/azeroth.gemspec CHANGED
@@ -18,9 +18,9 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ['lib']
20
20
 
21
- gem.add_runtime_dependency 'activesupport', '~> 5.2.0'
21
+ gem.add_runtime_dependency 'activesupport', '~> 5.2.0'
22
22
  gem.add_runtime_dependency 'darthjee-active_ext', '>= 1.3.2'
23
- gem.add_runtime_dependency 'sinclair', '>= 1.5.2'
23
+ gem.add_runtime_dependency 'sinclair', '>= 1.5.2'
24
24
 
25
25
  gem.add_development_dependency 'actionpack', '~> 5.x'
26
26
  gem.add_development_dependency 'activerecord', '~> 5.x'
data/config/yardstick.yml CHANGED
@@ -24,6 +24,7 @@ rules:
24
24
  exclude:
25
25
  - Azeroth::Decorator#initialize
26
26
  - Azeroth::Model#initialize
27
+ - Azeroth::RequestHandler#initialize
27
28
  - Azeroth::ResourceBuilder#initialize
28
29
  - Azeroth::ResourceRouteBuilder#initialize
29
30
  - Azeroth::Resourceable::Builder#initialize
data/lib/azeroth.rb CHANGED
@@ -5,12 +5,15 @@
5
5
  # Lib for easily developing controllers
6
6
  #
7
7
  # @see Resourceable
8
+
9
+ require 'sinclair'
10
+
8
11
  module Azeroth
9
12
  autoload :Decorator, 'azeroth/decorator'
10
13
  autoload :Model, 'azeroth/model'
14
+ autoload :RequestHandler, 'azeroth/request_handler'
11
15
  autoload :Resourceable, 'azeroth/resourceable'
12
16
  autoload :ResourceBuilder, 'azeroth/resource_builder'
13
- autoload :ResourceRouteBuilder, 'azeroth/resource_route_builder'
14
17
  autoload :RoutesBuilder, 'azeroth/routes_builder'
15
18
  autoload :Options, 'azeroth/options'
16
19
  end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Azeroth
4
+ # @api private
5
+ #
6
+ # Handle a request on behalf of the controller
7
+ #
8
+ # Request handler sends messages to the controller
9
+ # in order to get the resource and rendering the response
10
+ class RequestHandler
11
+ autoload :Create, 'azeroth/request_handler/create'
12
+ autoload :Destroy, 'azeroth/request_handler/destroy'
13
+ autoload :Edit, 'azeroth/request_handler/edit'
14
+ autoload :Index, 'azeroth/request_handler/index'
15
+ autoload :New, 'azeroth/request_handler/new'
16
+ autoload :Show, 'azeroth/request_handler/show'
17
+ autoload :Update, 'azeroth/request_handler/update'
18
+
19
+ # @param controller [ApplicationController]
20
+ # @param model [Azeroth::Model]
21
+ def initialize(controller, model)
22
+ @controller = controller
23
+ @model = model
24
+ end
25
+
26
+ # process the request
27
+ #
28
+ # No action is performd when format is HTML
29
+ #
30
+ # When format is json, the resource is fetched/processed
31
+ # (by the subclass) and returned as json (decorated)
32
+ #
33
+ # @return [String]
34
+ def process
35
+ return unless json?
36
+
37
+ json = model.decorate(resource)
38
+
39
+ controller.instance_eval do
40
+ render(json: json)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :controller, :model
47
+ # @method controller
48
+ # @api private
49
+ # @private
50
+ #
51
+ # Controller to receive the message
52
+ #
53
+ # @return [ApplicationController]
54
+
55
+ # @method model
56
+ # @api private
57
+ # @private
58
+ #
59
+ # Model interface
60
+ #
61
+ # @return [Azeroth::Model]
62
+
63
+ delegate :params, to: :controller
64
+ # @method params
65
+ # @api private
66
+ # @private
67
+ #
68
+ # request parameters
69
+ #
70
+ # @return [ActionController::Parameters]
71
+
72
+ # @private
73
+ #
74
+ # Checks if request format is json
75
+ #
76
+ # @return [TrueClass,FalseClass]
77
+ def json?
78
+ params[:format]&.to_s == 'json'
79
+ end
80
+
81
+ # @private
82
+ #
83
+ # Resource to be serialized and returned
84
+ #
85
+ # Must be implemented in subclass
86
+ #
87
+ # @return [Object]
88
+ # @raise Not implmented
89
+ def resource
90
+ raise 'must be implemented in subclass'
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Azeroth
4
+ class RequestHandler
5
+ # @api private
6
+ #
7
+ # hadler for requests to create resources
8
+ class Create < RequestHandler
9
+ private
10
+
11
+ # @private
12
+ #
13
+ # Creates and return an instance of the model
14
+ #
15
+ # creation uses the method +"#{model.name}_params"+ to
16
+ # fetch all allowed attributes for creation
17
+ #
18
+ # @return [Object]
19
+ def resource
20
+ attributes = controller.send("#{model.name}_params")
21
+ collection = controller.send(model.plural)
22
+ collection.create(attributes)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Azeroth
4
+ class RequestHandler
5
+ # @api private
6
+ #
7
+ # hadler for requests to destroy resources
8
+ class Destroy < RequestHandler
9
+ private
10
+
11
+ # @private
12
+ #
13
+ # Destroy and return an instance of the model
14
+ #
15
+ # @return [Object]
16
+ def resource
17
+ resource = controller.send(model.name)
18
+ resource.tap(&:destroy)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Azeroth
4
+ class RequestHandler
5
+ # @api private
6
+ #
7
+ # hadler for requests to edit resources
8
+ #
9
+ # This handler does the same as {Show}
10
+ # returning the model for editing
11
+ class Edit < RequestHandler::Show
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Azeroth
4
+ class RequestHandler
5
+ # @api private
6
+ #
7
+ # hadler for requests to list resources
8
+ class Index < RequestHandler
9
+ private
10
+
11
+ # @private
12
+ #
13
+ # return a collection of the model
14
+ #
15
+ # @return [Enumerable<Object>]
16
+ def resource
17
+ controller.send(model.plural)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Azeroth
4
+ class RequestHandler
5
+ # @api private
6
+ #
7
+ # hadler for requests for new resources
8
+ class New < RequestHandler
9
+ private
10
+
11
+ # @private
12
+ #
13
+ # returns a new empty instance of the model
14
+ #
15
+ # the initialization of the model uses the collection
16
+ # so that any attribute related to the collection
17
+ # will be present
18
+ #
19
+ # @return [Object]
20
+ def resource
21
+ controller.send(model.plural).new
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Azeroth
4
+ class RequestHandler
5
+ # @api private
6
+ #
7
+ # hadler for requests to show a resource
8
+ class Show < RequestHandler
9
+ private
10
+
11
+ # @private
12
+ #
13
+ # Finds and return an instance of the model
14
+ #
15
+ # finding happens through calling the controller
16
+ # method +"#{model.name}"+
17
+ #
18
+ # @return [Object]
19
+ def resource
20
+ controller.send(model.name)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Azeroth
4
+ class RequestHandler
5
+ # @api private
6
+ #
7
+ # hadler for requests to update resource
8
+ class Update < RequestHandler
9
+ private
10
+
11
+ # @private
12
+ #
13
+ # Updates and return an instance of the model
14
+ #
15
+ # update uses the method +"#{model.name}_params"+ to
16
+ # fetch all allowed attributes for update
17
+ #
18
+ # @return [Object]
19
+ def resource
20
+ attributes = controller.send("#{model.name}_params")
21
+ resource = controller.send(model.name)
22
+ resource.update(attributes)
23
+ resource
24
+ end
25
+ end
26
+ end
27
+ end
@@ -19,20 +19,6 @@ module Azeroth
19
19
 
20
20
  private
21
21
 
22
- # @api private
23
- # @private
24
- #
25
- # Perform rendering of an action based on the requested format
26
- #
27
- # @return [String]
28
- def render_basic
29
- action = params[:action]
30
- respond_to do |format|
31
- format.json { render json: send("#{action}_resource") }
32
- format.html { action }
33
- end
34
- end
35
-
36
22
  # @api private
37
23
  # @private
38
24
  #
@@ -24,7 +24,6 @@ module Azeroth
24
24
 
25
25
  add_params
26
26
  add_resource
27
- add_resource_for_routes
28
27
  add_routes
29
28
  end
30
29
 
@@ -121,13 +120,6 @@ module Azeroth
121
120
  ResourceBuilder.new(model, builder).append
122
121
  end
123
122
 
124
- # Add the routes resource methods
125
- #
126
- # @return [Array<Sinclair::MethodDefinition>]
127
- def add_resource_for_routes
128
- ResourceRouteBuilder.new(model, builder).append
129
- end
130
-
131
123
  # Add metohods for each route
132
124
  #
133
125
  # @return [Array<Sinclair::MethodDefinition>]
@@ -19,14 +19,11 @@ module Azeroth
19
19
  #
20
20
  # @return [Array<Sinclair::MethodDefinition>]
21
21
  def append
22
- %i[index show edit].each do |route|
23
- add_method(route, 'render_basic')
24
- end
22
+ actions = %i[create destroy edit index new show update]
25
23
 
26
- add_method(:new, '')
27
- add_method(:update, update_code)
28
- add_method(:create, create_code)
29
- add_method(:destroy, destroy_code)
24
+ actions.each do |route|
25
+ add_method(route, &route_code(route))
26
+ end
30
27
  end
31
28
 
32
29
  private
@@ -57,38 +54,17 @@ module Azeroth
57
54
  #
58
55
  # @return [Array<Sinclair::MethodDefinition>]
59
56
 
60
- # @private
61
- #
62
- # Method code to update route
63
- #
64
- # @return [String]
65
- def update_code
66
- <<-RUBY
67
- render json: update_resource
68
- RUBY
69
- end
57
+ def route_code(route)
58
+ model_interface = model
59
+ handler_class = Azeroth::RequestHandler.const_get(
60
+ route.to_s.capitalize
61
+ )
70
62
 
71
- # @private
72
- #
73
- # Method code to create route
74
- #
75
- # @return [String]
76
- def create_code
77
- <<-RUBY
78
- render json: create_resource
79
- RUBY
80
- end
81
-
82
- # @private
83
- #
84
- # Method code to destroy route
85
- #
86
- # @return [String]
87
- def destroy_code
88
- <<-RUBY
89
- #{model.name}.destroy
90
- head :no_content
91
- RUBY
63
+ proc do
64
+ handler_class.new(
65
+ self, model_interface
66
+ ).process
67
+ end
92
68
  end
93
69
  end
94
70
  end