azeroth 0.6.5 → 0.7.4

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: 8e28a5c409533eebedc8579f02941b55a956b8296459243b8cf5572857f64d1c
4
- data.tar.gz: ba50b329165868a7b2a65b4762a20c91898c45593bedc528e440d6d51b9fae01
3
+ metadata.gz: 197968637291c04e3ce145d6d5dd9f84451e4b47d104d420aef7c1a5e7b999dc
4
+ data.tar.gz: 9936013b82fcc56048027c6a469130bc15913e095c5373a9b3b50d577d2fcc93
5
5
  SHA512:
6
- metadata.gz: 38ca1d96bf67471f7eb0bdd1c626832e534c9418646d09d2381659096c1739a8d06b592a8c4063e1068f4e2fa8502bc9fcc25f2a9e6c998c659459eea837325c
7
- data.tar.gz: 9cb5e259c5f9961d60d6ae8c50563ada7d45ab2a850f2cf79dfd8977d5b30b53ea9e029617466d19a237c2256db3c6491fe9a674cb463ad074e76b78693852ee
6
+ metadata.gz: d05f185b913c3ac80e58bfad3cdcfea1de68c4836251d5a121826961bf3779adc3e9b39e4dff6d92e72204a4bc8d355d063f0b910c4607dad17f741206a31149
7
+ data.tar.gz: 85e4d4c4625e36fd40358bf486e5bb2dd81fef82cc6fbf198545b6fdad51642d63d3ee5af2e828d0cf394ca0dfbf7aff5efd568634517a1c5236a79901275915
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.7.0 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.5](https://www.rubydoc.info/gems/azeroth/0.6.5)
14
+ [https://www.rubydoc.info/gems/azeroth/0.7.4](https://www.rubydoc.info/gems/azeroth/0.7.4)
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,12 @@ 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
+ - after_save: Method/Proc to be ran after saving the resource on create or update
63
+ - build_with: Method/Block to be ran when building the reource on create
42
64
 
43
65
  ```ruby
44
66
  # publishers_controller.rb
@@ -76,6 +98,36 @@ It accepts options
76
98
  end
77
99
  ```
78
100
 
101
+ ```ruby
102
+ # pokemons_controller.rb
103
+
104
+ class PokemonsController < ApplicationController
105
+ include Azeroth::Resourceable
106
+
107
+ resource_for :pokemon,
108
+ only: %i[create update],
109
+ before_save: :set_favorite
110
+
111
+ private
112
+
113
+ def set_favorite
114
+ pokemon.favorite = true
115
+ end
116
+
117
+ def pokemons
118
+ master.pokemons
119
+ end
120
+
121
+ def master
122
+ @master ||= PokemonMaster.find(master_id)
123
+ end
124
+
125
+ def master_id
126
+ params.require(:pokemon_master_id)
127
+ end
128
+ end
129
+ ```
130
+
79
131
  ## Azeroth::Decorator
80
132
 
81
133
  [Decorators](https://www.rubydoc.info/gems/azeroth/Azeroth/Decorator) are
data/azeroth.gemspec CHANGED
@@ -18,32 +18,33 @@ 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.7'
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.4'
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
- gem.add_development_dependency 'rake', '13.0.1'
35
- gem.add_development_dependency 'reek', '5.6.0'
35
+ gem.add_development_dependency 'rake', '13.0.3'
36
+ gem.add_development_dependency 'reek', '6.0.3'
36
37
  gem.add_development_dependency 'rspec', '3.9.0'
37
- gem.add_development_dependency 'rspec-core', '3.9.1'
38
- gem.add_development_dependency 'rspec-expectations', '3.9.1'
38
+ gem.add_development_dependency 'rspec-core', '3.9.3'
39
+ gem.add_development_dependency 'rspec-expectations', '3.9.4'
39
40
  gem.add_development_dependency 'rspec-mocks', '3.9.1'
40
41
  gem.add_development_dependency 'rspec-rails', '3.9.0'
41
- gem.add_development_dependency 'rspec-support', '3.9.2'
42
+ gem.add_development_dependency 'rspec-support', '3.9.4'
42
43
  gem.add_development_dependency 'rubocop', '0.80.1'
43
44
  gem.add_development_dependency 'rubocop-rspec', '1.38.1'
44
- gem.add_development_dependency 'rubycritic', '4.4.1'
45
+ gem.add_development_dependency 'rubycritic', '4.6.1'
45
46
  gem.add_development_dependency 'simplecov', '0.17.1'
46
47
  gem.add_development_dependency 'sqlite3', '1.4.2'
47
- gem.add_development_dependency 'yard', '0.9.24'
48
+ gem.add_development_dependency 'yard', '0.9.26'
48
49
  gem.add_development_dependency 'yardstick', '0.9.9'
49
50
  end
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
  #
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,17 @@ 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
+ after_save: nil,
23
+ build_with: nil
17
24
  }.freeze
18
25
 
19
26
  with_options DEFAULT_OPTIONS
@@ -25,6 +32,20 @@ module Azeroth
25
32
  [only].flatten.map(&:to_sym) - [except].flatten.map(&:to_sym)
26
33
  end
27
34
 
35
+ # Returns event dispatcher
36
+ #
37
+ # Event dispatcher is responsible for
38
+ # sending events such as +before_save+
39
+ # to it's correct calling point
40
+ #
41
+ # @return [Jace::Dispatcher]
42
+ def event_dispatcher(event)
43
+ Jace::Dispatcher.new(
44
+ before: try("before_#{event}"),
45
+ after: try("after_#{event}")
46
+ )
47
+ end
48
+
28
49
  # @method only
29
50
  # @api private
30
51
  #
@@ -50,5 +71,34 @@ module Azeroth
50
71
  # model.as_json
51
72
  #
52
73
  # @return [Decorator,TrueClass,FalseClass]
74
+
75
+ # @method before_save
76
+ # @api private
77
+ #
78
+ # Block or method name to be run before save
79
+ #
80
+ # The given method or block will be ran
81
+ # before committing changes in models
82
+ # to database
83
+ #
84
+ # @return [Symbol,Proc]
85
+
86
+ # @method after_save
87
+ # @api private
88
+ #
89
+ # Block or method name to be run after save
90
+ #
91
+ # The given method or block will be ran
92
+ # after committing changes in models
93
+ # to database
94
+ #
95
+ # @return [Symbol,Proc]
96
+
97
+ # @method build_with
98
+ # @api private
99
+ #
100
+ # Block or method name to be ran when building the resource
101
+ #
102
+ # @return [Symbol,Proc]
53
103
  end
54
104
  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