bright_serializer 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/build.yml +1 -2
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +22 -0
- data/lib/bright_serializer/attribute.rb +5 -2
- data/lib/bright_serializer/entity/base.rb +42 -0
- data/lib/bright_serializer/entity/parser.rb +19 -0
- data/lib/bright_serializer/inflector.rb +31 -0
- data/lib/bright_serializer/serializer.rb +22 -2
- data/lib/bright_serializer/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 124b91b1e579f871a02ebdc415fc663c3053ea9a45592a3bcf37cae0f3ef6f20
|
4
|
+
data.tar.gz: c59949c767f3391e97e38d332ecaf14a93525ea5971edf260a044af5b66ee513
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a716280c2dc9064f639f9e67ccf0ab24ae4cf0060293d5d0d77bad889598857cd1aadc75612cc38a8ffe564d57b8e24f97e699d9fa46efdad03964a890b6a856
|
7
|
+
data.tar.gz: 0311226ca9be88fcc43d690486de25195c25cd7ccdf0bad636b0b4707dc50a01a07b7f830071d947f01ee6d4660b04122f9fed601a5113c2ee2e83600b639d53
|
data/.github/workflows/build.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
|
3
3
|
## master (unreleased)
|
4
4
|
|
5
|
+
* Your contribution
|
6
|
+
|
7
|
+
## 0.2.0 (2020-07-17)
|
8
|
+
|
9
|
+
* Add RubyGems version badge
|
10
|
+
* Handle inherit from a parent serializer
|
11
|
+
* Define entity in serializer for grape_swagger (#9)
|
12
|
+
|
5
13
|
## 0.1.1 (2020-07-13)
|
6
14
|
|
7
15
|
* Add description in gemspec file
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
[![Actions Status](https://github.com/petalmd/bright_serializer/workflows/Build/badge.svg)](https://github.com/petalmd/bright_serializer/actions?query=workflow%3ABuild)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/bright_serializer.svg)](https://badge.fury.io/rb/bright_serializer)
|
2
3
|
|
3
4
|
# BrightSerializer
|
4
5
|
|
@@ -124,6 +125,27 @@ class AccountSerializer
|
|
124
125
|
end
|
125
126
|
```
|
126
127
|
|
128
|
+
### Entity
|
129
|
+
|
130
|
+
You can define the entity of your serializer to generate documentation with the option `entity`.
|
131
|
+
The feature was build to work with [grape-swagger](https://github.com/ruby-grape/grape-swagger).
|
132
|
+
For more information about defining a model entity see the [Swagger documentation](https://swagger.io/specification/v2/?sbsearch=array%20response#schema-object).
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
class AccountSerializer
|
136
|
+
include BrightSerializer::Serializer
|
137
|
+
attribute :id, entity: { type: :string, description: 'The id of the account' }
|
138
|
+
attribute :name
|
139
|
+
|
140
|
+
attribute :friends,
|
141
|
+
entity: {
|
142
|
+
type: :array, items: { ref: 'FriendSerializer' }, description: 'The list the account friends.'
|
143
|
+
} do |object|
|
144
|
+
FriendSerializer.new(object.friends)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
127
149
|
## Development
|
128
150
|
|
129
151
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -1,14 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'entity/base'
|
4
|
+
|
3
5
|
module BrightSerializer
|
4
6
|
class Attribute
|
5
|
-
attr_reader :key, :block, :condition
|
7
|
+
attr_reader :key, :block, :condition, :entity
|
6
8
|
attr_accessor :transformed_key
|
7
9
|
|
8
|
-
def initialize(key, condition, &block)
|
10
|
+
def initialize(key, condition, entity, &block)
|
9
11
|
@key = key
|
10
12
|
@condition = condition
|
11
13
|
@block = block
|
14
|
+
@entity = entity ? Entity::Base.new(entity) : nil
|
12
15
|
end
|
13
16
|
|
14
17
|
def serialize(object, params)
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'parser'
|
4
|
+
|
5
|
+
module BrightSerializer
|
6
|
+
module Entity
|
7
|
+
class Base
|
8
|
+
DEFAULT_DEFINITION = { type: :undefined }.freeze
|
9
|
+
|
10
|
+
# https://swagger.io/specification/v2/?sbsearch=array%20response#schema-object
|
11
|
+
|
12
|
+
def initialize(definition)
|
13
|
+
@definition = definition
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_h
|
17
|
+
@definition.transform_keys! { |k| Inflector.camel_lower k.to_s }
|
18
|
+
parse_ref!
|
19
|
+
@definition
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_ref!
|
23
|
+
object = nested_hash(@definition, 'ref')
|
24
|
+
return unless object
|
25
|
+
|
26
|
+
ref_entity_name = Inflector.constantize(object.delete('ref')).entity_name
|
27
|
+
relation = "#/definitions/#{ref_entity_name}"
|
28
|
+
object['$ref'] = relation
|
29
|
+
end
|
30
|
+
|
31
|
+
def nested_hash(obj, key)
|
32
|
+
if obj.respond_to?(:key?) && obj.key?(key)
|
33
|
+
obj
|
34
|
+
elsif obj.respond_to?(:each)
|
35
|
+
r = nil
|
36
|
+
obj.find { |*a| r = nested_hash(a.last, key) }
|
37
|
+
r
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BrightSerializer
|
4
|
+
module Entity
|
5
|
+
class Parser
|
6
|
+
attr_reader :model
|
7
|
+
attr_reader :endpoint
|
8
|
+
|
9
|
+
def initialize(model, endpoint)
|
10
|
+
@model = model
|
11
|
+
@endpoint = endpoint
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
@model.entity
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -28,5 +28,36 @@ class Inflector
|
|
28
28
|
underscored_word.tr!('_', '-')
|
29
29
|
underscored_word
|
30
30
|
end
|
31
|
+
|
32
|
+
# File activesupport/lib/active_support/inflector/methods.rb, line 271
|
33
|
+
def constantize(camel_cased_word)
|
34
|
+
names = camel_cased_word.split('::')
|
35
|
+
|
36
|
+
# Trigger a built-in NameError exception including the ill-formed constant in the message.
|
37
|
+
Object.const_get(camel_cased_word) if names.empty?
|
38
|
+
|
39
|
+
# Remove the first blank element in case of '::ClassName' notation.
|
40
|
+
names.shift if names.size > 1 && names.first.empty?
|
41
|
+
|
42
|
+
names.inject(Object) do |constant, name|
|
43
|
+
if constant == Object
|
44
|
+
constant.const_get(name)
|
45
|
+
else
|
46
|
+
candidate = constant.const_get(name)
|
47
|
+
next candidate if constant.const_defined?(name, false)
|
48
|
+
next candidate unless Object.const_defined?(name)
|
49
|
+
|
50
|
+
# Go down the ancestors to check if it is owned directly. The check
|
51
|
+
# stops when we reach Object or the end of ancestors tree.
|
52
|
+
constant = constant.ancestors.each_with_object(constant) do |ancestor, const|
|
53
|
+
break const if ancestor == Object
|
54
|
+
break ancestor if ancestor.const_defined?(name, false)
|
55
|
+
end
|
56
|
+
|
57
|
+
# owner is in Object, so raise
|
58
|
+
constant.const_get(name, false)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
31
62
|
end
|
32
63
|
end
|
@@ -4,6 +4,7 @@ require 'oj'
|
|
4
4
|
require 'set'
|
5
5
|
require_relative 'attribute'
|
6
6
|
require_relative 'inflector'
|
7
|
+
require_relative 'entity/base'
|
7
8
|
|
8
9
|
module BrightSerializer
|
9
10
|
module Serializer
|
@@ -31,7 +32,7 @@ module BrightSerializer
|
|
31
32
|
end
|
32
33
|
|
33
34
|
def serializable_hash
|
34
|
-
if @object.respond_to?(:
|
35
|
+
if @object.respond_to?(:each) && !@object.respond_to?(:each_pair)
|
35
36
|
@object.map { |o| serialize o }
|
36
37
|
else
|
37
38
|
serialize(@object)
|
@@ -49,9 +50,15 @@ module BrightSerializer
|
|
49
50
|
module ClassMethods
|
50
51
|
attr_reader :attributes_to_serialize, :transform_method
|
51
52
|
|
53
|
+
def inherited(subclass)
|
54
|
+
super
|
55
|
+
subclass.instance_variable_set(:@attributes_to_serialize, []) unless subclass.attributes_to_serialize
|
56
|
+
subclass.attributes_to_serialize.concat(@attributes_to_serialize)
|
57
|
+
end
|
58
|
+
|
52
59
|
def attributes(*attributes, **options, &block)
|
53
60
|
attributes.each do |key|
|
54
|
-
attribute = Attribute.new(key, options[:if], &block)
|
61
|
+
attribute = Attribute.new(key, options[:if], options[:entity], &block)
|
55
62
|
attribute.transformed_key = run_transform_key(key)
|
56
63
|
@attributes_to_serialize << attribute
|
57
64
|
end
|
@@ -74,6 +81,19 @@ module BrightSerializer
|
|
74
81
|
input.to_sym
|
75
82
|
end
|
76
83
|
end
|
84
|
+
|
85
|
+
def entity
|
86
|
+
{}.tap do |result|
|
87
|
+
@attributes_to_serialize.each do |attribute|
|
88
|
+
entity_value = attribute.entity&.to_h || BrightSerializer::Entity::Base::DEFAULT_DEFINITION
|
89
|
+
result.merge!(attribute.transformed_key => entity_value)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def entity_name
|
95
|
+
name.split('::').last.downcase
|
96
|
+
end
|
77
97
|
end
|
78
98
|
end
|
79
99
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bright_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean-Francis Bastien
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -101,6 +101,8 @@ files:
|
|
101
101
|
- bright_serializer.gemspec
|
102
102
|
- lib/bright_serializer.rb
|
103
103
|
- lib/bright_serializer/attribute.rb
|
104
|
+
- lib/bright_serializer/entity/base.rb
|
105
|
+
- lib/bright_serializer/entity/parser.rb
|
104
106
|
- lib/bright_serializer/inflector.rb
|
105
107
|
- lib/bright_serializer/serializer.rb
|
106
108
|
- lib/bright_serializer/version.rb
|