From 5bb3555043fa89426e168ab6b90cc65a6fc5593b Mon Sep 17 00:00:00 2001 From: TheDro Date: Fri, 4 Dec 2020 09:56:08 -0500 Subject: [PATCH 1/5] Added configuration class --- gql_serializer-1.0.0.gem | Bin 8704 -> 0 bytes lib/gql_serializer.rb | 10 +++++++ lib/gql_serializer/configuration.rb | 21 ++++++++++++++ spec/gql_serializer/configuration_spec.rb | 27 ++++++++++++++++++ ...ializer_spec.rb => gql_serializer_spec.rb} | 0 spec/spec_helper.rb | 1 + 6 files changed, 59 insertions(+) delete mode 100644 gql_serializer-1.0.0.gem create mode 100644 lib/gql_serializer/configuration.rb create mode 100644 spec/gql_serializer/configuration_spec.rb rename spec/{serializer_spec.rb => gql_serializer_spec.rb} (100%) diff --git a/gql_serializer-1.0.0.gem b/gql_serializer-1.0.0.gem deleted file mode 100644 index 8dddc69f5e614406c6a1e4469f480f942bb20e9a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8704 zcmeHMRZtvUvK|;R4DKEX?(Xghfx#iTJAn!A4ncwi9SD%1!G|CrFu1!00)zy&A;Bd$ zbMxQ(uv`Db-Iv{ax3>O%>2s>|0qFVvRRR6IUmq`TYtP>y`8nC!yZ*i5Pv?JR z|DW9cmfWA7|GQovC4d1Gx;>WYz%Pa?THp8yLoY1X&RMj4W?&k69}Ayx<)jRxurKzO za)?ie9J{YuNy-1J?F`u|ku$JLWK@2M%~6lW(C7&J$0Tun2}MD{jtWsp?K&PQ~5l(<#y1%$e&iYBY#++?4$H zJ)y43*WS9Npcg5|~a9i}Qm$*I_#^jB=} zJ(35nxBRZb{c6gMIU0L}RE?qV+?VFW8&RY%Yu*-<55&eHeUNcbW280r+^ntcq50Jt z+55oGiy-)F{|)%(J2GgudtVP=$6Sx0ggqLf-D2Wjg-fa3#Z$R$W^5!x=eD{)b`UEF z6tF0F^s&;L?X#oRDYm$3_SJv(VWu#!u8&YPzU zAzUoc8o0Q>8DtrD5yhTsZ43I&>L!ZaH~T*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qbD!e12*Xl9vz7* zgdlzOocWQP?KIpp=^jby)YJ^IK>`x^mFo@)i}fC#2UepU{eCEwPA|3v$@ny>ZpZhw z@uzOOO_Fo!p*!MW>Evaax}FL?i(RnyLGQ-AIm2T~p$2(9z)}hPM5TqFwa%g<_1~*>K$01C>_QrxV?qhb`A2H{f!WR$zK)ts{(&j;6JSb{{`mp*_r?V diff --git a/lib/gql_serializer.rb b/lib/gql_serializer.rb index 754da44..0511ae9 100644 --- a/lib/gql_serializer.rb +++ b/lib/gql_serializer.rb @@ -1,9 +1,19 @@ require "gql_serializer/version" require "gql_serializer/extensions" +require "gql_serializer/configuration" module GqlSerializer + def self.configuration + @configuration ||= Configuration.new + end + + def self.configure + yield configuration + end + + def self.parse_query(input) query = input.dup query.strip! diff --git a/lib/gql_serializer/configuration.rb b/lib/gql_serializer/configuration.rb new file mode 100644 index 0000000..4980d52 --- /dev/null +++ b/lib/gql_serializer/configuration.rb @@ -0,0 +1,21 @@ + + +module GqlSerializer + class Configuration + CAMEL_CASE = :camel + SNAKE_CASE = :snake + NONE_CASE = :none + SUPPORTED_CASES = [CAMEL_CASE, SNAKE_CASE, NONE_CASE] + + def initialize + @case = NONE_CASE + end + + attr_reader :case + + def case=(value) + raise "Specified case '#{value}' is not supported" unless SUPPORTED_CASES.include?(value) + @case = value + end + end +end \ No newline at end of file diff --git a/spec/gql_serializer/configuration_spec.rb b/spec/gql_serializer/configuration_spec.rb new file mode 100644 index 0000000..6f83273 --- /dev/null +++ b/spec/gql_serializer/configuration_spec.rb @@ -0,0 +1,27 @@ + + +RSpec.describe GqlSerializer::Configuration do + describe 'case' do + it 'supports none, snake case and camel case' do + GqlSerializer.configure do |config| + config.case= GqlSerializer::Configuration::NONE_CASE + config.case= GqlSerializer::Configuration::SNAKE_CASE + config.case= GqlSerializer::Configuration::CAMEL_CASE + end + + expect(GqlSerializer.configuration.case).to eq(:camel) + end + + it 'raises an error for unsupported cases' do + GqlSerializer.configure do |config| + config.case= :unsupported + end + + raise "error was not raised" + rescue => e + expect(e.message).to include("not supported") + end + end + + +end \ No newline at end of file diff --git a/spec/serializer_spec.rb b/spec/gql_serializer_spec.rb similarity index 100% rename from spec/serializer_spec.rb rename to spec/gql_serializer_spec.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7bb9640..dcb38ac 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ require "bundler/setup" require "gql_serializer" +require "byebug" RSpec.configure do |config| # Enable flags like --only-failures and --next-failure From 1441fa8638565c7d250504f05407cfc0cd6e3c69 Mon Sep 17 00:00:00 2001 From: TheDro Date: Fri, 4 Dec 2020 10:36:53 -0500 Subject: [PATCH 2/5] Switched from byebug to pry --- Gemfile.lock | 8 ++++++-- gql_serializer.gemspec | 2 +- spec/spec_helper.rb | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8466c09..f718283 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -18,12 +18,16 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2, >= 2.2.2) - byebug (11.1.3) + coderay (1.1.3) concurrent-ruby (1.1.6) diff-lcs (1.4.4) i18n (1.8.2) concurrent-ruby (~> 1.0) + method_source (1.0.0) minitest (5.14.0) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) rake (12.3.3) rspec (3.10.0) rspec-core (~> 3.10.0) @@ -48,8 +52,8 @@ PLATFORMS x64-mingw32 DEPENDENCIES - byebug (~> 11.1) gql_serializer! + pry (~> 0.13.1) rake (~> 12.0) rspec (~> 3.0) sqlite3 (~> 1.4.2) diff --git a/gql_serializer.gemspec b/gql_serializer.gemspec index 5645ace..4a470d5 100644 --- a/gql_serializer.gemspec +++ b/gql_serializer.gemspec @@ -29,7 +29,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake", "~> 12.0" spec.add_development_dependency "rspec", "~> 3.0" - spec.add_development_dependency "byebug", "~> 11.1" + spec.add_development_dependency "pry", "~> 0.13.1" spec.add_development_dependency "sqlite3", "~> 1.4.2" spec.add_runtime_dependency "activerecord", ">= 5.2", "< 6.1" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dcb38ac..e2b10d4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,6 @@ require "bundler/setup" require "gql_serializer" -require "byebug" +require "pry" RSpec.configure do |config| # Enable flags like --only-failures and --next-failure From 3fd7b1e0eac6a68e449ec9443952b19bf87c0dba Mon Sep 17 00:00:00 2001 From: TheDro Date: Fri, 4 Dec 2020 10:37:22 -0500 Subject: [PATCH 3/5] Added support for methods --- lib/gql_serializer.rb | 9 +++++---- spec/gql_serializer_spec.rb | 10 +++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/gql_serializer.rb b/lib/gql_serializer.rb index 0511ae9..731e4f0 100644 --- a/lib/gql_serializer.rb +++ b/lib/gql_serializer.rb @@ -60,15 +60,16 @@ def self.serialize(record, hasharray) hash = {} model = record.class + all_relations = model.reflections.keys - attributes = hasharray.filter do |e| - next false if !e.is_a?(String) + relations = hasharray.filter do |e| + next true if !e.is_a?(String) key, alias_key = e.split(':') - model.attribute_names.include?(key) + all_relations.include?(key) end - relations = hasharray - attributes + attributes = hasharray - relations attributes = model.attribute_names if attributes.empty? attributes.each do |e| diff --git a/spec/gql_serializer_spec.rb b/spec/gql_serializer_spec.rb index c83ac5b..cdf18b2 100644 --- a/spec/gql_serializer_spec.rb +++ b/spec/gql_serializer_spec.rb @@ -1,5 +1,4 @@ require 'active_record' -require 'byebug' RSpec.describe GqlSerializer do it "has a version number" do @@ -59,6 +58,9 @@ class TestUser < ActiveRecord::Base has_many :test_orders + def encoded_id + "TestUser-#{id}" + end end class TestOrder < ActiveRecord::Base @@ -141,6 +143,12 @@ class TestItem < ActiveRecord::Base 'orders' => [{'cost' => orders[0].total}, {'cost' => orders[1].total}] }) end + + it 'supports methods' do + user = TestUser.create(name: 'John', email: 'john@test.com') + + expect(user.as_gql('encoded_id')).to eq({'encoded_id' => "TestUser-#{user.id}"}) + end end end From 1f24551b1e4ff4f6158d47f7b86e5fe19db0c34b Mon Sep 17 00:00:00 2001 From: TheDro Date: Fri, 4 Dec 2020 10:38:22 -0500 Subject: [PATCH 4/5] Added test case --- spec/gql_serializer_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/gql_serializer_spec.rb b/spec/gql_serializer_spec.rb index cdf18b2..0ebf390 100644 --- a/spec/gql_serializer_spec.rb +++ b/spec/gql_serializer_spec.rb @@ -148,6 +148,7 @@ class TestItem < ActiveRecord::Base user = TestUser.create(name: 'John', email: 'john@test.com') expect(user.as_gql('encoded_id')).to eq({'encoded_id' => "TestUser-#{user.id}"}) + expect(user.as_gql('encoded_id:id')).to eq({'id' => "TestUser-#{user.id}"}) end end From 77c674a80b0fa31fabb8885c3f0d5b37badd6407 Mon Sep 17 00:00:00 2001 From: TheDro Date: Fri, 4 Dec 2020 14:47:47 -0500 Subject: [PATCH 5/5] Added case conversion --- Gemfile.lock | 2 +- lib/gql_serializer.rb | 31 +++++++++++++++++++++------- lib/gql_serializer/configuration.rb | 4 ++++ lib/gql_serializer/extensions.rb | 14 +++++++------ lib/gql_serializer/version.rb | 2 +- spec/gql_serializer_spec.rb | 32 +++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 15 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f718283..0e448aa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - gql_serializer (1.0.0) + gql_serializer (1.1.0) activerecord (>= 5.2, < 6.1) GEM diff --git a/lib/gql_serializer.rb b/lib/gql_serializer.rb index 731e4f0..6f5bc67 100644 --- a/lib/gql_serializer.rb +++ b/lib/gql_serializer.rb @@ -46,7 +46,7 @@ def self.query_include(model, hasharray) include_array end - def self.serialize(record, hasharray) + def self.serialize(record, hasharray, options) if record.nil? return nil @@ -54,7 +54,7 @@ def self.serialize(record, hasharray) if record.respond_to? :map return record.map do |r| - self.serialize(r, hasharray) + self.serialize(r, hasharray, options) end end @@ -74,21 +74,24 @@ def self.serialize(record, hasharray) attributes.each do |e| key, alias_key = e.split(':') - alias_key ||= key + alias_key = apply_case(alias_key || key, options[:case]) + hash[alias_key] = record.public_send(key) end relations.each do |e| if e.is_a? String key, alias_key = e.split(':') - alias_key ||= key + alias_key = apply_case(alias_key || key, options[:case]) + rel_records = record.public_send(key) - hash[alias_key] = self.serialize(rel_records, []) + hash[alias_key] = self.serialize(rel_records, [], options) else key, alias_key = e.keys.first.split(':') - alias_key ||= key + alias_key = apply_case(alias_key || key, options[:case]) + rel_records = record.public_send(key) - hash[alias_key] = self.serialize(rel_records, e.values.first) + hash[alias_key] = self.serialize(rel_records, e.values.first, options) end end @@ -98,6 +101,20 @@ def self.serialize(record, hasharray) private + def self.apply_case(key, key_case) + case key_case + when Configuration::CAMEL_CASE + result = key.camelize + result[0] = result[0].downcase + when Configuration::SNAKE_CASE + result = key.underscore + else + result = key + end + + result + end + def self.parse_it(query) result = [] while query&.length&.> 0 diff --git a/lib/gql_serializer/configuration.rb b/lib/gql_serializer/configuration.rb index 4980d52..b0c4dde 100644 --- a/lib/gql_serializer/configuration.rb +++ b/lib/gql_serializer/configuration.rb @@ -17,5 +17,9 @@ def case=(value) raise "Specified case '#{value}' is not supported" unless SUPPORTED_CASES.include?(value) @case = value end + + def to_h + {case: @case} + end end end \ No newline at end of file diff --git a/lib/gql_serializer/extensions.rb b/lib/gql_serializer/extensions.rb index 4c93c3b..7413656 100644 --- a/lib/gql_serializer/extensions.rb +++ b/lib/gql_serializer/extensions.rb @@ -8,25 +8,27 @@ def as_gql(query = nil) end module Relation - def as_gql(query = nil) + def as_gql(query = nil, options = {}) query_hasharray = query ? GqlSerializer.parse_query(query) : [] include_hasharray = GqlSerializer.query_include(self.model, query_hasharray) records = self.includes(include_hasharray).records - GqlSerializer.serialize(records, query_hasharray) + options_with_defaults = GqlSerializer.configuration.to_h.merge(options) + GqlSerializer.serialize(records, query_hasharray, options_with_defaults) end end module ActiveRecord - def self.as_gql(query = nil) - self.all.as_gql(query) + def self.as_gql(query = nil, options = {}) + self.all.as_gql(query, options) end - def as_gql(query = nil) + def as_gql(query = nil, options = {}) query_hasharray = query ? GqlSerializer.parse_query(query) : [] include_hasharray = GqlSerializer.query_include(self.class, query_hasharray) record = include_hasharray.empty? ? self : self.class.where(id: self).includes(include_hasharray).first - GqlSerializer.serialize(record, query_hasharray) + options_with_defaults = GqlSerializer.configuration.to_h.merge(options) + GqlSerializer.serialize(record, query_hasharray, options_with_defaults) end end end diff --git a/lib/gql_serializer/version.rb b/lib/gql_serializer/version.rb index 8b2fd8e..cc4243c 100644 --- a/lib/gql_serializer/version.rb +++ b/lib/gql_serializer/version.rb @@ -1,3 +1,3 @@ module GqlSerializer - VERSION = "1.0.0" + VERSION = "1.1.0" end diff --git a/spec/gql_serializer_spec.rb b/spec/gql_serializer_spec.rb index 0ebf390..abe88ab 100644 --- a/spec/gql_serializer_spec.rb +++ b/spec/gql_serializer_spec.rb @@ -150,6 +150,38 @@ class TestItem < ActiveRecord::Base expect(user.as_gql('encoded_id')).to eq({'encoded_id' => "TestUser-#{user.id}"}) expect(user.as_gql('encoded_id:id')).to eq({'id' => "TestUser-#{user.id}"}) end + + describe 'case conversions' do + class CaseUser < TestUser + def snake_case + 'snake' + end + + def camelCase + 'camel' + end + end + + it 'converts keys to camel case' do + user = CaseUser.create(name: 'John') + + expect(user.as_gql('snake_case camelCase', + {case: GqlSerializer::Configuration::CAMEL_CASE})) + .to eq({'snakeCase' => 'snake', 'camelCase' => 'camel'}) + end + + it 'converts keys to snake case' do + user = CaseUser.create(name: 'John') + + expect(user.as_gql('snake_case camelCase', + {case: GqlSerializer::Configuration::SNAKE_CASE})) + .to eq({'snake_case' => 'snake', 'camel_case' => 'camel'}) + end + + it 'uses the configured case by default' do + binding.pry + end + end end end