Sha256: 305bfcfc5b37de264535f2041d7828cf057e02a1e8cbb3be78cb8fd196205195

Contents?: true

Size: 1.81 KB

Versions: 22

Compression:

Stored size: 1.81 KB

Contents

module GraphQL
  class Query
    # Read-only access to values, normalizing all keys to strings
    #
    # {Arguments} recursively wraps the input in {Arguments} instances.
    class Arguments
      extend Forwardable

      def initialize(values)
        @original_values = values
        @argument_values = values.inject({}) do |memo, (inner_key, inner_value)|
          memo[inner_key.to_s] = wrap_value(inner_value)
          memo
        end
      end

      # @param [String, Symbol] name or index of value to access
      # @return [Object] the argument at that key
      def [](key)
        @argument_values[key.to_s]
      end

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

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

      private

      def wrap_value(value)
        case value
        when Array
          value.map { |item| wrap_value(item) }
        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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
graphql-0.18.4 lib/graphql/query/arguments.rb
graphql-0.18.3 lib/graphql/query/arguments.rb
graphql-0.18.2 lib/graphql/query/arguments.rb
graphql-0.18.1 lib/graphql/query/arguments.rb
graphql-0.18.0 lib/graphql/query/arguments.rb
graphql-0.17.2 lib/graphql/query/arguments.rb
graphql-0.17.1 lib/graphql/query/arguments.rb
graphql-0.17.0 lib/graphql/query/arguments.rb
graphql-0.16.1 lib/graphql/query/arguments.rb
graphql-0.16.0 lib/graphql/query/arguments.rb
graphql-0.15.3 lib/graphql/query/arguments.rb
graphql-0.15.2 lib/graphql/query/arguments.rb
graphql-0.14.2 lib/graphql/query/arguments.rb
graphql-0.15.1 lib/graphql/query/arguments.rb
graphql-0.15.0 lib/graphql/query/arguments.rb
graphql-0.14.1 lib/graphql/query/arguments.rb
graphql-0.14.0 lib/graphql/query/arguments.rb
graphql-0.13.0 lib/graphql/query/arguments.rb
graphql-0.12.1 lib/graphql/query/arguments.rb
graphql-0.12.0 lib/graphql/query/arguments.rb