pan_stuff 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +3 -0
- data/lib/pan_stuff/active_record_pagination.rb +28 -0
- data/lib/pan_stuff/method_not_overridden_error.rb +9 -0
- data/lib/pan_stuff/pagination/metadata.rb +15 -0
- data/lib/pan_stuff/pagination/metadata_serializer.rb +14 -0
- data/lib/pan_stuff/params_helpers.rb +34 -0
- data/lib/pan_stuff/railtie.rb +37 -0
- data/lib/pan_stuff/resources_controller.rb +184 -0
- data/lib/pan_stuff/serializer/base.rb +56 -0
- data/lib/pan_stuff/serializer/exception_serializer.rb +36 -0
- data/lib/pan_stuff/serializer/hash_serializer.rb +48 -0
- data/lib/pan_stuff/serializer/money_serializer.rb +35 -0
- data/lib/pan_stuff/serializer/object_serializer.rb +101 -0
- data/lib/pan_stuff/serializer/resource_errors_serializer.rb +44 -0
- data/lib/pan_stuff/serializer/smart_hash_serializer.rb +32 -0
- data/lib/pan_stuff/serializer/validation_response.rb +15 -0
- data/lib/pan_stuff/serializer/validation_response_serializer.rb +33 -0
- data/lib/pan_stuff/serializer.rb +6 -0
- data/lib/pan_stuff/version.rb +3 -0
- data/lib/pan_stuff.rb +12 -0
- data/lib/tasks/pan_stuff_tasks.rake +4 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 617af704f2bf1397d4013602cdaae42c69b97d12d9025b3c5dca2b63eb06328a
|
4
|
+
data.tar.gz: cc964c632f08f9e7f9a66ae7caa50479ec25f98781a30fe5e26fe2ddc5a18937
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3608b95a5da82acdebb56893939254730ed81639e969d5fb64c8df48ff793038b00b441c087b9201674598cf37c4222256a891fe56591b94ec7b62f05037372
|
7
|
+
data.tar.gz: 85d7475fdd592331c35e941208fd5a7d32e7fe87e3ed7f3b8e36569250692a3ac050a3fee9f1b4b107afffb08cd663488246e32475c099e5a1af72322c370037
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright Umberto Peserico
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# PanStuff
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "pan_stuff"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install pan_stuff
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module ActiveRecordPagination
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
class_methods do
|
8
|
+
def paginate(current_page:, items_per_page:)
|
9
|
+
limit = items_per_page
|
10
|
+
offset = (current_page - 1) * items_per_page
|
11
|
+
|
12
|
+
total_count = count
|
13
|
+
total_pages = (total_count.to_d / items_per_page).ceil
|
14
|
+
|
15
|
+
metadata = {
|
16
|
+
total_count: total_count,
|
17
|
+
total_pages: total_pages,
|
18
|
+
current_page: current_page,
|
19
|
+
items_per_page: items_per_page,
|
20
|
+
}
|
21
|
+
[
|
22
|
+
limit(limit).offset(offset),
|
23
|
+
metadata,
|
24
|
+
]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Pagination
|
5
|
+
class Metadata
|
6
|
+
include ActiveModel::Model
|
7
|
+
include ActiveModel::Attributes
|
8
|
+
|
9
|
+
attribute :total_count
|
10
|
+
attribute :total_pages
|
11
|
+
attribute :current_page
|
12
|
+
attribute :items_per_page
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Pagination
|
5
|
+
class MetadataSerializer
|
6
|
+
include ::PanStuff::Serializer::ObjectSerializer
|
7
|
+
|
8
|
+
attribute :total_count
|
9
|
+
attribute :total_pages
|
10
|
+
attribute :current_page
|
11
|
+
attribute :items_per_page
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module ParamsHelpers
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def request_params
|
8
|
+
request.request_parameters.to_h.deep_symbolize_keys
|
9
|
+
end
|
10
|
+
|
11
|
+
def query_params
|
12
|
+
res = request.query_parameters.deep_transform_keys(&:underscore).deep_symbolize_keys
|
13
|
+
|
14
|
+
parse_query_params(res)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def parse_query_params(val)
|
20
|
+
case val
|
21
|
+
when ->(v) { v.is_a?(Hash) }
|
22
|
+
val.transform_values do |val2|
|
23
|
+
parse_query_params(val2)
|
24
|
+
end
|
25
|
+
when ->(v) { v.is_a?(Array) }
|
26
|
+
val.map { |v| parse_query_params(v) }
|
27
|
+
when 'null', 'undefined'
|
28
|
+
nil
|
29
|
+
else
|
30
|
+
val
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module PanStuff
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
|
4
|
+
initializer 'pan_stuff.setup_renderers' do
|
5
|
+
ActionController::Renderers.add :resource do |resource, options|
|
6
|
+
options = options.slice(:serializer, :meta, :message, :context, :message, :location)
|
7
|
+
|
8
|
+
result = options[:serializer].new(resource, meta: options[:meta], message: options[:message]).as_json
|
9
|
+
|
10
|
+
self.content_type = Mime[:json]
|
11
|
+
|
12
|
+
result
|
13
|
+
end
|
14
|
+
|
15
|
+
ActionController::Renderers.add :resource_errors do |errors, _options|
|
16
|
+
result = PanStuff::Serializer::ResourceErrorsSerializer.new(errors).as_json
|
17
|
+
|
18
|
+
self.content_type = Mime[:json]
|
19
|
+
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
ActionController::Renderers.add :exception do |exception, options|
|
24
|
+
options = options.slice(:status, :error)
|
25
|
+
|
26
|
+
result = PanStuff::Serializer::ExceptionSerializer.new(exception: exception, root: false, **options).as_json
|
27
|
+
|
28
|
+
self.content_type = Mime[:json]
|
29
|
+
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Base.send(:include, PanStuff::ActiveRecordPagination)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module ResourcesController
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def index
|
8
|
+
@resource_collection = resource_collection_resolver
|
9
|
+
|
10
|
+
render resource: @resource_collection,
|
11
|
+
serializer: index_resource_serializer,
|
12
|
+
context: context,
|
13
|
+
meta: meta
|
14
|
+
end
|
15
|
+
|
16
|
+
def show
|
17
|
+
@resource = resource_finder
|
18
|
+
|
19
|
+
render resource: @resource,
|
20
|
+
serializer: show_resource_serializer,
|
21
|
+
context: context,
|
22
|
+
meta: meta,
|
23
|
+
location: resource_location
|
24
|
+
end
|
25
|
+
|
26
|
+
def create
|
27
|
+
@resource = resource_creator
|
28
|
+
|
29
|
+
if @resource.errors.any?
|
30
|
+
render resource_errors: @resource.errors, status: :unprocessable_entity
|
31
|
+
else
|
32
|
+
render resource: @resource,
|
33
|
+
serializer: create_resource_serializer,
|
34
|
+
context: context,
|
35
|
+
meta: meta,
|
36
|
+
message: create_message,
|
37
|
+
location: resource_location,
|
38
|
+
status: :created
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def update
|
43
|
+
@resource = resource_updater
|
44
|
+
|
45
|
+
if @resource.errors.any?
|
46
|
+
render resource_errors: @resource.errors, status: :unprocessable_entity
|
47
|
+
else
|
48
|
+
render resource: @resource,
|
49
|
+
serializer: update_resource_serializer,
|
50
|
+
context: context,
|
51
|
+
meta: meta,
|
52
|
+
message: update_message,
|
53
|
+
location: resource_location
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def destroy
|
58
|
+
@resource = resource_destroyer
|
59
|
+
|
60
|
+
if @resource.errors.any?
|
61
|
+
render resource_errors: @resource.errors, status: :unprocessable_entity
|
62
|
+
else
|
63
|
+
render resource: @resource,
|
64
|
+
serializer: destroy_resource_serializer,
|
65
|
+
context: context,
|
66
|
+
meta: meta,
|
67
|
+
message: destroy_message,
|
68
|
+
location: resource_location
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
protected
|
73
|
+
|
74
|
+
def context
|
75
|
+
@context ||= {}
|
76
|
+
end
|
77
|
+
|
78
|
+
def add_meta(key, value)
|
79
|
+
meta[key] = value
|
80
|
+
end
|
81
|
+
|
82
|
+
def meta
|
83
|
+
@meta ||= {}
|
84
|
+
end
|
85
|
+
|
86
|
+
def resource_service
|
87
|
+
resource_service_class.new
|
88
|
+
end
|
89
|
+
|
90
|
+
def resource_service_class
|
91
|
+
raise MethodNotOverriddenError.new(__method__, self)
|
92
|
+
end
|
93
|
+
|
94
|
+
def resource_serializer
|
95
|
+
raise MethodNotOverriddenError.new(__method__, self)
|
96
|
+
end
|
97
|
+
|
98
|
+
def resource_location
|
99
|
+
raise MethodNotOverriddenError.new(__method__, self)
|
100
|
+
end
|
101
|
+
|
102
|
+
def resource_params_root_key
|
103
|
+
raise MethodNotOverriddenError.new(__method__, self)
|
104
|
+
end
|
105
|
+
|
106
|
+
def create_message
|
107
|
+
nil
|
108
|
+
end
|
109
|
+
|
110
|
+
def update_message
|
111
|
+
nil
|
112
|
+
end
|
113
|
+
|
114
|
+
def destroy_message
|
115
|
+
nil
|
116
|
+
end
|
117
|
+
|
118
|
+
def resource_params
|
119
|
+
@resource_params ||= request_params.fetch(resource_params_root_key)
|
120
|
+
rescue KeyError => e
|
121
|
+
raise ActionController::ParameterMissing, e
|
122
|
+
end
|
123
|
+
|
124
|
+
def context=(context)
|
125
|
+
self.context.merge!(context)
|
126
|
+
end
|
127
|
+
|
128
|
+
def index_resource_serializer
|
129
|
+
resource_serializer
|
130
|
+
end
|
131
|
+
|
132
|
+
def show_resource_serializer
|
133
|
+
resource_serializer
|
134
|
+
end
|
135
|
+
|
136
|
+
def create_resource_serializer
|
137
|
+
resource_serializer
|
138
|
+
end
|
139
|
+
|
140
|
+
def update_resource_serializer
|
141
|
+
resource_serializer
|
142
|
+
end
|
143
|
+
|
144
|
+
def destroy_resource_serializer
|
145
|
+
resource_serializer
|
146
|
+
end
|
147
|
+
|
148
|
+
def resource_collection_resolver
|
149
|
+
resource_service.all(ancestry_key_params)
|
150
|
+
end
|
151
|
+
|
152
|
+
def resource_finder
|
153
|
+
resource_service.find(ancestry_key_params, resource_key_param)
|
154
|
+
end
|
155
|
+
|
156
|
+
def resource_creator
|
157
|
+
resource_service.create(ancestry_key_params, resource_create_params)
|
158
|
+
end
|
159
|
+
|
160
|
+
def resource_updater
|
161
|
+
resource_service.update(ancestry_key_params, resource_key_param, resource_update_params)
|
162
|
+
end
|
163
|
+
|
164
|
+
def resource_destroyer
|
165
|
+
resource_service.destroy(ancestry_key_params, resource_key_param)
|
166
|
+
end
|
167
|
+
|
168
|
+
def resource_key_param
|
169
|
+
params[:id]
|
170
|
+
end
|
171
|
+
|
172
|
+
def ancestry_key_params
|
173
|
+
{}
|
174
|
+
end
|
175
|
+
|
176
|
+
def resource_create_params
|
177
|
+
resource_params
|
178
|
+
end
|
179
|
+
|
180
|
+
def resource_update_params
|
181
|
+
resource_params
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
module Base
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
TRANSFORMS_MAPPING = {
|
9
|
+
camel_lower: %i[camelize lower],
|
10
|
+
underscore: :underscore,
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
included do
|
14
|
+
cattr_accessor :transform_method_value, default: TRANSFORMS_MAPPING[:camel_lower]
|
15
|
+
end
|
16
|
+
|
17
|
+
def as_json(opts = nil)
|
18
|
+
JSON.generate(result, opts)
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_h
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
alias to_hash to_h
|
26
|
+
|
27
|
+
def serializable_hash!
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def run_key_transform!(key)
|
32
|
+
key.to_s.send(*transform_method_value).to_sym
|
33
|
+
end
|
34
|
+
|
35
|
+
class_methods do
|
36
|
+
def transform_method(transform_method)
|
37
|
+
unless TRANSFORMS_MAPPING.key?(transform_method)
|
38
|
+
raise StandardError, "Transform method must be equal to #{TRANSFORMS_MAPPING.keys}"
|
39
|
+
end
|
40
|
+
|
41
|
+
self.transform_method_value = TRANSFORMS_MAPPING[transform_method]
|
42
|
+
end
|
43
|
+
|
44
|
+
def collection?(resource)
|
45
|
+
resource.is_a?(Enumerable) && !resource.respond_to?(:each_pair)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def result
|
52
|
+
@result ||= serializable_hash!
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
class ExceptionSerializer
|
6
|
+
include Base
|
7
|
+
|
8
|
+
attr_reader :error, :status, :exception, :root
|
9
|
+
|
10
|
+
def initialize(status: nil, error: nil, exception: nil, root: true)
|
11
|
+
@error = error
|
12
|
+
@status = status
|
13
|
+
@exception = exception
|
14
|
+
@root = root
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def serializable_hash!
|
20
|
+
data = {}
|
21
|
+
result = {}
|
22
|
+
|
23
|
+
data[run_key_transform!(:status)] = status
|
24
|
+
data[run_key_transform!(:error)] = error if error
|
25
|
+
data[run_key_transform!(:exception)] = exception if exception
|
26
|
+
|
27
|
+
if root
|
28
|
+
result[run_key_transform!(:validation_response)] = data
|
29
|
+
result
|
30
|
+
else
|
31
|
+
data
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
class HashSerializer
|
6
|
+
include Base
|
7
|
+
|
8
|
+
attr_reader :hash, :meta, :message, :root
|
9
|
+
|
10
|
+
def initialize(resource, meta: nil, message: nil, context: nil, root: true)
|
11
|
+
@hash = resource
|
12
|
+
@resource = resource
|
13
|
+
@meta = meta
|
14
|
+
@message = message
|
15
|
+
@context = context || {}
|
16
|
+
@root = root
|
17
|
+
end
|
18
|
+
|
19
|
+
def data
|
20
|
+
hash
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def serializable_hash!
|
26
|
+
if root
|
27
|
+
result = {}
|
28
|
+
|
29
|
+
result[run_key_transform!(:data)] = serialize_data
|
30
|
+
result[run_key_transform!(:meta)] = meta&.deep_transform_keys { |key| run_key_transform!(key) } if meta
|
31
|
+
result[run_key_transform!(:message)] = message if message
|
32
|
+
|
33
|
+
result
|
34
|
+
else
|
35
|
+
serialize_data
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def serialize_data
|
40
|
+
if self.class.collection? @resource
|
41
|
+
@resource.map { |o| o.deep_transform_keys { |key| run_key_transform!(key) } }
|
42
|
+
else
|
43
|
+
@resource&.deep_transform_keys { |key| run_key_transform!(key) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
class MoneySerializer
|
6
|
+
include ObjectSerializer
|
7
|
+
|
8
|
+
attribute :cents, method: :cents
|
9
|
+
attribute :amount, method: :amount
|
10
|
+
attribute :currency, method: :currency
|
11
|
+
attribute :formatted_text, method: :formatted_text
|
12
|
+
attribute :symbol, method: :symbol
|
13
|
+
|
14
|
+
def cents(record)
|
15
|
+
record.cents
|
16
|
+
end
|
17
|
+
|
18
|
+
def amount(record)
|
19
|
+
record.to_f
|
20
|
+
end
|
21
|
+
|
22
|
+
def currency(record)
|
23
|
+
record.currency.iso_code
|
24
|
+
end
|
25
|
+
|
26
|
+
def formatted_text(record)
|
27
|
+
record.format(symbol: "#{record.currency.symbol} ")
|
28
|
+
end
|
29
|
+
|
30
|
+
def symbol(record)
|
31
|
+
record.currency.symbol
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
module ObjectSerializer
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
include Base
|
8
|
+
|
9
|
+
included do
|
10
|
+
class << self
|
11
|
+
attr_writer :attributes_to_serialize
|
12
|
+
|
13
|
+
def attributes_to_serialize
|
14
|
+
@attributes_to_serialize ||= []
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :resource, :message, :context, :root
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(resource, meta: nil, message: nil, context: nil, root: true)
|
22
|
+
@resource = resource
|
23
|
+
@meta = meta
|
24
|
+
@message = message
|
25
|
+
@context = context || {}
|
26
|
+
@root = root
|
27
|
+
end
|
28
|
+
|
29
|
+
class_methods do
|
30
|
+
def attribute(key, options = {})
|
31
|
+
attributes_to_serialize.push({
|
32
|
+
name: key,
|
33
|
+
**options.slice(:method, :serializer, :serialize_if),
|
34
|
+
})
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def data
|
39
|
+
if self.class.collection? @resource
|
40
|
+
data_for_collection
|
41
|
+
else
|
42
|
+
data_for_one_record
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def meta
|
47
|
+
@meta&.deep_transform_keys do |k|
|
48
|
+
run_key_transform!(k)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def serializable_hash!
|
53
|
+
if root
|
54
|
+
use_root
|
55
|
+
else
|
56
|
+
data
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def data_for_collection
|
63
|
+
@resource.map { |el| build_record(el) }
|
64
|
+
end
|
65
|
+
|
66
|
+
def data_for_one_record
|
67
|
+
build_record(@resource) if @resource
|
68
|
+
end
|
69
|
+
|
70
|
+
def build_record(record)
|
71
|
+
self.class.attributes_to_serialize.each_with_object({}) do |attr, hash|
|
72
|
+
name = attr[:name]
|
73
|
+
serializer = attr[:serializer]
|
74
|
+
method = attr[:method]
|
75
|
+
serialize_if = attr[:serialize_if]
|
76
|
+
|
77
|
+
next if serialize_if && !serialize_if.call(context)
|
78
|
+
|
79
|
+
hash[run_key_transform!(name)] = if serializer.present?
|
80
|
+
attr[:serializer].new(record.send(name), root: false, context: context)
|
81
|
+
.to_h
|
82
|
+
elsif method.present?
|
83
|
+
send(method, record)
|
84
|
+
else
|
85
|
+
record.send(name)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def use_root
|
91
|
+
result = {}
|
92
|
+
|
93
|
+
result[run_key_transform!(:data)] = data
|
94
|
+
result[run_key_transform!(:meta)] = meta if @meta.present?
|
95
|
+
result[run_key_transform!(:message)] = message if @message.present?
|
96
|
+
|
97
|
+
result
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
class ResourceErrorsSerializer
|
6
|
+
include Base
|
7
|
+
|
8
|
+
def initialize(errors)
|
9
|
+
@resource = errors
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def errors
|
15
|
+
@resource.full_messages
|
16
|
+
end
|
17
|
+
|
18
|
+
def details
|
19
|
+
@resource.details.each_with_object({}) do |error, result|
|
20
|
+
field = error[0]
|
21
|
+
errors = error[1]
|
22
|
+
result[run_key_transform!(field)] = errors.map do |e|
|
23
|
+
e.each_with_object({}) do |obj, result2|
|
24
|
+
if obj[0] == :error
|
25
|
+
result2[:error] = run_key_transform!(obj[1])
|
26
|
+
else
|
27
|
+
result2[run_key_transform!(obj[0])] = obj[1]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def serializable_hash!
|
35
|
+
result = {}
|
36
|
+
|
37
|
+
result[run_key_transform!(:errors)] = errors
|
38
|
+
result[run_key_transform!(:details)] = details
|
39
|
+
|
40
|
+
result
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
class SmartHashSerializer < HashSerializer
|
6
|
+
def initialize(resource, meta: nil, message: nil, context: nil, root: true)
|
7
|
+
super(
|
8
|
+
check_type(resource),
|
9
|
+
meta: meta,
|
10
|
+
message: message,
|
11
|
+
context: context,
|
12
|
+
root: root,
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def check_type(stuff)
|
19
|
+
case stuff
|
20
|
+
when Array
|
21
|
+
stuff.map { |s| check_type(s) }
|
22
|
+
when Hash
|
23
|
+
stuff.transform_values { |v| check_type(v) }
|
24
|
+
when Money
|
25
|
+
MoneySerializer.new(stuff, root: false).data
|
26
|
+
else
|
27
|
+
stuff
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
class ValidationResponse
|
6
|
+
include ActiveModel::Model
|
7
|
+
include ActiveModel::Attributes
|
8
|
+
include ActiveModel::Serialization
|
9
|
+
|
10
|
+
attribute :id, type: :string
|
11
|
+
attribute :status, type: :integer
|
12
|
+
attribute :message, type: :string
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PanStuff
|
4
|
+
module Serializer
|
5
|
+
class ValidationResponseSerializer
|
6
|
+
include Base
|
7
|
+
|
8
|
+
attr_reader :message
|
9
|
+
|
10
|
+
def initialize(resource, message = nil)
|
11
|
+
@resource = resource
|
12
|
+
@message = message
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def validation_response
|
18
|
+
@resource.attributes.symbolize_keys.deep_transform_keys do |k|
|
19
|
+
run_key_transform!(k)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def serializable_hash!
|
24
|
+
result = {}
|
25
|
+
|
26
|
+
result[run_key_transform!(:validation_response)] = validation_response
|
27
|
+
result[run_key_transform!(:message)] = message if message
|
28
|
+
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/pan_stuff.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pan_stuff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Umberto Peserico
|
8
|
+
- Alessio Bussolari
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2024-09-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: money
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '6.19'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '6.19'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rails
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 7.0.0
|
35
|
+
- - "<"
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '8'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 7.0.0
|
45
|
+
- - "<"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '8'
|
48
|
+
description: Description of PanStuff.
|
49
|
+
email:
|
50
|
+
- umberto.peserico@pandev.it
|
51
|
+
- alessio.bussolari@pandev.it
|
52
|
+
executables: []
|
53
|
+
extensions: []
|
54
|
+
extra_rdoc_files: []
|
55
|
+
files:
|
56
|
+
- MIT-LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/pan_stuff.rb
|
60
|
+
- lib/pan_stuff/active_record_pagination.rb
|
61
|
+
- lib/pan_stuff/method_not_overridden_error.rb
|
62
|
+
- lib/pan_stuff/pagination/metadata.rb
|
63
|
+
- lib/pan_stuff/pagination/metadata_serializer.rb
|
64
|
+
- lib/pan_stuff/params_helpers.rb
|
65
|
+
- lib/pan_stuff/railtie.rb
|
66
|
+
- lib/pan_stuff/resources_controller.rb
|
67
|
+
- lib/pan_stuff/serializer.rb
|
68
|
+
- lib/pan_stuff/serializer/base.rb
|
69
|
+
- lib/pan_stuff/serializer/exception_serializer.rb
|
70
|
+
- lib/pan_stuff/serializer/hash_serializer.rb
|
71
|
+
- lib/pan_stuff/serializer/money_serializer.rb
|
72
|
+
- lib/pan_stuff/serializer/object_serializer.rb
|
73
|
+
- lib/pan_stuff/serializer/resource_errors_serializer.rb
|
74
|
+
- lib/pan_stuff/serializer/smart_hash_serializer.rb
|
75
|
+
- lib/pan_stuff/serializer/validation_response.rb
|
76
|
+
- lib/pan_stuff/serializer/validation_response_serializer.rb
|
77
|
+
- lib/pan_stuff/version.rb
|
78
|
+
- lib/tasks/pan_stuff_tasks.rake
|
79
|
+
homepage: https://bitbucket.org/pandev-srl/pan-stuff
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata:
|
83
|
+
rubygems_mfa_required: 'true'
|
84
|
+
homepage_uri: https://bitbucket.org/pandev-srl/pan-stuff
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 3.2.0
|
94
|
+
- - "<"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.4'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubygems_version: 3.5.17
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Summary of PanStuff.
|
107
|
+
test_files: []
|