Sha256: 985c55cb043b2e2af8e2bb44d6bbe3e2deef7ec45f69fd78916efad696f7c6c3

Contents?: true

Size: 1.38 KB

Versions: 30

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Kitchen
  # A place to store labeled items during recipe work.  Essentially, a slightly
  # improved hash.
  #
  class Pantry
    include Enumerable

    # Adds an item to the pantry with the provided label
    #
    # @param item [Object] something to store
    # @param label [String, Symbol] a label with which to retrieve this item later.
    def store(item, label:)
      @hash[label.to_sym] = item
    end

    # Get an item from the pantry
    #
    # @param label [String, Symbol] the item's label
    # @return [Object]
    #
    def get(label)
      @hash[label.to_sym]
    end

    # Get an item from the pantry, raising if not present
    #
    # @param label [String, Symbol] the item's label
    # @raise [RecipeError] if there's no item for the label
    # @return [Object]
    #
    def get!(label)
      get(label) || raise(RecipeError, "There is no pantry item labeled '#{label}'")
    end

    # Iterate over the pantry items
    #
    # @yield Gives each label and item pair to the block
    # @yieldparam label [Symbol] the item's label
    # @yieldparam item [Object] the item
    #
    def each(&block)
      @hash.each { |k, v| block.call(k, v) }
    end

    # Returns the number of items in the pantry
    #
    # @return [Integer]
    #
    def size
      @hash.keys.size
    end

    protected

    def initialize
      @hash = {}
    end

  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
openstax_kitchen-19.0.0 lib/kitchen/pantry.rb
openstax_kitchen-18.0.0 lib/kitchen/pantry.rb
openstax_kitchen-17.1.0 lib/kitchen/pantry.rb
openstax_kitchen-17.0.0 lib/kitchen/pantry.rb
openstax_kitchen-16.0.0 lib/kitchen/pantry.rb
openstax_kitchen-15.0.0 lib/kitchen/pantry.rb
openstax_kitchen-14.0.0 lib/kitchen/pantry.rb
openstax_kitchen-13.0.0 lib/kitchen/pantry.rb
openstax_kitchen-12.2.0 lib/kitchen/pantry.rb
openstax_kitchen-12.1.0 lib/kitchen/pantry.rb
openstax_kitchen-12.0.0 lib/kitchen/pantry.rb
openstax_kitchen-11.2.0 lib/kitchen/pantry.rb
openstax_kitchen-11.1.0 lib/kitchen/pantry.rb
openstax_kitchen-11.0.0 lib/kitchen/pantry.rb
openstax_kitchen-10.0.0 lib/kitchen/pantry.rb
openstax_kitchen-9.2.0 lib/kitchen/pantry.rb
openstax_kitchen-9.1.0 lib/kitchen/pantry.rb
openstax_kitchen-9.0.0 lib/kitchen/pantry.rb
openstax_kitchen-8.0.1 lib/kitchen/pantry.rb
openstax_kitchen-8.0.0 lib/kitchen/pantry.rb