gql_serializer 2.2.0
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 +7 -0
- data/.github/workflows/ruby.yml +33 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +62 -0
- data/LICENSE.txt +21 -0
- data/README.md +146 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gql_serializer.gemspec +36 -0
- data/lib/gql_serializer/configuration.rb +30 -0
- data/lib/gql_serializer/extensions.rb +52 -0
- data/lib/gql_serializer/version.rb +3 -0
- data/lib/gql_serializer.rb +178 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1d8e3d8ba95425285d54b5e60ef3d871964b9753888a0142234ab5e2b3946062
|
4
|
+
data.tar.gz: 7870f9feb370d956d6e556f10e5542c108341d83a20f81e2faa77beceee4a21a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c741e6372a5c1a4e970de523867b798a775559e21815ca4e8995bbb8c1b5832f1ab725e29b2fa02fe6cd4fae28f166cf91308ef42993cb29008558afa1f7e81
|
7
|
+
data.tar.gz: 8cb2f9357675f3abeaa3a3bd1d21aae86c7adca3f3c44942bf4b0446785e26a4a4ad84435066fd241452733da5340739d8620d12753c47eed06f51e6d2422010
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ master ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ master ]
|
15
|
+
|
16
|
+
jobs:
|
17
|
+
test:
|
18
|
+
|
19
|
+
runs-on: ubuntu-latest
|
20
|
+
|
21
|
+
steps:
|
22
|
+
- uses: actions/checkout@v2
|
23
|
+
- name: Set up Ruby
|
24
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
25
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
26
|
+
# uses: ruby/setup-ruby@v1
|
27
|
+
uses: ruby/setup-ruby@21351ecc0a7c196081abca5dc55b08f085efe09a
|
28
|
+
with:
|
29
|
+
ruby-version: 2.6
|
30
|
+
- name: Install dependencies
|
31
|
+
run: bundle install
|
32
|
+
- name: Run tests
|
33
|
+
run: rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.5
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gql_serializer (2.2.0)
|
5
|
+
activerecord (>= 5.2, < 8.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.1.5)
|
11
|
+
activesupport (= 6.1.5)
|
12
|
+
activerecord (6.1.5)
|
13
|
+
activemodel (= 6.1.5)
|
14
|
+
activesupport (= 6.1.5)
|
15
|
+
activesupport (6.1.5)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
zeitwerk (~> 2.3)
|
21
|
+
coderay (1.1.3)
|
22
|
+
concurrent-ruby (1.1.9)
|
23
|
+
diff-lcs (1.4.4)
|
24
|
+
i18n (1.10.0)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
method_source (1.0.0)
|
27
|
+
minitest (5.15.0)
|
28
|
+
pry (0.13.1)
|
29
|
+
coderay (~> 1.1)
|
30
|
+
method_source (~> 1.0)
|
31
|
+
rake (12.3.3)
|
32
|
+
rspec (3.10.0)
|
33
|
+
rspec-core (~> 3.10.0)
|
34
|
+
rspec-expectations (~> 3.10.0)
|
35
|
+
rspec-mocks (~> 3.10.0)
|
36
|
+
rspec-core (3.10.0)
|
37
|
+
rspec-support (~> 3.10.0)
|
38
|
+
rspec-expectations (3.10.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.10.0)
|
41
|
+
rspec-mocks (3.10.0)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.10.0)
|
44
|
+
rspec-support (3.10.0)
|
45
|
+
sqlite3 (1.4.2)
|
46
|
+
tzinfo (2.0.4)
|
47
|
+
concurrent-ruby (~> 1.0)
|
48
|
+
zeitwerk (2.5.4)
|
49
|
+
|
50
|
+
PLATFORMS
|
51
|
+
ruby
|
52
|
+
x64-mingw32
|
53
|
+
|
54
|
+
DEPENDENCIES
|
55
|
+
gql_serializer!
|
56
|
+
pry (~> 0.13.1)
|
57
|
+
rake (~> 12.0)
|
58
|
+
rspec (~> 3.0)
|
59
|
+
sqlite3 (~> 1.4.2)
|
60
|
+
|
61
|
+
BUNDLED WITH
|
62
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Andrew Scullion
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# GqlSerializer
|
2
|
+
|
3
|
+
GqlSerializer is a gem that makes it easy to serialize ActiveRecord objects into json using a short syntax similar to GraphQL instead of the more verbose syntax used by the `as_json` method.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'gql_serializer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gql_serializer
|
20
|
+
|
21
|
+
## Basic Usage
|
22
|
+
|
23
|
+
Say you have the following `User` class
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
attribute :email_address, :string
|
28
|
+
attribute :name, :string
|
29
|
+
def encoded_id
|
30
|
+
"User-#{id}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
and you create a user
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
user = User.create(name: 'Test User', email_address: 'user@test.com')
|
39
|
+
```
|
40
|
+
|
41
|
+
You can call `as_gql` with no arguments to get:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
user.as_gql
|
45
|
+
=> {
|
46
|
+
"id"=>1,
|
47
|
+
"name"=>"Test User",
|
48
|
+
"email_address"=>"user@test.com"
|
49
|
+
}
|
50
|
+
```
|
51
|
+
|
52
|
+
By default, all attributes are included in the serialized form but you can create you want with the right arguments and include methods.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
user.as_gql('name encoded_id')
|
56
|
+
=> {
|
57
|
+
"name" => "Test User",
|
58
|
+
"encoded_id" => "User-1"
|
59
|
+
}
|
60
|
+
```
|
61
|
+
|
62
|
+
## Advanced Usage
|
63
|
+
|
64
|
+
Here's where the library becomes more powerful. The GraphQL-esque syntax allows for aliasing and digging into nested objects.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class User < ActiveRecord::Base
|
68
|
+
attribute :email_address, :string
|
69
|
+
attribute :name, :string
|
70
|
+
has_many :orders
|
71
|
+
def encoded_id
|
72
|
+
"User-#{id}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class Order < ActiveRecord::Base
|
77
|
+
belongs_to :user
|
78
|
+
attribute :total, :float
|
79
|
+
attribute :placed_at, :date_time
|
80
|
+
end
|
81
|
+
|
82
|
+
user = User.create(name: 'Test User', email_address: 'user@test.com')
|
83
|
+
order = Order.create(user: user, total: 3.50, placed_at: DateTime.now)
|
84
|
+
|
85
|
+
user.as_gql('name orders')
|
86
|
+
=> {
|
87
|
+
"name" => "Test User",
|
88
|
+
"orders" => [{
|
89
|
+
"id" => 1,
|
90
|
+
"total" => 3.5,
|
91
|
+
"placed_at" => "2020-12-23T08:30:00Z"
|
92
|
+
}]
|
93
|
+
}
|
94
|
+
|
95
|
+
order.as_gql('user { email_address encoded_id:real_id }')
|
96
|
+
=> {
|
97
|
+
"id" => 1,
|
98
|
+
"total" => 3.5,
|
99
|
+
"placed_at" => "2020-12-23T08:30:00Z",
|
100
|
+
"user" => {
|
101
|
+
"email_address" => "user@test.com",
|
102
|
+
"real_id" => "User-1"
|
103
|
+
}
|
104
|
+
}
|
105
|
+
```
|
106
|
+
|
107
|
+
It's also possible to automatically convert the case of the keys into either camel case or snake case. We recommend that you configure this globally (see Configuration section) but it can be done using an optional second argument.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
user.as_gql('email_address name:full_name', {case: GqlSerializer::Configuration::CAMEL_CASE})
|
111
|
+
=> {
|
112
|
+
"emailAddress" => "user@test.com",
|
113
|
+
"fullName" => "Test User"
|
114
|
+
}
|
115
|
+
```
|
116
|
+
|
117
|
+
## Configuration
|
118
|
+
|
119
|
+
In a Rails application, the configuration can be added to an initializer in `config/initalizers/gql_serializer.rb`. The following is the default configuration (no change):
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
GqlSerializer.configure do |config|
|
123
|
+
# no case conversion
|
124
|
+
config.case = GqlSerializer::Configuration::NONE_CASE
|
125
|
+
# set to true to avoid additional query in some cases.
|
126
|
+
# The default of false avoids a potential breaking change from version 2.1 to 2.2
|
127
|
+
config.preload = false
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
The options for `case` are: `NONE_CASE, CAMEL_CASE, SNAKE_CASE`.
|
132
|
+
|
133
|
+
## Development
|
134
|
+
|
135
|
+
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.
|
136
|
+
|
137
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
138
|
+
|
139
|
+
## Contributing
|
140
|
+
|
141
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/TheDro/gql_serializer.
|
142
|
+
|
143
|
+
|
144
|
+
## License
|
145
|
+
|
146
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gql_serializer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative 'lib/gql_serializer/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "gql_serializer"
|
5
|
+
spec.version = GqlSerializer::VERSION
|
6
|
+
spec.authors = ["Andrew Scullion"]
|
7
|
+
spec.email = ["andrewsc32@protonmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{A gem that adds `as_gql` to easily serialize ActiveRecord objects}
|
10
|
+
spec.description = %q{A gem that adds `as_gql` to easily serialize ActiveRecord objects}
|
11
|
+
spec.homepage = "https://github.com/TheDro/gql_serializer"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.1")
|
14
|
+
|
15
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/TheDro/gql_serializer"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/TheDro/gql_serializer"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
spec.add_development_dependency "pry", "~> 0.13.1"
|
33
|
+
spec.add_development_dependency "sqlite3", "~> 1.4.2"
|
34
|
+
|
35
|
+
spec.add_runtime_dependency "activerecord", ">= 5.2", "< 8.0"
|
36
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module GqlSerializer
|
4
|
+
class Configuration
|
5
|
+
CAMEL_CASE = :camel
|
6
|
+
SNAKE_CASE = :snake
|
7
|
+
NONE_CASE = :none
|
8
|
+
SUPPORTED_CASES = [CAMEL_CASE, SNAKE_CASE, NONE_CASE]
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
reset
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :case, :preload
|
15
|
+
|
16
|
+
def case=(value)
|
17
|
+
raise "Specified case '#{value}' is not supported" unless SUPPORTED_CASES.include?(value)
|
18
|
+
@case = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def reset
|
22
|
+
@case = NONE_CASE
|
23
|
+
@preload = false # Default will be true in version 3+
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_h
|
27
|
+
{case: @case}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
module GqlSerializer
|
4
|
+
module Array
|
5
|
+
def as_gql(query = nil)
|
6
|
+
map { |v| v.as_gql(query) }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Relation
|
11
|
+
def as_gql(query = nil, options = {})
|
12
|
+
options_with_defaults = GqlSerializer.configuration.to_h.merge(options)
|
13
|
+
query_hasharray = query ? GqlSerializer.parse_query(query) : []
|
14
|
+
include_hasharray = GqlSerializer.query_include(self.model, query_hasharray)
|
15
|
+
records = self.includes(include_hasharray).records
|
16
|
+
GqlSerializer.serialize(records, query_hasharray, options_with_defaults)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ActiveRecord
|
21
|
+
def self.as_gql(query = nil, options = {})
|
22
|
+
self.all.as_gql(query, options)
|
23
|
+
end
|
24
|
+
|
25
|
+
def as_gql(query = nil, options = {})
|
26
|
+
options_with_defaults = GqlSerializer.configuration.to_h.merge(options)
|
27
|
+
query_hasharray = query ? GqlSerializer.parse_query(query) : []
|
28
|
+
include_hasharray = GqlSerializer.query_include(self.class, query_hasharray)
|
29
|
+
if options_with_defaults[:preload]
|
30
|
+
GqlSerializer._preload([self], include_hasharray)
|
31
|
+
GqlSerializer.serialize(self, query_hasharray, options_with_defaults)
|
32
|
+
else
|
33
|
+
record = include_hasharray.empty? ? self : self.class.where(id: self).includes(include_hasharray).first
|
34
|
+
GqlSerializer.serialize(record, query_hasharray, options_with_defaults)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self._preload(records, include_hasharray)
|
40
|
+
if ::ActiveRecord::VERSION::MAJOR >= 7
|
41
|
+
::ActiveRecord::Associations::Preloader.
|
42
|
+
new(records: records, associations: include_hasharray).call
|
43
|
+
else
|
44
|
+
::ActiveRecord::Associations::Preloader.
|
45
|
+
new.preload(records, include_hasharray)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
ActiveRecord::Base.include GqlSerializer::ActiveRecord
|
51
|
+
ActiveRecord::Relation.include GqlSerializer::Relation
|
52
|
+
Array.include GqlSerializer::Array
|
@@ -0,0 +1,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
|
+
include_array = []
|
30
|
+
relations = model.reflections
|
31
|
+
hasharray.each do |element|
|
32
|
+
if element.is_a? String
|
33
|
+
key = element.split(':')[0]
|
34
|
+
include_array.push(key) if relations[key]
|
35
|
+
elsif element.is_a? Hash
|
36
|
+
key = element.keys.first.split(':')[0]
|
37
|
+
relation_model = model.reflections[key].klass
|
38
|
+
relation_hasharray = self.query_include(relation_model, element.values.first)
|
39
|
+
if relation_hasharray.empty?
|
40
|
+
include_array.push(key)
|
41
|
+
else
|
42
|
+
include_array.push({key => relation_hasharray})
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
include_array
|
47
|
+
end
|
48
|
+
|
49
|
+
# example hasharray = ["id", "name", "tags", { "panels" => ["id", { "cards" => ["content"] }] }]
|
50
|
+
def self.serialize(records, hasharray, options, instructions = {})
|
51
|
+
|
52
|
+
if records.nil?
|
53
|
+
return nil
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
if records.respond_to? :map
|
58
|
+
return records.map do |record|
|
59
|
+
self.serialize(record, hasharray, options, instructions)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
record = records
|
63
|
+
|
64
|
+
id = "#{record.class}, #{hasharray}"
|
65
|
+
instruction = instructions[id]
|
66
|
+
if (!instruction)
|
67
|
+
instruction = {klass: record.class, hasharray: hasharray, relations: [], attributes: []}
|
68
|
+
instructions[id] = instruction
|
69
|
+
|
70
|
+
|
71
|
+
model = record.class
|
72
|
+
all_relations = model.reflections.keys
|
73
|
+
|
74
|
+
relations = hasharray.filter do |relation|
|
75
|
+
next true if !relation.is_a?(String)
|
76
|
+
|
77
|
+
key, alias_key = relation.split(':')
|
78
|
+
|
79
|
+
all_relations.include?(key)
|
80
|
+
end
|
81
|
+
|
82
|
+
attributes = hasharray - relations
|
83
|
+
attributes = model.attribute_names if attributes.empty?
|
84
|
+
|
85
|
+
attributes.each do |attribute|
|
86
|
+
key, alias_key = attribute.split(':')
|
87
|
+
alias_key = apply_case(alias_key || key, options[:case])
|
88
|
+
|
89
|
+
instruction[:attributes].push({key: key, alias_key: alias_key})
|
90
|
+
end
|
91
|
+
|
92
|
+
relations.each do |relation|
|
93
|
+
if relation.is_a? String
|
94
|
+
key, alias_key = relation.split(':')
|
95
|
+
alias_key = apply_case(alias_key || key, options[:case])
|
96
|
+
|
97
|
+
instruction[:relations].push({key: key, alias_key: alias_key, hasharray: []})
|
98
|
+
else
|
99
|
+
key, alias_key = relation.keys.first.split(':')
|
100
|
+
alias_key = apply_case(alias_key || key, options[:case])
|
101
|
+
|
102
|
+
instruction[:relations].push({key: key, alias_key: alias_key, hasharray: relation.values.first})
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
hash = {}
|
108
|
+
instruction[:attributes].each do |attribute|
|
109
|
+
hash[attribute[:alias_key]] = coerce_value(record.public_send(attribute[:key]))
|
110
|
+
end
|
111
|
+
instruction[:relations].each do |relation|
|
112
|
+
relation_records = record.public_send(relation[:key])
|
113
|
+
hash[relation[:alias_key]] = self.serialize(relation_records, relation[:hasharray], options, instructions)
|
114
|
+
end
|
115
|
+
|
116
|
+
hash
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.coerce_value(value)
|
120
|
+
return value.to_f if value.is_a? BigDecimal
|
121
|
+
return value.new_offset(0).strftime("%FT%TZ") if value.is_a? DateTime
|
122
|
+
return value.utc.iso8601 if value.is_a? Time
|
123
|
+
value
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def self.apply_case(key, key_case)
|
130
|
+
case key_case
|
131
|
+
when Configuration::CAMEL_CASE
|
132
|
+
result = key.camelize
|
133
|
+
result[0] = result[0].downcase
|
134
|
+
when Configuration::SNAKE_CASE
|
135
|
+
result = key.underscore
|
136
|
+
else
|
137
|
+
result = key
|
138
|
+
end
|
139
|
+
|
140
|
+
result
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.parse_it(query)
|
144
|
+
result = []
|
145
|
+
while query&.length&.> 0
|
146
|
+
if query[0] == ' '
|
147
|
+
query.strip!
|
148
|
+
next
|
149
|
+
elsif query[0] == '}'
|
150
|
+
return result, query[1..-1]
|
151
|
+
end
|
152
|
+
|
153
|
+
next_key = query[/[_a-zA-Z0-9:]+/]
|
154
|
+
query = query[next_key.length..-1]
|
155
|
+
query.strip!
|
156
|
+
|
157
|
+
if query.nil? || query.empty? || query[0].match?(/[_a-zA-Z0-9:]/)
|
158
|
+
result.push(next_key)
|
159
|
+
|
160
|
+
elsif query&.[](0) == '{'
|
161
|
+
query = query[1..-1]
|
162
|
+
obj, query = parse_it(query)
|
163
|
+
result.push(next_key => obj)
|
164
|
+
|
165
|
+
elsif query[0] == '}'
|
166
|
+
result.push(next_key)
|
167
|
+
return result, query[1..-1]
|
168
|
+
|
169
|
+
else
|
170
|
+
raise "unsupported character '#{query[0]}'"
|
171
|
+
|
172
|
+
end
|
173
|
+
end
|
174
|
+
return result, nil
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gql_serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Scullion
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-03-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '12.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '12.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.13.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.13.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.4.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.4.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.2'
|
76
|
+
- - "<"
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '8.0'
|
79
|
+
type: :runtime
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '5.2'
|
86
|
+
- - "<"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '8.0'
|
89
|
+
description: A gem that adds `as_gql` to easily serialize ActiveRecord objects
|
90
|
+
email:
|
91
|
+
- andrewsc32@protonmail.com
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- ".github/workflows/ruby.yml"
|
97
|
+
- ".gitignore"
|
98
|
+
- ".rspec"
|
99
|
+
- ".ruby-version"
|
100
|
+
- ".travis.yml"
|
101
|
+
- Gemfile
|
102
|
+
- Gemfile.lock
|
103
|
+
- LICENSE.txt
|
104
|
+
- README.md
|
105
|
+
- Rakefile
|
106
|
+
- bin/console
|
107
|
+
- bin/setup
|
108
|
+
- gql_serializer.gemspec
|
109
|
+
- lib/gql_serializer.rb
|
110
|
+
- lib/gql_serializer/configuration.rb
|
111
|
+
- lib/gql_serializer/extensions.rb
|
112
|
+
- lib/gql_serializer/version.rb
|
113
|
+
homepage: https://github.com/TheDro/gql_serializer
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata:
|
117
|
+
homepage_uri: https://github.com/TheDro/gql_serializer
|
118
|
+
source_code_uri: https://github.com/TheDro/gql_serializer
|
119
|
+
changelog_uri: https://github.com/TheDro/gql_serializer
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 2.6.1
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubygems_version: 3.1.6
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: A gem that adds `as_gql` to easily serialize ActiveRecord objects
|
139
|
+
test_files: []
|