Sha256: 1b9fcb771025c3975ac727386ab87ef60036ab8d359a7f317c22af84e72cf553

Contents?: true

Size: 806 Bytes

Versions: 6

Compression:

Stored size: 806 Bytes

Contents

# frozen_string_literal: true

module Grape
  module Util
    class StackableValues < BaseInheritable
      # Even if there is no value, an empty array will be returned.
      def [](name)
        inherited_value = inherited_values[name]
        new_value = new_values[name]

        return new_value || [] unless inherited_value

        concat_values(inherited_value, new_value)
      end

      def []=(name, value)
        new_values[name] ||= []
        new_values[name].push value
      end

      def to_hash
        keys.each_with_object({}) do |key, result|
          result[key] = self[key]
        end
      end

      protected

      def concat_values(inherited_value, new_value)
        return inherited_value unless new_value

        inherited_value + new_value
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
grape-2.3.0 lib/grape/util/stackable_values.rb
grape-2.2.0 lib/grape/util/stackable_values.rb
grape-2.1.3 lib/grape/util/stackable_values.rb
grape-2.1.2 lib/grape/util/stackable_values.rb
grape-2.1.1 lib/grape/util/stackable_values.rb
grape-2.1.0 lib/grape/util/stackable_values.rb