Sha256: 254b4ec441ba6bf43ce8943cd483291c155cec774b5975be00b3ad1f0e8c1fe4

Contents?: true

Size: 1.78 KB

Versions: 7

Compression:

Stored size: 1.78 KB

Contents

module Playmo

  # This class contains all registered recipes.
  # You can register own recipe in this class
  class Cookbook
    include Enumerable
    attr_accessor :recipes, :cooked_recipes

    def self.instance
      @@instance ||= Playmo::Cookbook.new
    end

    def initialize
      @recipes = []
      @cooked_recipes = []
    end

    def each
      recipes.each { |x| yield x }
    end

    def size
      recipes.size
    end

    def last
      recipes.last
    end

    def [](i)
      recipes[i]
    end

    def delete(target)
      recipes.delete target
    end

    # Is recipe already cooked?
    def cooked?(recipe)
      @cooked_recipes.include?(recipe)
    end
    
    # Adds the new recipe before the specified existing recipe in the Cookbook stack.
    def insert(existing_recipe, new_recipe)
      index = assert_index(existing_recipe, :before)
      recipes.insert(index, new_recipe)
    end

    alias_method :insert_before, :insert

    # Adds the new recipe after the specified existing recipe in the Cookbook stack.
    def insert_after(existing_recipe, new_recipe)
      index = assert_index(existing_recipe, :after)
      insert(index + 1, new_recipe)
    end

    # Adds the new recipe at the bottom of the Cookbook stack.
    def use(new_recipe)
      recipes.push(new_recipe)
    end

    def cook_recipes!(application_name)
      prepared_recipes = []
      
      recipes.each do |recipe|
        prepared_recipes << recipe.new
      end

      prepared_recipes.each do |recipe|
        recipe.cook!(application_name)
        @cooked_recipes << recipe
      end
    end
    
  protected

    def assert_index(index, where)
      i = index.is_a?(Integer) ? index : recipes.index(index)
      raise "No such recipe to insert #{where}: #{index.inspect}" unless i
      i
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
playmo-0.0.18 lib/playmo/cookbook.rb
playmo-0.0.17 lib/playmo/cookbook.rb
playmo-0.0.14 lib/playmo/cookbook.rb
playmo-0.0.13 lib/playmo/cookbook.rb
playmo-0.0.12 lib/playmo/cookbook.rb
playmo-0.0.11 lib/playmo/cookbook.rb
playmo-0.0.10 lib/playmo/cookbook.rb