Sha256: 6f3ab9703e8ec2f2dabf067f980fe7ec41589b95327df51aa1e24e446f5dffa7

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 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)
        @hash = values
        @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)
        @values[key.to_s]
      end

      # Get the original Ruby hash
      # @return [Hash] the original values hash
      def to_h
        @hash
      end

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

      private

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphql-0.10.9 lib/graphql/query/arguments.rb
graphql-0.10.8 lib/graphql/query/arguments.rb
graphql-0.10.7 lib/graphql/query/arguments.rb
graphql-0.10.6 lib/graphql/query/arguments.rb
graphql-0.10.5 lib/graphql/query/arguments.rb
graphql-0.10.4 lib/graphql/query/arguments.rb