From 22b1e09e570b52536e50bed8ab23723519e0dd01 Mon Sep 17 00:00:00 2001 From: Joe Forshaw Date: Fri, 18 Jun 2021 14:16:08 +0100 Subject: [PATCH 1/9] Track Resource#method_missing calls using JsonApiClient::Helper::Debug --- lib/json_api_client/helpers/debug.rb | 14 ++++++++++++++ lib/json_api_client/resource.rb | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 lib/json_api_client/helpers/debug.rb diff --git a/lib/json_api_client/helpers/debug.rb b/lib/json_api_client/helpers/debug.rb new file mode 100644 index 0000000..eacacf9 --- /dev/null +++ b/lib/json_api_client/helpers/debug.rb @@ -0,0 +1,14 @@ +module JsonApiClient + module Helper + module Debug + def track_resource_attribute_calls + $_resource_attribute_calls = {} + yield + puts '----------------------------------------------------------' + pp $_resource_attribute_calls + puts '----------------------------------------------------------' + $_resource_attribute_calls = nil # To avoid risk of memory leaks + end + end + end +end diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index b0b0110..0cde0ff 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -584,6 +584,8 @@ def relation_objects_for(name, relationship_definition) end def method_missing(method, *args) + track_attribute_call(method) if tracking_attributes? + relationship_definition = relationship_definition_for(method) return super unless relationship_definition @@ -640,4 +642,14 @@ def fill_errors end end end + + def tracking_attributes? + defined?($_resource_attribute_calls) && $_resource_attribute_calls.present? + end + + def track_attribute_call(method) + $_resource_attribute_calls[self.class] ||= {} + $_resource_attribute_calls[self.class][method] ||= 0 + $_resource_attribute_calls[self.class][method] += 1 + end end From 7864dec276782b645c1466bfa000ef3eb11c0440 Mon Sep 17 00:00:00 2001 From: Joe Forshaw Date: Fri, 18 Jun 2021 14:43:08 +0100 Subject: [PATCH 2/9] Helpers vs Helper typo --- lib/json_api_client/helpers/debug.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json_api_client/helpers/debug.rb b/lib/json_api_client/helpers/debug.rb index eacacf9..d580d30 100644 --- a/lib/json_api_client/helpers/debug.rb +++ b/lib/json_api_client/helpers/debug.rb @@ -1,5 +1,5 @@ module JsonApiClient - module Helper + module Helpers module Debug def track_resource_attribute_calls $_resource_attribute_calls = {} From 3ee5d0ac41d884827ec05eeaf2df16d67429e65d Mon Sep 17 00:00:00 2001 From: Joe Forshaw Date: Fri, 18 Jun 2021 14:48:53 +0100 Subject: [PATCH 3/9] Autoload Debug --- lib/json_api_client/helpers.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/json_api_client/helpers.rb b/lib/json_api_client/helpers.rb index 6f6f71a..e8b0db4 100644 --- a/lib/json_api_client/helpers.rb +++ b/lib/json_api_client/helpers.rb @@ -5,5 +5,6 @@ module Helpers autoload :DynamicAttributes, 'json_api_client/helpers/dynamic_attributes' autoload :URI, 'json_api_client/helpers/uri' autoload :Associatable, 'json_api_client/helpers/associatable' + autoload :Debug, 'json_api_client/helpers/debug' end end From d4ec21ad9d27cbb4a9100bf63dcfa8fc98531542 Mon Sep 17 00:00:00 2001 From: Joe Forshaw Date: Fri, 18 Jun 2021 15:51:37 +0100 Subject: [PATCH 4/9] Reverted refactor because it was causing infinite loops --- lib/json_api_client/helpers/debug.rb | 1 - lib/json_api_client/resource.rb | 16 +++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/json_api_client/helpers/debug.rb b/lib/json_api_client/helpers/debug.rb index d580d30..22c3910 100644 --- a/lib/json_api_client/helpers/debug.rb +++ b/lib/json_api_client/helpers/debug.rb @@ -7,7 +7,6 @@ def track_resource_attribute_calls puts '----------------------------------------------------------' pp $_resource_attribute_calls puts '----------------------------------------------------------' - $_resource_attribute_calls = nil # To avoid risk of memory leaks end end end diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index 0cde0ff..9d8c50f 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -584,7 +584,11 @@ def relation_objects_for(name, relationship_definition) end def method_missing(method, *args) - track_attribute_call(method) if tracking_attributes? + if defined?($_resource_attribute_calls) + $_resource_attribute_calls[self.class] ||= {} + $_resource_attribute_calls[self.class][method] ||= 0 + $_resource_attribute_calls[self.class][method] += 1 + end relationship_definition = relationship_definition_for(method) @@ -642,14 +646,4 @@ def fill_errors end end end - - def tracking_attributes? - defined?($_resource_attribute_calls) && $_resource_attribute_calls.present? - end - - def track_attribute_call(method) - $_resource_attribute_calls[self.class] ||= {} - $_resource_attribute_calls[self.class][method] ||= 0 - $_resource_attribute_calls[self.class][method] += 1 - end end From e343f7d6e6616c6d8c8f9452fbfcaaca49c03f68 Mon Sep 17 00:00:00 2001 From: Joe Forshaw Date: Fri, 18 Jun 2021 16:46:15 +0100 Subject: [PATCH 5/9] Ignore assigments --- lib/json_api_client/resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index 9d8c50f..32e5478 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -584,7 +584,7 @@ def relation_objects_for(name, relationship_definition) end def method_missing(method, *args) - if defined?($_resource_attribute_calls) + if defined?($_resource_attribute_calls) && !method.ends_with?('=') $_resource_attribute_calls[self.class] ||= {} $_resource_attribute_calls[self.class][method] ||= 0 $_resource_attribute_calls[self.class][method] += 1 From 4e667da42d8577df9e62b9ed4444abf4948dd5fc Mon Sep 17 00:00:00 2001 From: Joe Forshaw Date: Fri, 18 Jun 2021 16:50:37 +0100 Subject: [PATCH 6/9] end_with? vs ends_with? --- lib/json_api_client/resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index 32e5478..b25f538 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -584,7 +584,7 @@ def relation_objects_for(name, relationship_definition) end def method_missing(method, *args) - if defined?($_resource_attribute_calls) && !method.ends_with?('=') + if defined?($_resource_attribute_calls) && !method.end_with?('=') $_resource_attribute_calls[self.class] ||= {} $_resource_attribute_calls[self.class][method] ||= 0 $_resource_attribute_calls[self.class][method] += 1 From 4ae0f43babba08b2a681b52484d2c833009ceee0 Mon Sep 17 00:00:00 2001 From: Joe Forshaw Date: Fri, 18 Jun 2021 16:56:12 +0100 Subject: [PATCH 7/9] method in method_missing is sometimes a string and a symbol???? --- lib/json_api_client/resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index b25f538..b5b6dbc 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -584,7 +584,7 @@ def relation_objects_for(name, relationship_definition) end def method_missing(method, *args) - if defined?($_resource_attribute_calls) && !method.end_with?('=') + if defined?($_resource_attribute_calls) && !method.to_s.ends_with?('=') $_resource_attribute_calls[self.class] ||= {} $_resource_attribute_calls[self.class][method] ||= 0 $_resource_attribute_calls[self.class][method] += 1 From 3d86ab07123aa409972a4bac52b54841c5ec709a Mon Sep 17 00:00:00 2001 From: Rob Aldred Date: Tue, 3 Aug 2021 12:23:12 +0100 Subject: [PATCH 8/9] Fix tracking of fields called outside of scope of controller --- lib/json_api_client/helpers/debug.rb | 6 +++++- lib/json_api_client/resource.rb | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/json_api_client/helpers/debug.rb b/lib/json_api_client/helpers/debug.rb index 22c3910..9de49a9 100644 --- a/lib/json_api_client/helpers/debug.rb +++ b/lib/json_api_client/helpers/debug.rb @@ -1,9 +1,13 @@ module JsonApiClient module Helpers module Debug - def track_resource_attribute_calls + def self.included $_resource_attribute_calls = {} + end + + def track_resource_attribute_calls yield + ensure puts '----------------------------------------------------------' pp $_resource_attribute_calls puts '----------------------------------------------------------' diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index b5b6dbc..74ed4d3 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -585,9 +585,9 @@ def relation_objects_for(name, relationship_definition) def method_missing(method, *args) if defined?($_resource_attribute_calls) && !method.to_s.ends_with?('=') - $_resource_attribute_calls[self.class] ||= {} - $_resource_attribute_calls[self.class][method] ||= 0 - $_resource_attribute_calls[self.class][method] += 1 + $_resource_attribute_calls[self.class.to_s] ||= {} + $_resource_attribute_calls[self.class.to_s][method] ||= 0 + $_resource_attribute_calls[self.class.to_s][method] += 1 end relationship_definition = relationship_definition_for(method) From a2ce8a1460eeb4bd7f562adf190c2c1688f293fd Mon Sep 17 00:00:00 2001 From: Rob Aldred Date: Tue, 3 Aug 2021 13:50:19 +0100 Subject: [PATCH 9/9] included passes base --- lib/json_api_client/helpers/debug.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json_api_client/helpers/debug.rb b/lib/json_api_client/helpers/debug.rb index 9de49a9..7c9f004 100644 --- a/lib/json_api_client/helpers/debug.rb +++ b/lib/json_api_client/helpers/debug.rb @@ -1,7 +1,7 @@ module JsonApiClient module Helpers module Debug - def self.included + def self.included(_base) $_resource_attribute_calls = {} end