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.
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
@@ -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.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
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"
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
- 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 = false # Default will be true in version 3+
24
- end
25
-
26
- def to_h
27
- self.instance_values.symbolize_keys
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(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
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 = "2.2.1"
3
- end
1
+ module GqlSerializer
2
+ VERSION = "3.0.1"
3
+ end