gql_serializer 2.2.1 → 3.0.1
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/.github/workflows/ruby.yml +13 -7
- data/.gitignore +16 -16
- data/.rspec +3 -3
- data/.ruby-version +1 -1
- data/.travis.yml +6 -6
- data/Gemfile +5 -5
- data/Gemfile.lock +51 -31
- data/LICENSE.txt +21 -21
- data/README.md +189 -146
- data/Rakefile +6 -6
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/gql_serializer.gemspec +36 -36
- data/lib/gql_serializer/configuration.rb +29 -29
- data/lib/gql_serializer/extensions.rb +71 -52
- data/lib/gql_serializer/version.rb +3 -3
- data/lib/gql_serializer.rb +218 -178
- metadata +10 -16
data/lib/gql_serializer.rb
CHANGED
@@ -1,178 +1,218 @@
|
|
1
|
-
require "gql_serializer/version"
|
2
|
-
require "gql_serializer/extensions"
|
3
|
-
require "gql_serializer/configuration"
|
4
|
-
|
5
|
-
|
6
|
-
module GqlSerializer
|
7
|
-
|
8
|
-
def self.configuration
|
9
|
-
@configuration ||= Configuration.new
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.configure
|
13
|
-
yield configuration
|
14
|
-
end
|
15
|
-
|
16
|
-
|
17
|
-
def self.parse_query(input)
|
18
|
-
query = input.dup
|
19
|
-
query.strip!
|
20
|
-
query.gsub!(/[\r\n\t ]+/, ' ')
|
21
|
-
query.gsub!(/\{ /, '{')
|
22
|
-
query.gsub!(/ }/, '}')
|
23
|
-
|
24
|
-
result, _ = self.parse_it(query)
|
25
|
-
result
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.query_include(model, hasharray)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
if
|
40
|
-
|
41
|
-
|
42
|
-
include_array.push(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
1
|
+
require "gql_serializer/version"
|
2
|
+
require "gql_serializer/extensions"
|
3
|
+
require "gql_serializer/configuration"
|
4
|
+
|
5
|
+
|
6
|
+
module GqlSerializer
|
7
|
+
|
8
|
+
def self.configuration
|
9
|
+
@configuration ||= Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure
|
13
|
+
yield configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def self.parse_query(input)
|
18
|
+
query = input.dup
|
19
|
+
query.strip!
|
20
|
+
query.gsub!(/[\r\n\t ]+/, ' ')
|
21
|
+
query.gsub!(/\{ /, '{')
|
22
|
+
query.gsub!(/ }/, '}')
|
23
|
+
|
24
|
+
result, _ = self.parse_it(query)
|
25
|
+
result
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.query_include(model, hasharray)
|
29
|
+
return [] if !model.respond_to? :reflections
|
30
|
+
include_array = []
|
31
|
+
relations = model.reflections
|
32
|
+
hasharray.each do |element|
|
33
|
+
if element.is_a? String
|
34
|
+
key = element.split(':')[0]
|
35
|
+
include_array.push(key) if relations[key]
|
36
|
+
elsif element.is_a? Hash
|
37
|
+
key = element.keys.first.split(':')[0]
|
38
|
+
relation_model = model.reflections&.[](key)&.klass
|
39
|
+
next if relation_model.nil?
|
40
|
+
relation_hasharray = self.query_include(relation_model, element.values.first)
|
41
|
+
if relation_hasharray.empty?
|
42
|
+
include_array.push(key)
|
43
|
+
else
|
44
|
+
include_array.push({key => relation_hasharray})
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
include_array
|
49
|
+
end
|
50
|
+
|
51
|
+
# example hasharray = ["id", "name:real_name", "tags", { "panels" => ["id", { "cards" => ["content"] }] }]
|
52
|
+
def self.serialize(records, hasharray, options, instructions = {})
|
53
|
+
|
54
|
+
if records.nil?
|
55
|
+
return nil
|
56
|
+
end
|
57
|
+
|
58
|
+
if records.is_a?(Hash)
|
59
|
+
return self.serialize_hash(records, hasharray, options, instructions)
|
60
|
+
end
|
61
|
+
|
62
|
+
if records.respond_to? :map
|
63
|
+
return records.map do |record|
|
64
|
+
self.serialize(record, hasharray, options, instructions)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if records.class.respond_to? :reflections
|
69
|
+
return self.serialize_active_record(records, hasharray, options, instructions)
|
70
|
+
end
|
71
|
+
|
72
|
+
if !hasharray.empty?
|
73
|
+
return self.serialize_object(records, hasharray, options, instructions)
|
74
|
+
end
|
75
|
+
|
76
|
+
return coerce_value(records)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.serialize_active_record(record, hasharray, options, instructions = {})
|
80
|
+
id = "#{record.class}, #{hasharray}"
|
81
|
+
instruction = instructions[id]
|
82
|
+
if (!instruction)
|
83
|
+
instruction = {klass: record.class, hasharray: hasharray, attributes: []}
|
84
|
+
instructions[id] = instruction
|
85
|
+
|
86
|
+
model = record.class
|
87
|
+
all_relations = model.reflections.keys
|
88
|
+
relations = hasharray.filter do |relation|
|
89
|
+
key, _ = self.get_keys(relation, options)
|
90
|
+
all_relations.include?(key)
|
91
|
+
end
|
92
|
+
|
93
|
+
if (hasharray - relations).empty?
|
94
|
+
attributes = model.attribute_names + relations
|
95
|
+
else
|
96
|
+
attributes = hasharray
|
97
|
+
end
|
98
|
+
|
99
|
+
attributes.each do |attribute|
|
100
|
+
key, alias_key, sub_hasharray = self.get_keys(attribute, options)
|
101
|
+
instruction[:attributes].push({key: key, alias_key: alias_key, hasharray: sub_hasharray})
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
hash = {}
|
106
|
+
instruction[:attributes].each do |attribute|
|
107
|
+
value = record.public_send(attribute[:key])
|
108
|
+
hash[attribute[:alias_key]] = self.serialize(value, attribute[:hasharray], options, instructions)
|
109
|
+
end
|
110
|
+
|
111
|
+
hash
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.serialize_object(record, hasharray, options, instructions = {})
|
115
|
+
hash = {}
|
116
|
+
|
117
|
+
hasharray.each do |attribute|
|
118
|
+
key, alias_key, sub_hasharray = self.get_keys(attribute, options)
|
119
|
+
value = record.public_send(key)
|
120
|
+
hash[alias_key] = self.serialize(value, sub_hasharray, options, instructions)
|
121
|
+
end
|
122
|
+
|
123
|
+
hash
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.serialize_hash(record, hasharray, options, instructions = {})
|
127
|
+
hash = {}
|
128
|
+
attributes = hasharray.empty? ? record.keys : hasharray
|
129
|
+
|
130
|
+
attributes.each do |attribute|
|
131
|
+
key, alias_key, sub_hasharray = self.get_keys(attribute, options)
|
132
|
+
value = self.get_hash_value(record, key)
|
133
|
+
hash[alias_key] = self.serialize(value, sub_hasharray, options, instructions)
|
134
|
+
end
|
135
|
+
|
136
|
+
hash
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.coerce_value(value)
|
140
|
+
return value.to_f if value.is_a? BigDecimal
|
141
|
+
return value.new_offset(0).strftime("%FT%TZ") if value.is_a? DateTime
|
142
|
+
return value.utc.iso8601 if value.is_a? Time
|
143
|
+
value
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
private
|
148
|
+
|
149
|
+
def self.apply_case(key, key_case)
|
150
|
+
case key_case
|
151
|
+
when Configuration::CAMEL_CASE
|
152
|
+
result = key.camelize
|
153
|
+
result[0] = result[0].downcase
|
154
|
+
when Configuration::SNAKE_CASE
|
155
|
+
result = key.underscore
|
156
|
+
else
|
157
|
+
result = key
|
158
|
+
end
|
159
|
+
|
160
|
+
result
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.get_hash_value(hash, key)
|
164
|
+
return hash[key] if hash.key?(key)
|
165
|
+
return hash[key.to_sym] if hash.key?(key.to_sym)
|
166
|
+
raise NoMethodError, "undefined field '#{key.to_s}' for #{hash}"
|
167
|
+
end
|
168
|
+
|
169
|
+
def self.get_keys(attribute, options)
|
170
|
+
if attribute.is_a?(Symbol)
|
171
|
+
key, alias_key = attribute.to_s.split(':')
|
172
|
+
hasharray = []
|
173
|
+
elsif attribute.is_a?(String)
|
174
|
+
key, alias_key = attribute.split(':')
|
175
|
+
hasharray = []
|
176
|
+
else
|
177
|
+
key, alias_key = attribute.keys.first.split(':')
|
178
|
+
hasharray = attribute.values.first
|
179
|
+
end
|
180
|
+
alias_key = apply_case(alias_key || key, options[:case])
|
181
|
+
[key, alias_key, hasharray]
|
182
|
+
end
|
183
|
+
|
184
|
+
def self.parse_it(query)
|
185
|
+
result = []
|
186
|
+
while query&.length&.> 0
|
187
|
+
if query[0] == ' '
|
188
|
+
query.strip!
|
189
|
+
next
|
190
|
+
elsif query[0] == '}'
|
191
|
+
return result, query[1..-1]
|
192
|
+
end
|
193
|
+
|
194
|
+
next_key = query[/[_a-zA-Z0-9:]+/]
|
195
|
+
query = query[next_key.length..-1]
|
196
|
+
query.strip!
|
197
|
+
|
198
|
+
if query.nil? || query.empty? || query[0].match?(/[_a-zA-Z0-9:]/)
|
199
|
+
result.push(next_key)
|
200
|
+
|
201
|
+
elsif query&.[](0) == '{'
|
202
|
+
query = query[1..-1]
|
203
|
+
obj, query = parse_it(query)
|
204
|
+
result.push(next_key => obj)
|
205
|
+
|
206
|
+
elsif query[0] == '}'
|
207
|
+
result.push(next_key)
|
208
|
+
return result, query[1..-1]
|
209
|
+
|
210
|
+
else
|
211
|
+
raise "unsupported character '#{query[0]}'"
|
212
|
+
|
213
|
+
end
|
214
|
+
end
|
215
|
+
return result, nil
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gql_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Scullion
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -44,48 +44,42 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.
|
47
|
+
version: '0.15'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.
|
54
|
+
version: '0.15'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: sqlite3
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: '2.7'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: '2.7'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: activerecord
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
76
|
-
- - "<"
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version: '8.0'
|
75
|
+
version: '7.0'
|
79
76
|
type: :runtime
|
80
77
|
prerelease: false
|
81
78
|
version_requirements: !ruby/object:Gem::Requirement
|
82
79
|
requirements:
|
83
80
|
- - ">="
|
84
81
|
- !ruby/object:Gem::Version
|
85
|
-
version: '
|
86
|
-
- - "<"
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
version: '8.0'
|
82
|
+
version: '7.0'
|
89
83
|
description: A gem that adds `as_gql` to easily serialize ActiveRecord objects
|
90
84
|
email:
|
91
85
|
- andrewsc32@protonmail.com
|
@@ -125,14 +119,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
119
|
requirements:
|
126
120
|
- - ">="
|
127
121
|
- !ruby/object:Gem::Version
|
128
|
-
version: 2.
|
122
|
+
version: 2.7.0
|
129
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
124
|
requirements:
|
131
125
|
- - ">="
|
132
126
|
- !ruby/object:Gem::Version
|
133
127
|
version: '0'
|
134
128
|
requirements: []
|
135
|
-
rubygems_version: 3.
|
129
|
+
rubygems_version: 3.4.19
|
136
130
|
signing_key:
|
137
131
|
specification_version: 4
|
138
132
|
summary: A gem that adds `as_gql` to easily serialize ActiveRecord objects
|