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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b04f7b52a385c2deb726e1449dec990107e7030c2cd9dd3bee7df90e82a23369
|
4
|
+
data.tar.gz: 61119b12d003535f652e2e7bded7318c4d6ffcfc9478a9f68f17b19675a528f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63eb3361a7d64b0c41ed45296e2db79a1447386665101bd1b0757734c97702475320923a0356afc8c379ca4c93459a21fcf0158aedbe08e2167bedb0baae3ba7
|
7
|
+
data.tar.gz: f4e88b31cfc4496aab946670f507c853b6a3a1fd102efd0aaab9842ddfae51ae30e494cc15253ff1a3f47f87af13e0fd6227f4859efb0bc422245056a2b6d477
|
data/.github/workflows/ruby.yml
CHANGED
@@ -9,25 +9,31 @@ name: Ruby
|
|
9
9
|
|
10
10
|
on:
|
11
11
|
push:
|
12
|
-
branches: [ master ]
|
12
|
+
branches: [ "master" ]
|
13
13
|
pull_request:
|
14
|
-
branches: [ master ]
|
14
|
+
branches: [ "master" ]
|
15
|
+
|
16
|
+
permissions:
|
17
|
+
contents: read
|
15
18
|
|
16
19
|
jobs:
|
17
20
|
test:
|
18
21
|
|
19
22
|
runs-on: ubuntu-latest
|
23
|
+
strategy:
|
24
|
+
matrix:
|
25
|
+
ruby-version: ['3.2.2']
|
20
26
|
|
21
27
|
steps:
|
22
|
-
- uses: actions/checkout@
|
28
|
+
- uses: actions/checkout@v4
|
23
29
|
- name: Set up Ruby
|
24
30
|
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
25
31
|
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
26
32
|
# uses: ruby/setup-ruby@v1
|
27
|
-
uses: ruby/setup-ruby@
|
33
|
+
uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
|
28
34
|
with:
|
29
|
-
ruby-version:
|
30
|
-
- name:
|
35
|
+
ruby-version: ${{ matrix.ruby-version }}
|
36
|
+
- name: Bundle install
|
31
37
|
run: bundle install
|
32
38
|
- name: Run tests
|
33
|
-
run:
|
39
|
+
run: bundle exec rake
|
data/.gitignore
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
/.bundle/
|
2
|
-
/.yardoc
|
3
|
-
/_yardoc/
|
4
|
-
/coverage/
|
5
|
-
/doc/
|
6
|
-
/pkg/
|
7
|
-
/spec/reports/
|
8
|
-
/tmp/
|
9
|
-
|
10
|
-
# rspec failure tracking
|
11
|
-
.rspec_status
|
12
|
-
|
13
|
-
.idea
|
14
|
-
.byebug_history
|
15
|
-
test.db
|
16
|
-
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
|
10
|
+
# rspec failure tracking
|
11
|
+
.rspec_status
|
12
|
+
|
13
|
+
.idea
|
14
|
+
.byebug_history
|
15
|
+
test.db
|
16
|
+
|
17
17
|
*.gem
|
data/.rspec
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
--format documentation
|
2
|
-
--color
|
3
|
-
--require spec_helper
|
1
|
+
--format documentation
|
2
|
+
--color
|
3
|
+
--require spec_helper
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
ruby-3.2.8
|
data/.travis.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
---
|
2
|
-
language: ruby
|
3
|
-
cache: bundler
|
4
|
-
rvm:
|
5
|
-
- 2.
|
6
|
-
before_install: gem install bundler -v 2.1.4
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
cache: bundler
|
4
|
+
rvm:
|
5
|
+
- 2.7.4
|
6
|
+
before_install: gem install bundler -v 2.1.4
|
data/Gemfile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
source "https://rubygems.org"
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in gql_serializer.gemspec
|
4
|
-
gemspec
|
5
|
-
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in gql_serializer.gemspec
|
4
|
+
gemspec
|
5
|
+
|
data/Gemfile.lock
CHANGED
@@ -1,49 +1,69 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
gql_serializer (
|
5
|
-
activerecord (>=
|
4
|
+
gql_serializer (3.0.1)
|
5
|
+
activerecord (>= 7.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (
|
11
|
-
activesupport (=
|
12
|
-
activerecord (
|
13
|
-
activemodel (=
|
14
|
-
activesupport (=
|
15
|
-
|
16
|
-
|
10
|
+
activemodel (8.0.2)
|
11
|
+
activesupport (= 8.0.2)
|
12
|
+
activerecord (8.0.2)
|
13
|
+
activemodel (= 8.0.2)
|
14
|
+
activesupport (= 8.0.2)
|
15
|
+
timeout (>= 0.4.0)
|
16
|
+
activesupport (8.0.2)
|
17
|
+
base64
|
18
|
+
benchmark (>= 0.3)
|
19
|
+
bigdecimal
|
20
|
+
concurrent-ruby (~> 1.0, >= 1.3.1)
|
21
|
+
connection_pool (>= 2.2.5)
|
22
|
+
drb
|
17
23
|
i18n (>= 1.6, < 2)
|
24
|
+
logger (>= 1.4.2)
|
18
25
|
minitest (>= 5.1)
|
19
|
-
|
26
|
+
securerandom (>= 0.3)
|
27
|
+
tzinfo (~> 2.0, >= 2.0.5)
|
28
|
+
uri (>= 0.13.1)
|
29
|
+
base64 (0.3.0)
|
30
|
+
benchmark (0.4.1)
|
31
|
+
bigdecimal (3.2.2)
|
20
32
|
coderay (1.1.3)
|
21
|
-
concurrent-ruby (1.
|
22
|
-
|
23
|
-
|
33
|
+
concurrent-ruby (1.3.5)
|
34
|
+
connection_pool (2.5.3)
|
35
|
+
diff-lcs (1.6.2)
|
36
|
+
drb (2.2.3)
|
37
|
+
i18n (1.14.7)
|
24
38
|
concurrent-ruby (~> 1.0)
|
25
|
-
|
26
|
-
|
27
|
-
|
39
|
+
logger (1.7.0)
|
40
|
+
method_source (1.1.0)
|
41
|
+
mini_portile2 (2.8.9)
|
42
|
+
minitest (5.25.5)
|
43
|
+
pry (0.15.2)
|
28
44
|
coderay (~> 1.1)
|
29
45
|
method_source (~> 1.0)
|
30
46
|
rake (12.3.3)
|
31
|
-
rspec (3.
|
32
|
-
rspec-core (~> 3.
|
33
|
-
rspec-expectations (~> 3.
|
34
|
-
rspec-mocks (~> 3.
|
35
|
-
rspec-core (3.
|
36
|
-
rspec-support (~> 3.
|
37
|
-
rspec-expectations (3.
|
47
|
+
rspec (3.13.1)
|
48
|
+
rspec-core (~> 3.13.0)
|
49
|
+
rspec-expectations (~> 3.13.0)
|
50
|
+
rspec-mocks (~> 3.13.0)
|
51
|
+
rspec-core (3.13.5)
|
52
|
+
rspec-support (~> 3.13.0)
|
53
|
+
rspec-expectations (3.13.5)
|
38
54
|
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
-
rspec-support (~> 3.
|
40
|
-
rspec-mocks (3.
|
55
|
+
rspec-support (~> 3.13.0)
|
56
|
+
rspec-mocks (3.13.5)
|
41
57
|
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
-
rspec-support (~> 3.
|
43
|
-
rspec-support (3.
|
44
|
-
|
45
|
-
|
58
|
+
rspec-support (~> 3.13.0)
|
59
|
+
rspec-support (3.13.4)
|
60
|
+
securerandom (0.4.1)
|
61
|
+
sqlite3 (2.7.2)
|
62
|
+
mini_portile2 (~> 2.8.0)
|
63
|
+
timeout (0.4.3)
|
64
|
+
tzinfo (2.0.6)
|
46
65
|
concurrent-ruby (~> 1.0)
|
66
|
+
uri (1.0.3)
|
47
67
|
|
48
68
|
PLATFORMS
|
49
69
|
ruby
|
@@ -51,10 +71,10 @@ PLATFORMS
|
|
51
71
|
|
52
72
|
DEPENDENCIES
|
53
73
|
gql_serializer!
|
54
|
-
pry (~> 0.
|
74
|
+
pry (~> 0.15)
|
55
75
|
rake (~> 12.0)
|
56
76
|
rspec (~> 3.0)
|
57
|
-
sqlite3 (~>
|
77
|
+
sqlite3 (~> 2.7)
|
58
78
|
|
59
79
|
BUNDLED WITH
|
60
80
|
2.1.4
|
data/LICENSE.txt
CHANGED
@@ -1,21 +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.
|
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
CHANGED
@@ -1,146 +1,189 @@
|
|
1
|
-
# GqlSerializer
|
2
|
-
|
3
|
-
GqlSerializer is a gem that makes it easy to serialize ActiveRecord objects into
|
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
|
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
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
1
|
+
# GqlSerializer
|
2
|
+
|
3
|
+
GqlSerializer is a gem that makes it easy to serialize ActiveRecord objects, hashes and arrays 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 run:
|
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
|
+
As of version 3, you can use as_gql on hashes. Optionally, objects can also be serialized.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
class Item
|
121
|
+
attr_accessor :name, :price
|
122
|
+
def initialize(name, price)
|
123
|
+
@name = name
|
124
|
+
@price = price
|
125
|
+
end
|
126
|
+
|
127
|
+
def display
|
128
|
+
name + ': ' + ("%.2f" % price)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
hash = {
|
133
|
+
id: 1,
|
134
|
+
total_price: 11.50,
|
135
|
+
items: [
|
136
|
+
Item.new('Thing', 6.00), Item.new('Stuff', 5.50)
|
137
|
+
]
|
138
|
+
}
|
139
|
+
|
140
|
+
hash.as_gql('total_price items { name:item_name display }')
|
141
|
+
=> {
|
142
|
+
"total_price" => 11.5,
|
143
|
+
"items" => [{
|
144
|
+
"item_name" => "Thing",
|
145
|
+
"display" => "Thing: 6.00"
|
146
|
+
},{
|
147
|
+
"item_name" => "Stuff",
|
148
|
+
"display" => "Stuff: 5.50"
|
149
|
+
}]
|
150
|
+
}
|
151
|
+
|
152
|
+
```
|
153
|
+
|
154
|
+
## Configuration
|
155
|
+
|
156
|
+
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):
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
GqlSerializer.configure do |config|
|
160
|
+
# no case conversion
|
161
|
+
config.case = GqlSerializer::Configuration::NONE_CASE
|
162
|
+
# avoids an additional query in some cases.
|
163
|
+
# Set to false for compatibility with version <= 2.1
|
164
|
+
config.preload = true
|
165
|
+
end
|
166
|
+
# Optionally add the `as_gql` method to all objects:
|
167
|
+
# Object.include GqlSerializer::Object
|
168
|
+
```
|
169
|
+
|
170
|
+
The options for `case` are: `NONE_CASE, CAMEL_CASE, SNAKE_CASE`.
|
171
|
+
|
172
|
+
## Compatibility
|
173
|
+
|
174
|
+
The current version of this gem has been tested on Rails version up to 8.0, but likely works with newer versions as well.
|
175
|
+
|
176
|
+
## Development
|
177
|
+
|
178
|
+
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.
|
179
|
+
|
180
|
+
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).
|
181
|
+
|
182
|
+
## Contributing
|
183
|
+
|
184
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/TheDro/gql_serializer.
|
185
|
+
|
186
|
+
|
187
|
+
## License
|
188
|
+
|
189
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
RSpec::Core::RakeTask.new(:spec)
|
5
|
-
|
6
|
-
task :default => :spec
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
data/bin/console
CHANGED
@@ -1,14 +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__)
|
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__)
|