Sha256: e8d7e5e005be9c41acc4320739db1c6dd93160c66609b67fa41b6f3ab9ac61ea

Contents?: true

Size: 919 Bytes

Versions: 15

Compression:

Stored size: 919 Bytes

Contents

# frozen_string_literal: true

require_relative 'base_inheritable'

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

        [].tap do |value|
          value.concat(inherited_value)
          value.concat(new_value)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
grape-2.0.0 lib/grape/util/stackable_values.rb
grape-1.8.0 lib/grape/util/stackable_values.rb
grape-1.7.1 lib/grape/util/stackable_values.rb
grape-1.7.0 lib/grape/util/stackable_values.rb
grape-1.6.2 lib/grape/util/stackable_values.rb
grape-1.6.1 lib/grape/util/stackable_values.rb
grape-1.6.0 lib/grape/util/stackable_values.rb
grape-1.5.3 lib/grape/util/stackable_values.rb
grape-1.5.2 lib/grape/util/stackable_values.rb
grape-1.5.1 lib/grape/util/stackable_values.rb
grape-1.5.0 lib/grape/util/stackable_values.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/grape-1.4.0/lib/grape/util/stackable_values.rb
grape-1.4.0 lib/grape/util/stackable_values.rb
grape-1.3.3 lib/grape/util/stackable_values.rb
grape-1.3.2 lib/grape/util/stackable_values.rb