lib/graphql/remote_loader/loader.rb in graphql-remote_loader-0.0.2 vs lib/graphql/remote_loader/loader.rb in graphql-remote_loader-0.0.3

- old
+ new

@@ -17,10 +17,14 @@ prime = Prime.take(@index - 1).last self.for.load([query, prime]) end + def self.reset_index + @index = nil + end + # Given a query string, return a response JSON def query(query_string) raise NotImplementedError, "RemoteLoader::Loader should be subclassed and #query must be defined" end @@ -39,11 +43,11 @@ def filter_keys_on_response(obj, prime) return obj unless obj.is_a? Hash filtered_results = {} # Select field keys on the results hash - fields = obj.keys.select { |k| k.match? /\Ap[0-9]+.*[^?]\z/ } + fields = obj.keys.select { |k| k.match /\Ap[0-9]+.*[^?]\z/ } # Filter methods that were not requested in this sub-query fields = fields.select do |field| prime_factor = field.match(/\Ap([0-9]+)/)[1].to_i (prime_factor % prime) == 0 @@ -54,13 +58,21 @@ method_name = method.match(/\Ap[0-9]+(.*)/)[1] method_value = obj[method] filtered_value = filter_keys_on_response(method_value, prime) - filtered_results[method_name.underscore] = filtered_value + filtered_results[underscore(method_name)] = filtered_value end filtered_results + end + + def underscore(str) + str.gsub(/::/, '/'). + gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). + gsub(/([a-z\d])([A-Z])/,'\1_\2'). + tr("-", "_"). + downcase end end end end