azeroth 0.6.4 → 0.7.3

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
  SHA256:
3
- metadata.gz: ac2c03e9a7b51ed50db9ecab43a4f7257d98839126f79a45cc6b3563efd7b296
4
- data.tar.gz: 95d18220fae24c2f049cf186fe25a0d5cdc94bada48ccd9ce02b867c5334f3ce
3
+ metadata.gz: 51518b95de11772f06c52d5000e7fd2a4d888dc8d22402e978ba5eb90da2eb75
4
+ data.tar.gz: 363de647988cec21c1cb741d4cbca29801676291010cb8316a480dc6db52108e
5
5
  SHA512:
6
- metadata.gz: 8dd092347ebebde2ccdaad5b2c165a8a595a2f8f2a6fc463303566f1b3812d84b22a0295f75ce9dbf696c752ad045691953696166705e0294f94ae3450b0f3e7
7
- data.tar.gz: 4a406a9e2d4609034cab48bd8aff6ead1c644f3535d3cacd6e18523881572dde035afccbc4279ee96f9a110a0b078897641ebcfc5008a93cf47b7b4ce89bd746
6
+ metadata.gz: 695b44810f4cbc23c71ceb40cfcce8bd657d63cb30aaac9346328c1b3a0d92bafc27f0206caa0bc5f7d044eb6641a27f92fde3f1216e438afff21e584d6d6c45
7
+ data.tar.gz: 20b4768481d46c1210e75eea1c6fa6552dede748f871fe402af864fe1bc855dd9b3196550966a01c672b5a7891a810cc29c2b4d1e179c2c289b97169225553e5
data/.circleci/config.yml CHANGED
@@ -1,8 +1,24 @@
1
1
  version: 2
2
+ workflows:
3
+ version: 2
4
+ test-and-build:
5
+ jobs:
6
+ - test:
7
+ filters:
8
+ tags:
9
+ only: /.*/
10
+ - build-and-release:
11
+ requires: [test]
12
+ filters:
13
+ tags:
14
+ only: /\d+\.\d+\.\d+/
15
+ branches:
16
+ only:
17
+ - master
2
18
  jobs:
3
- build:
19
+ test:
4
20
  docker:
5
- - image: darthjee/circleci_rails_gems:0.5.1
21
+ - image: darthjee/circleci_rails_gems:0.6.3
6
22
  environment:
7
23
  PROJECT: azeroth
8
24
  steps:
@@ -34,3 +50,22 @@ jobs:
34
50
  - run:
35
51
  name: Check unit tests
36
52
  command: check_specs
53
+ build-and-release:
54
+ docker:
55
+ - image: darthjee/circleci_rails_gems:0.6.3
56
+ environment:
57
+ PROJECT: azeroth
58
+ steps:
59
+ - checkout
60
+ - run:
61
+ name: Bundle Install
62
+ command: bundle install
63
+ - run:
64
+ name: Signin
65
+ command: build_gem.sh signin
66
+ - run:
67
+ name: Build Gem
68
+ command: build_gem.sh build
69
+ - run:
70
+ name: Push Gem
71
+ command: build_gem.sh push
data/Dockerfile CHANGED
@@ -1,5 +1,5 @@
1
- FROM darthjee/rails_gems:0.5.2 as base
2
- FROM darthjee/scripts:0.1.7 as scripts
1
+ FROM darthjee/rails_gems:0.6.3 as base
2
+ FROM darthjee/scripts:0.1.8 as scripts
3
3
 
4
4
  ######################################
5
5
 
data/README.md CHANGED
@@ -11,7 +11,7 @@ Azeroth
11
11
 
12
12
  Yard Documentation
13
13
  -------------------
14
- [https://www.rubydoc.info/gems/azeroth/0.6.4](https://www.rubydoc.info/gems/azeroth/0.6.4)
14
+ [https://www.rubydoc.info/gems/azeroth/0.7.3](https://www.rubydoc.info/gems/azeroth/0.7.3)
15
15
 
16
16
  Azeroth has been designed making the coding of controllers easier
17
17
  as routes in controllers are usually copy, paste and replace of same
@@ -25,6 +25,25 @@ does not perform database operations
25
25
  Future versions will enable `html` rendering to also perform
26
26
  database operations.
27
27
 
28
+ Installation
29
+ ---------------
30
+
31
+ - Install it
32
+
33
+ ```ruby
34
+ gem install azeroth
35
+ ```
36
+
37
+ - Or add Sinclair to your `Gemfile` and `bundle install`:
38
+
39
+ ```ruby
40
+ gem 'azeroth'
41
+ ```
42
+
43
+ ```bash
44
+ bundle install azeroth
45
+ ```
46
+
28
47
  Usage
29
48
  -----
30
49
 
@@ -36,9 +55,11 @@ which adds a resource and action methods for `create`, `show`, `index`,
36
55
  `update`, `delete`, `edit`
37
56
 
38
57
  It accepts options
39
- -only List of actions to be built
40
- -except List of actions to not to be built
41
- -decorator Decorator class or flag allowing/disallowing decorators
58
+ - only: List of actions to be built
59
+ - except: List of actions to not to be built
60
+ - decorator: Decorator class or flag allowing/disallowing decorators
61
+ - before_save: Method/Proc to be ran before saving the resource on create or update
62
+ - build_with: Method/Block to be ran when building the reource on create
42
63
 
43
64
  ```ruby
44
65
  # publishers_controller.rb
@@ -76,6 +97,36 @@ It accepts options
76
97
  end
77
98
  ```
78
99
 
100
+ ```ruby
101
+ # pokemons_controller.rb
102
+
103
+ class PokemonsController < ApplicationController
104
+ include Azeroth::Resourceable
105
+
106
+ resource_for :pokemon,
107
+ only: %i[create update],
108
+ before_save: :set_favorite
109
+
110
+ private
111
+
112
+ def set_favorite
113
+ pokemon.favorite = true
114
+ end
115
+
116
+ def pokemons
117
+ master.pokemons
118
+ end
119
+
120
+ def master
121
+ @master ||= PokemonMaster.find(master_id)
122
+ end
123
+
124
+ def master_id
125
+ params.require(:pokemon_master_id)
126
+ end
127
+ end
128
+ ```
129
+
79
130
  ## Azeroth::Decorator
80
131
 
81
132
  [Decorators](https://www.rubydoc.info/gems/azeroth/Azeroth/Decorator) are
@@ -121,6 +172,6 @@ Exposing is done through the class method
121
172
  [expose](https://www.rubydoc.info/gems/azeroth/Azeroth/Decorator#expose-class_method)
122
173
  which accepts several options:
123
174
 
124
- -as: custom key to expose
125
- -if: method/block to be called checking if an attribute should or should not be exposed
126
- -decorator: flag to use or not a decorator or decorator class to be used
175
+ - as: custom key to expose
176
+ - if: method/block to be called checking if an attribute should or should not be exposed
177
+ - decorator: flag to use or not a decorator or decorator class to be used
data/azeroth.gemspec CHANGED
@@ -18,18 +18,19 @@ 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.x'
22
22
  gem.add_runtime_dependency 'darthjee-active_ext', '>= 1.3.2'
23
- gem.add_runtime_dependency 'sinclair', '>= 1.6.4'
23
+ gem.add_runtime_dependency 'jace', '>= 0.0.3'
24
+ gem.add_runtime_dependency 'sinclair', '>= 1.6.5'
24
25
 
25
- gem.add_development_dependency 'actionpack', '5.2.4.2'
26
- gem.add_development_dependency 'activerecord', '5.2.4.2'
26
+ gem.add_development_dependency 'actionpack', '~> 5.2.x'
27
+ gem.add_development_dependency 'activerecord', '~> 5.2.x'
27
28
  gem.add_development_dependency 'bundler', '1.16.1'
28
29
  gem.add_development_dependency 'factory_bot', '5.2.0'
29
- gem.add_development_dependency 'nokogiri', '1.10.9'
30
+ gem.add_development_dependency 'nokogiri', '1.11.3'
30
31
  gem.add_development_dependency 'pry', '0.12.2'
31
32
  gem.add_development_dependency 'pry-nav', '0.3.0'
32
- gem.add_development_dependency 'rails', '5.2.4.2'
33
+ gem.add_development_dependency 'rails', '~> 5.2.x'
33
34
  gem.add_development_dependency 'rails-controller-testing', '1.0.4'
34
35
  gem.add_development_dependency 'rake', '13.0.1'
35
36
  gem.add_development_dependency 'reek', '5.6.0'
data/lib/azeroth.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'sinclair'
4
+ require 'jace'
4
5
 
5
6
  # @api public
6
7
  #
@@ -257,6 +257,8 @@ module Azeroth
257
257
  #
258
258
  # @return [Hash]
259
259
  def as_json(*args)
260
+ return nil if object.nil?
261
+
260
262
  return array_as_json(*args) if enum?
261
263
 
262
264
  HashBuilder.new(self).as_json
data/lib/azeroth/model.rb CHANGED
@@ -62,6 +62,8 @@ module Azeroth
62
62
  #
63
63
  # @return [Azeroth::Options]
64
64
 
65
+ delegate :decorator, to: :options
66
+
65
67
  # @private
66
68
  #
67
69
  # Returns decorator class for the object
@@ -85,7 +87,7 @@ module Azeroth
85
87
  # @return [Azeroth::Decorator,DummyDecorator]
86
88
  def calculate_decorator_class
87
89
  return DummyDecorator unless options.decorator
88
- return options.decorator if options.decorator.is_a?(Class)
90
+ return decorator if decorator.is_a?(Class)
89
91
 
90
92
  klass::Decorator
91
93
  rescue NameError
@@ -10,10 +10,16 @@ module Azeroth
10
10
  # Sinclair::Options
11
11
  class Options < Sinclair::Options
12
12
  # Default options
13
+ #
14
+ # @api public
15
+ #
16
+ # @see Resourceable::ClassMethods#resource_for
13
17
  DEFAULT_OPTIONS = {
14
18
  only: %i[create destroy edit index new show update],
15
19
  except: [],
16
- decorator: true
20
+ decorator: true,
21
+ before_save: nil,
22
+ build_with: nil
17
23
  }.freeze
18
24
 
19
25
  with_options DEFAULT_OPTIONS
@@ -25,6 +31,19 @@ module Azeroth
25
31
  [only].flatten.map(&:to_sym) - [except].flatten.map(&:to_sym)
26
32
  end
27
33
 
34
+ # Returns event dispatcher
35
+ #
36
+ # Event dispatcher is responsible for
37
+ # sending events such as +before_save+
38
+ # to it's correct calling point
39
+ #
40
+ # @return [Jace::Dispatcher]
41
+ def event_dispatcher(event)
42
+ Jace::Dispatcher.new(
43
+ before: try("before_#{event}")
44
+ )
45
+ end
46
+
28
47
  # @method only
29
48
  # @api private
30
49
  #
@@ -50,5 +69,23 @@ module Azeroth
50
69
  # model.as_json
51
70
  #
52
71
  # @return [Decorator,TrueClass,FalseClass]
72
+
73
+ # @method before_save
74
+ # @api private
75
+ #
76
+ # Block or method name to be run before save
77
+ #
78
+ # The given method or block will be ran
79
+ # before committing changes in models
80
+ # to database
81
+ #
82
+ # @return [Symbol,Proc]
83
+
84
+ # @method build_with
85
+ # @api private
86
+ #
87
+ # Block or method name to be ran when building the resource
88
+ #
89
+ # @return [Symbol,Proc]
53
90
  end
54
91
  end
@@ -18,9 +18,10 @@ module Azeroth
18
18
 
19
19
  # @param controller [ApplicationController]
20
20
  # @param model [Azeroth::Model]
21
- def initialize(controller, model)
21
+ def initialize(controller, model, options)
22
22
  @controller = controller
23
23
  @model = model
24
+ @options = options
24
25
  end
25
26
 
26
27
  # process the request
@@ -44,7 +45,7 @@ module Azeroth
44
45
 
45
46
  private
46
47
 
47
- attr_reader :controller, :model
48
+ attr_reader :controller, :model, :options
48
49
  # @method controller
49
50
  # @api private
50
51
  # @private
@@ -61,6 +62,14 @@ module Azeroth
61
62
  #
62
63
  # @return [Azeroth::Model]
63
64
 
65
+ # @method options
66
+ # @api private
67
+ # @private
68
+ #
69
+ # Handling options
70
+ #
71
+ # @return [Azeroth::Options]
72
+
64
73
  delegate :params, to: :controller
65
74
  # @method params
66
75
  # @api private
@@ -104,5 +113,33 @@ module Azeroth
104
113
  def status
105
114
  :ok
106
115
  end
116
+
117
+ # @private
118
+ #
119
+ # Run a block triggering the event
120
+ #
121
+ # @return [Object] Result of given block
122
+ def trigger_event(event, &block)
123
+ options.event_dispatcher(event)
124
+ .dispatch(controller, &block)
125
+ end
126
+
127
+ # @private
128
+ #
129
+ # Attributes to be used on resource creating
130
+ #
131
+ # @return [Hash]
132
+ def attributes
133
+ @attributes ||= controller.send("#{model.name}_params")
134
+ end
135
+
136
+ # @private
137
+ #
138
+ # Collection scope of the resource
139
+ #
140
+ # @return [ActiveRecord::Relation]
141
+ def collection
142
+ @collection = controller.send(model.plural)
143
+ end
107
144
  end
108
145
  end
@@ -8,6 +8,8 @@ module Azeroth
8
8
  class Create < RequestHandler
9
9
  private
10
10
 
11
+ delegate :build_with, to: :options
12
+
11
13
  # @private
12
14
  #
13
15
  # Creates and return an instance of the model
@@ -17,16 +19,40 @@ module Azeroth
17
19
  #
18
20
  # @return [Object]
19
21
  def resource
20
- @resource ||= build_resource
22
+ @resource ||= build_and_save_resource
21
23
  end
22
24
 
23
- # build resource for create
25
+ # @private
26
+ #
27
+ # build resource for create and save it
24
28
  #
25
29
  # @return [Object]
30
+ def build_and_save_resource
31
+ @resource = build_resource
32
+ controller.instance_variable_set("@#{model.name}", resource)
33
+
34
+ trigger_event(:save) do
35
+ resource.tap(&:save)
36
+ end
37
+ end
38
+
39
+ # @private
40
+ #
41
+ # build resource without saving it
42
+ #
43
+ # when +build_with+ option is given, the proc/method
44
+ # is called instead of collection.build
45
+ #
46
+ # @return [Object] resource built
26
47
  def build_resource
27
- attributes = controller.send("#{model.name}_params")
28
- collection = controller.send(model.plural)
29
- collection.create(attributes)
48
+ return collection.build(attributes) unless build_with
49
+
50
+ case build_with
51
+ when Proc
52
+ controller.instance_eval(&build_with)
53
+ else
54
+ controller.send(build_with)
55
+ end
30
56
  end
31
57
 
32
58
  # @private
@@ -24,10 +24,10 @@ module Azeroth
24
24
  #
25
25
  # @return [Object]
26
26
  def update_resource
27
- attributes = controller.send("#{model.name}_params")
28
-
29
27
  controller.send(model.name).tap do |entry|
30
- entry.update(attributes)
28
+ trigger_event(:save) do
29
+ entry.update(attributes)
30
+ end
31
31
  end
32
32
  end
33
33
 
@@ -21,6 +21,29 @@ module Azeroth
21
21
  autoload :Builder, 'azeroth/resourceable/builder'
22
22
  autoload :ClassMethods, 'azeroth/resourceable/class_methods'
23
23
 
24
+ class << self
25
+ # @method self.resource_for(name, **options)
26
+ # @api public
27
+ #
28
+ # @param name [String, Symbol] Name of the resource
29
+ # @param options [Hash] resource building options
30
+ # @option options only [Array<Symbol,String>] List of
31
+ # actions to be built
32
+ # @option options except [Array<Symbol,String>] List of
33
+ # actions to not to be built
34
+ # @option options decorator [Azeroth::Decorator,TrueClass,FalseClass]
35
+ # Decorator class or flag allowing/disallowing decorators
36
+ # @option options before_save [Symbol,Proc] method/block
37
+ # to be ran on the controller before saving the resource
38
+ # @option options build_with [Symbol,Proc] method/block
39
+ # to be ran when building resource
40
+ # (default proc { <resource_collection>.build(resource_params) }
41
+ #
42
+ # @return [Array<MethodDefinition>] list of methods created
43
+ #
44
+ # @see Options::DEFAULT_OPTIONS
45
+ end
46
+
24
47
  private
25
48
 
26
49
  # @api private