azeroth 0.0.4 → 0.0.5
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/.circleci/config.yml +10 -2
- data/.gitignore +8 -2
- data/.rubocop.yml +16 -0
- data/.rubocop_todo.yml +5 -24
- data/Dockerfile +26 -2
- data/Gemfile +2 -0
- data/README.md +2 -1
- data/Rakefile +1 -0
- data/azeroth.gemspec +20 -13
- data/config/check_specs.yml +5 -0
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.yml +7 -2
- data/lib/azeroth.rb +6 -3
- data/lib/azeroth/model.rb +20 -1
- data/lib/azeroth/options.rb +12 -0
- data/lib/azeroth/resource_builder.rb +64 -6
- data/lib/azeroth/resource_route_builder.rb +112 -9
- data/lib/azeroth/resourceable.rb +5 -1
- data/lib/azeroth/resourceable/builder.rb +104 -9
- data/lib/azeroth/routes_builder.rb +54 -20
- data/lib/azeroth/version.rb +3 -1
- data/spec/lib/azeroth/model_spec.rb +7 -5
- data/spec/lib/azeroth/resource_builder_spec.rb +5 -3
- data/spec/lib/azeroth/resource_route_builder_spec.rb +6 -2
- data/spec/lib/azeroth/resourceable/builder_spec.rb +6 -1
- data/spec/lib/azeroth/routes_builder_spec.rb +14 -8
- data/spec/spec_helper.rb +5 -1
- data/spec/support/app/controllers/controller.rb +2 -0
- data/spec/support/app/controllers/resource_builder_controller.rb +2 -0
- data/spec/support/app/controllers/resource_route_builder_controller.rb +2 -0
- data/spec/support/app/controllers/routes_builder_controller.rb +2 -0
- data/spec/support/app/models/document.rb +2 -0
- data/spec/support/app/serializers/serializer.rb +2 -0
- data/spec/support/matchers/add_method.rb +6 -2
- data/spec/support/schema.rb +2 -0
- data/spec/support/shared_examples/resource_routes.rb +5 -3
- metadata +151 -51
- data/scripts/check_readme.sh +0 -6
data/lib/azeroth/resourceable.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support'
|
2
4
|
|
3
5
|
module Azeroth
|
@@ -8,7 +10,9 @@ module Azeroth
|
|
8
10
|
|
9
11
|
class_methods do
|
10
12
|
def resource_for(name, **options)
|
11
|
-
Builder.new(
|
13
|
+
Builder.new(
|
14
|
+
self, name, Azeroth::Options.new(options)
|
15
|
+
).build
|
12
16
|
end
|
13
17
|
end
|
14
18
|
end
|
@@ -1,15 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'sinclair'
|
2
4
|
|
3
5
|
module Azeroth
|
4
6
|
module Resourceable
|
7
|
+
# @api private
|
8
|
+
# Builder responsible to add all methods to the controller
|
9
|
+
#
|
10
|
+
# Builder uses other builders to put together it's methods
|
11
|
+
#
|
12
|
+
# @see ResourceBuilder
|
13
|
+
# @see ResourceRouteBuilder
|
14
|
+
# @see RoutesBuilder
|
5
15
|
class Builder
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(clazz, model_name,
|
16
|
+
# @param clazz [ActionController::Base] Controller to
|
17
|
+
# to be changed
|
18
|
+
# @param model_name [Symbol,String]
|
19
|
+
# @param options [Options]
|
20
|
+
def initialize(clazz, model_name, options)
|
11
21
|
@clazz = clazz
|
12
22
|
@model = Azeroth::Model.new(model_name)
|
23
|
+
@options = options
|
13
24
|
|
14
25
|
add_params
|
15
26
|
add_resource
|
@@ -19,29 +30,113 @@ module Azeroth
|
|
19
30
|
|
20
31
|
private
|
21
32
|
|
33
|
+
attr_reader :clazz, :model, :options
|
34
|
+
# @method clazz
|
35
|
+
# @api private
|
36
|
+
# @private
|
37
|
+
#
|
38
|
+
# Controller to changed
|
39
|
+
#
|
40
|
+
# @return [ActionController::Base]
|
41
|
+
|
42
|
+
# @method model
|
43
|
+
# @api private
|
44
|
+
# @private
|
45
|
+
#
|
46
|
+
# Model interface to resource model
|
47
|
+
#
|
48
|
+
# @return [Model]
|
49
|
+
|
50
|
+
# @method options
|
51
|
+
# @api private
|
52
|
+
# @private
|
53
|
+
#
|
54
|
+
# Options
|
55
|
+
#
|
56
|
+
# @return [Options]
|
57
|
+
|
58
|
+
delegate :build, :add_method, to: :builder
|
59
|
+
# @method build
|
60
|
+
# @api private
|
61
|
+
# @private
|
62
|
+
#
|
63
|
+
# build all the methods
|
64
|
+
#
|
65
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
66
|
+
|
67
|
+
# @method add_method
|
68
|
+
# @api private
|
69
|
+
#
|
70
|
+
# Add method to be built
|
71
|
+
#
|
72
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
73
|
+
|
74
|
+
delegate :name, :klass, to: :model
|
75
|
+
# @method name
|
76
|
+
# @api private
|
77
|
+
# @private
|
78
|
+
#
|
79
|
+
# Resource name
|
80
|
+
#
|
81
|
+
# @return [Symbol,String]
|
82
|
+
|
83
|
+
# @method klass
|
84
|
+
# @api private
|
85
|
+
# @private
|
86
|
+
#
|
87
|
+
# Controller to be changed
|
88
|
+
#
|
89
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
90
|
+
|
91
|
+
# @api private
|
92
|
+
# @private
|
93
|
+
#
|
94
|
+
# Returns a method builder
|
95
|
+
#
|
96
|
+
# @return [Sinclair]
|
97
|
+
#
|
98
|
+
# @see https://www.rubydoc.info/gems/sinclair Sinclair
|
22
99
|
def builder
|
23
100
|
@builder ||= Sinclair.new(clazz)
|
24
101
|
end
|
25
102
|
|
103
|
+
# Add methods for id and parameters
|
104
|
+
#
|
105
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
26
106
|
def add_params
|
27
|
-
add_method("#{
|
28
|
-
add_method(
|
107
|
+
add_method("#{name}_id", 'params.require(:id)')
|
108
|
+
add_method(
|
109
|
+
"#{name}_params",
|
110
|
+
<<-CODE
|
111
|
+
params.require(:#{name})
|
112
|
+
.permit(:#{permitted_attributes.join(', :')})
|
113
|
+
CODE
|
114
|
+
)
|
29
115
|
end
|
30
116
|
|
117
|
+
# Add methods for resource fetching
|
118
|
+
#
|
119
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
31
120
|
def add_resource
|
32
121
|
ResourceBuilder.new(model, builder).append
|
33
122
|
end
|
34
123
|
|
124
|
+
# Add the routes resource methods
|
125
|
+
#
|
126
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
35
127
|
def add_resource_for_routes
|
36
128
|
ResourceRouteBuilder.new(model, builder).append
|
37
129
|
end
|
38
130
|
|
131
|
+
# Add metohods for each route
|
132
|
+
#
|
133
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
39
134
|
def add_routes
|
40
|
-
RoutesBuilder.new(model, builder).append
|
135
|
+
RoutesBuilder.new(model, builder, options).append
|
41
136
|
end
|
42
137
|
|
43
138
|
def permitted_attributes
|
44
|
-
@permitted_attributes ||=
|
139
|
+
@permitted_attributes ||= klass.attribute_names - ['id']
|
45
140
|
end
|
46
141
|
end
|
47
142
|
end
|
@@ -1,35 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Azeroth
|
4
|
+
# @api private
|
5
|
+
# @author Darthjee
|
6
|
+
#
|
7
|
+
# Builder resposible for adding routes methods to the controller
|
2
8
|
class RoutesBuilder
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def initialize(model, builder)
|
9
|
+
# @param model [Model] resource interface
|
10
|
+
# @param builder [Sinclair] methods builder
|
11
|
+
# @param options [Option]
|
12
|
+
def initialize(model, builder, options)
|
8
13
|
@model = model
|
9
14
|
@builder = builder
|
15
|
+
@options = options
|
10
16
|
end
|
11
17
|
|
18
|
+
# Append the routes methods to be built
|
19
|
+
#
|
20
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
12
21
|
def append
|
13
|
-
%i[index show new edit create update
|
14
|
-
add_method(route,
|
22
|
+
%i[index show new edit create update].each do |route|
|
23
|
+
add_method(route, 'render_basic')
|
15
24
|
end
|
25
|
+
|
26
|
+
add_method(:destroy, destroy_code)
|
16
27
|
end
|
17
28
|
|
18
29
|
private
|
19
30
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
attr_reader :model, :builder
|
32
|
+
# @method model
|
33
|
+
# @api private
|
34
|
+
# @private
|
35
|
+
#
|
36
|
+
# Resource interface
|
37
|
+
#
|
38
|
+
# @return [Model]
|
39
|
+
|
40
|
+
# @method builder
|
41
|
+
# @api private
|
42
|
+
# @private
|
43
|
+
#
|
44
|
+
# Methods builder
|
45
|
+
#
|
46
|
+
# @return [Sinclair]
|
47
|
+
|
48
|
+
delegate :add_method, to: :builder
|
49
|
+
# @method add_method
|
50
|
+
# @api private
|
51
|
+
# @private
|
52
|
+
#
|
53
|
+
# Appends a method
|
54
|
+
#
|
55
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
56
|
+
|
57
|
+
# @private
|
58
|
+
#
|
59
|
+
# Method code to destrou route
|
60
|
+
#
|
61
|
+
# @return [String]
|
62
|
+
def destroy_code
|
63
|
+
<<-RUBY
|
64
|
+
#{model.name}.destroy
|
65
|
+
head :no_content
|
66
|
+
RUBY
|
33
67
|
end
|
34
68
|
end
|
35
69
|
end
|
data/lib/azeroth/version.rb
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
shared_examples 'a model wrapper' do
|
4
6
|
describe '#name' do
|
5
7
|
it 'returns the model name' do
|
6
|
-
expect(
|
8
|
+
expect(model.name).to eq('document')
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
10
12
|
describe '#plural' do
|
11
13
|
it 'returns the model name pluralized' do
|
12
|
-
expect(
|
14
|
+
expect(model.plural).to eq('documents')
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
16
18
|
describe '#klass' do
|
17
19
|
it 'returns the class of the model' do
|
18
|
-
expect(
|
20
|
+
expect(model.klass).to eq(Document)
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
23
25
|
describe Azeroth::Model do
|
24
|
-
subject { described_class.new(input) }
|
26
|
+
subject(:model) { described_class.new(input) }
|
25
27
|
|
26
|
-
context 'when initializing with
|
28
|
+
context 'when initializing with symbol' do
|
27
29
|
let(:input) { :document }
|
28
30
|
|
29
31
|
it_behaves_like 'a model wrapper'
|
@@ -1,14 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Azeroth::ResourceBuilder do
|
4
|
-
subject { described_class.new(model, builder) }
|
6
|
+
subject(:resource_builder) { described_class.new(model, builder) }
|
5
7
|
|
6
8
|
let(:model) { Azeroth::Model.new(:document) }
|
7
9
|
let(:builder) { Sinclair.new(klass) }
|
8
10
|
let(:klass) { Class.new(ResourceBuilderController) }
|
9
11
|
|
10
12
|
before do
|
11
|
-
|
13
|
+
resource_builder.append
|
12
14
|
10.times { Document.create }
|
13
15
|
end
|
14
16
|
|
@@ -25,7 +27,7 @@ describe Azeroth::ResourceBuilder do
|
|
25
27
|
.to(true)
|
26
28
|
end
|
27
29
|
|
28
|
-
|
30
|
+
describe 'after the build' do
|
29
31
|
let(:controller) { klass.new(document_id: document.id) }
|
30
32
|
let(:document) { Document.create }
|
31
33
|
|
@@ -1,14 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Azeroth::ResourceRouteBuilder do
|
4
|
-
subject
|
6
|
+
subject(:resource_routes_builder) do
|
7
|
+
described_class.new(model, builder)
|
8
|
+
end
|
5
9
|
|
6
10
|
let(:model) { Azeroth::Model.new(:document) }
|
7
11
|
let(:builder) { Sinclair.new(klass) }
|
8
12
|
let(:klass) { Class.new(ResourceRouteBuilderController) }
|
9
13
|
|
10
14
|
before do
|
11
|
-
|
15
|
+
resource_routes_builder.append
|
12
16
|
10.times { Document.create }
|
13
17
|
end
|
14
18
|
|
@@ -1,7 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Azeroth::Resourceable::Builder do
|
4
|
-
subject { described_class.new(klass, :document) }
|
6
|
+
subject { described_class.new(klass, :document, options) }
|
7
|
+
|
8
|
+
let(:options) { Azeroth::Options.new(options_hash) }
|
9
|
+
let(:options_hash) { {} }
|
5
10
|
|
6
11
|
let(:klass) do
|
7
12
|
Class.new(Controller) do
|
@@ -1,21 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Azeroth::RoutesBuilder do
|
4
|
-
subject
|
6
|
+
subject(:routes_builder) do
|
7
|
+
described_class.new(model, builder, options)
|
8
|
+
end
|
5
9
|
|
6
|
-
let(:model)
|
7
|
-
let(:builder)
|
8
|
-
let(:klass)
|
9
|
-
let(:instance)
|
10
|
-
let(:params)
|
10
|
+
let(:model) { Azeroth::Model.new(:document) }
|
11
|
+
let(:builder) { Sinclair.new(klass) }
|
12
|
+
let(:klass) { Class.new(RoutesBuilderController) }
|
13
|
+
let(:instance) { klass.new(params) }
|
14
|
+
let(:params) { {} }
|
15
|
+
let(:options) { Azeroth::Options.new(options_hash) }
|
16
|
+
let(:options_hash) { {} }
|
11
17
|
|
12
18
|
before do
|
13
|
-
|
19
|
+
routes_builder.append
|
14
20
|
10.times { Document.create }
|
15
21
|
end
|
16
22
|
|
17
23
|
describe '#append' do
|
18
|
-
before {
|
24
|
+
before { routes_builder.append }
|
19
25
|
|
20
26
|
it 'adds index route' do
|
21
27
|
expect do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'simplecov'
|
2
4
|
SimpleCov.start do
|
3
5
|
add_filter '/spec/'
|
@@ -7,7 +9,9 @@ require 'pry-nav'
|
|
7
9
|
require 'azeroth'
|
8
10
|
|
9
11
|
require 'active_record'
|
10
|
-
ActiveRecord::Base.establish_connection(
|
12
|
+
ActiveRecord::Base.establish_connection(
|
13
|
+
adapter: 'sqlite3', database: ':memory:'
|
14
|
+
)
|
11
15
|
|
12
16
|
support_files = File.expand_path('spec/support/**/*.rb')
|
13
17
|
Dir[support_files].each { |file| require file }
|