Sha256: 8f27ac2f69965de87c873a1f1f5d4381aa499633aed492328e9cdf045aaea2e3

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

require 'singleton'

module Playmo

  # This class contains all registered recipes.
  # You can register own recipe in this class
  class Cookbook
    include Enumerable
    include Singleton

    attr_accessor :recipes

    def initialize
      @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

    def delete_all
      self.recipes = []
    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, options)
      recipes.each { |recipe| recipe.cook!(application_name) }

      if options['dry-run']
        puts "Recipes execution order:"
        recipes.each_with_index { |recipe, i| puts "#{i+1}. #{recipe.name}" }
      else
        Playmo::Action.execute_all # Execute all actions
      end
    end

    def find_recipe(recipe_symbol)
      recipes.find { |recipe| recipe.name == recipe_symbol }
    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

8 entries across 8 versions & 1 rubygems

Version Path
playmo-0.1.10 lib/playmo/cookbook.rb
playmo-0.1.9 lib/playmo/cookbook.rb
playmo-0.1.8 lib/playmo/cookbook.rb
playmo-0.1.7 lib/playmo/cookbook.rb
playmo-0.1.6 lib/playmo/cookbook.rb
playmo-0.1.5 lib/playmo/cookbook.rb
playmo-0.1.4 lib/playmo/cookbook.rb
playmo-0.1.3 lib/playmo/cookbook.rb