Skip to content

Commit

Permalink
fix(Query::Arguments) to_h unwraps Arguments recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Feb 11, 2016
1 parent 6b66d7e commit 6e3d6f0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
46 changes: 39 additions & 7 deletions lib/graphql/query/arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Arguments
extend Forwardable

def initialize(values)
@hash = values
@values = values.inject({}) do |memo, (inner_key, inner_value)|
@original_values = values
@argument_values = values.inject({}) do |memo, (inner_key, inner_value)|
memo[inner_key.to_s] = wrap_value(inner_value)
memo
end
Expand All @@ -17,28 +17,60 @@ def initialize(values)
# @param [String, Symbol] name or index of value to access
# @return [Object] the argument at that key
def [](key)
@values[key.to_s]
@argument_values[key.to_s]
end

# Get the original Ruby hash
# @return [Hash] the original values hash
def to_h
@hash
@unwrapped_values ||= unwrap_value(@original_values)
end

def_delegators :@values, :keys, :values, :each
def_delegators :string_key_values, :keys, :values, :each

private

def wrap_value(value)
if value.is_a?(Array)
case value
when Array
value.map { |item| wrap_value(item) }
elsif value.is_a?(Hash)
when Hash
self.class.new(value)
else
value
end
end

def unwrap_value(value)
case value
when Array
value.map { |item| unwrap_value(item) }
when Hash
value.inject({}) do |memo, (key, value)|
memo[key] = unwrap_value(value)
memo
end
when GraphQL::Query::Arguments
value.to_h
else
value
end
end

def string_key_values
@string_key_values ||= stringify_keys(to_h)
end

def stringify_keys(value)
case value
when Hash
value.inject({}) { |memo, (k, v)| memo[k.to_s] = stringify_keys(v); memo }
when Array
value.map { |v| stringify_keys(v) }
else
value
end
end
end
end
end
10 changes: 5 additions & 5 deletions spec/graphql/query/arguments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
require "spec_helper"

describe GraphQL::Query::Arguments do
let(:arguments) { GraphQL::Query::Arguments.new({ a: 1, b: 2 }) }
let(:arguments) { GraphQL::Query::Arguments.new({ a: 1, b: 2, c: GraphQL::Query::Arguments.new({ d: 3, e: 4}) }) }

it 'returns keys as strings' do
assert_equal(['a', 'b'], arguments.keys)
assert_equal(['a', 'b', 'c'], arguments.keys)
end

it 'delegates values to values hash' do
assert_equal([1, 2], arguments.values)
assert_equal([1, 2, {'d' => 3, 'e' => 4}], arguments.values)
end

it 'delegates each to values hash' do
pairs = []
arguments.each do |key, value|
pairs << [key, value]
end
assert_equal([['a', 1], ['b', 2]], pairs)
assert_equal([['a', 1], ['b', 2], ['c', {'d' => 3, 'e' => 4}]], pairs)
end

it 'returns original Ruby hash values with to_h' do
assert_equal({ a: 1, b: 2 }, arguments.to_h)
assert_equal({ a: 1, b: 2, c: { d: 3, e: 4 } }, arguments.to_h)
end
end

0 comments on commit 6e3d6f0

Please sign in to comment.