gql_serializer 2.2.0 → 3.0.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 +4 -4
- data/.github/workflows/ruby.yml +33 -33
- data/.gitignore +17 -15
- data/.rspec +3 -3
- data/.ruby-version +1 -1
- data/.travis.yml +6 -6
- data/Gemfile +5 -5
- data/Gemfile.lock +80 -62
- data/LICENSE.txt +21 -21
- data/README.md +185 -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 +12 -12
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__)
|
data/bin/setup
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
set -euo pipefail
|
3
|
-
IFS=$'\n\t'
|
4
|
-
set -vx
|
5
|
-
|
6
|
-
bundle install
|
7
|
-
|
8
|
-
# Do any other automated setup that you need to do here
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
set -euo pipefail
|
3
|
+
IFS=$'\n\t'
|
4
|
+
set -vx
|
5
|
+
|
6
|
+
bundle install
|
7
|
+
|
8
|
+
# Do any other automated setup that you need to do here
|
data/gql_serializer.gemspec
CHANGED
@@ -1,36 +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.
|
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.
|
33
|
-
spec.add_development_dependency "sqlite3", "~>
|
34
|
-
|
35
|
-
spec.add_runtime_dependency "activerecord", ">=
|
36
|
-
end
|
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.7.0")
|
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.15"
|
33
|
+
spec.add_development_dependency "sqlite3", "~> 2.7"
|
34
|
+
|
35
|
+
spec.add_runtime_dependency "activerecord", ">= 7.0", "< 8.1"
|
36
|
+
end
|
@@ -1,30 +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
|
-
|
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 =
|
24
|
-
end
|
25
|
-
|
26
|
-
def to_h
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
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_accessor :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 = true
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_h
|
27
|
+
self.instance_values.symbolize_keys
|
28
|
+
end
|
29
|
+
end
|
30
30
|
end
|
@@ -1,52 +1,71 @@
|
|
1
|
-
require "active_record"
|
2
|
-
|
3
|
-
module GqlSerializer
|
4
|
-
module Array
|
5
|
-
def as_gql(
|
6
|
-
map { |v| v.as_gql(
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
module
|
11
|
-
def as_gql(query = nil, options = {})
|
12
|
-
|
13
|
-
query_hasharray = query ? GqlSerializer.parse_query(query) : []
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
module GqlSerializer
|
4
|
+
module Array
|
5
|
+
def as_gql(*args)
|
6
|
+
map { |v| v.as_gql(*args) }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Hash
|
11
|
+
def as_gql(query = nil, options = {})
|
12
|
+
options_with_default = GqlSerializer.configuration.to_h.merge(options)
|
13
|
+
query_hasharray = query ? GqlSerializer.parse_query(query) : []
|
14
|
+
GqlSerializer.serialize(self, query_hasharray, options_with_default)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module Object
|
19
|
+
def as_gql(query = nil, options = {})
|
20
|
+
options_with_default = GqlSerializer.configuration.to_h.merge(options)
|
21
|
+
query_hasharray = query ? GqlSerializer.parse_query(query) : []
|
22
|
+
GqlSerializer.serialize(self, query_hasharray, options_with_default)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Relation
|
27
|
+
def as_gql(query = nil, options = {})
|
28
|
+
options_with_defaults = GqlSerializer.configuration.to_h.merge(options)
|
29
|
+
query_hasharray = query ? GqlSerializer.parse_query(query) : []
|
30
|
+
include_hasharray = GqlSerializer.query_include(self.model, query_hasharray)
|
31
|
+
records = self.includes(include_hasharray).records
|
32
|
+
GqlSerializer.serialize(records, query_hasharray, options_with_defaults)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module ActiveRecord
|
37
|
+
def self.as_gql(query = nil, options = {})
|
38
|
+
self.all.as_gql(query, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def as_gql(query = nil, options = {})
|
42
|
+
options_with_defaults = GqlSerializer.configuration.to_h.merge(options)
|
43
|
+
query_hasharray = query ? GqlSerializer.parse_query(query) : []
|
44
|
+
include_hasharray = GqlSerializer.query_include(self.class, query_hasharray)
|
45
|
+
if options_with_defaults[:preload]
|
46
|
+
GqlSerializer._preload([self], include_hasharray)
|
47
|
+
GqlSerializer.serialize(self, query_hasharray, options_with_defaults)
|
48
|
+
else
|
49
|
+
record = include_hasharray.empty? ? self : self.class.where(id: self).includes(include_hasharray).first
|
50
|
+
GqlSerializer.serialize(record, query_hasharray, options_with_defaults)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self._preload(records, include_hasharray)
|
56
|
+
if ::ActiveRecord::VERSION::MAJOR >= 7
|
57
|
+
::ActiveRecord::Associations::Preloader.
|
58
|
+
new(records: records, associations: include_hasharray).call
|
59
|
+
else
|
60
|
+
::ActiveRecord::Associations::Preloader.
|
61
|
+
new.preload(records, include_hasharray)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
ActiveRecord::Base.include GqlSerializer::ActiveRecord
|
67
|
+
ActiveRecord::Relation.include GqlSerializer::Relation
|
68
|
+
Array.include GqlSerializer::Array
|
69
|
+
Hash.include GqlSerializer::Hash
|
70
|
+
# This is invasive but users can choose to add `as_gql` to all objects in their project with:
|
71
|
+
# Object.include GqlSerializer::Object
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module GqlSerializer
|
2
|
-
VERSION = "
|
3
|
-
end
|
1
|
+
module GqlSerializer
|
2
|
+
VERSION = "3.0.0"
|
3
|
+
end
|