Module: Pawnee::Base::Roles::ClassMethods

Defined in:
lib/pawnee/pawnee/roles.rb

Instance Method Summary collapse

Instance Method Details

#class_roleObject



14
15
16
# File 'lib/pawnee/pawnee/roles.rb', line 14

def class_role
  @role.to_s
end

#invoke_roles(task_name, roles, options = {}) ⇒ Object

Invokes all recipes that implement the passed in role



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pawnee/pawnee/roles.rb', line 61

def invoke_roles(task_name, roles, options={})   
  role_classes = self.recipe_classes_with_roles(roles)

  # Run the taks on each role class
  role_classes.each do |recipe_class|
    # This class matches the role, so we should run it
    recipe = recipe_class.new([], options)

    task = recipe_class.tasks[task_name.to_s]
    recipe.invoke_task(task)
    
    # Copy back and updated options
    options = recipe.options
  end
end

#ordered_recipesObject

Returns the recipe classes in order based on the Gemfile order



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pawnee/pawnee/roles.rb', line 19

def ordered_recipes
  return @ordered_recipes if @ordered_recipes
  names = Bundler.load.dependencies.map(&:name)

  # Setup a hash with the recipe name and the recipe class
  recipe_pool = recipes.dup.inject({}) {|memo,recipe| memo[recipe.gem_name] = recipe ; memo }

  # Go through the gems in the order they are in the Gemfile, then
  # add them to the ordered list
  @ordered_recipes = []
  names.each do |name|
    if recipe_pool[name]
      @ordered_recipes << recipe_pool[name]
      recipe_pool.delete(name)
    end
  end

  # Add the remaining recipes (load them after everything else)
  @ordered_recipes += recipe_pool.values

  return @ordered_recipes
end

#recipe_classes_with_roles(roles) ⇒ Object

Returns the list of classes that match the current list of roles in the correct run order



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pawnee/pawnee/roles.rb', line 44

def recipe_classes_with_roles(roles)
  # Check to make sure some recipes have been added
  if ordered_recipes.size == 0
    raise Thor::InvocationError, 'no recipes have been defined'
  end
  if (roles.is_a?(Array) && roles.size == 0) || roles == :all
    # Use all classes
    role_classes = ordered_recipes
  else
    # Remove classes that don't fit the roles being used
    role_classes = ordered_recipes.reject do |recipe_class|
      ![roles].flatten.map(&:to_s).include?(recipe_class.class_role)
    end
  end        
end

#role(role_name) ⇒ Object

Assigns the role for this class



10
11
12
# File 'lib/pawnee/pawnee/roles.rb', line 10

def role(role_name)
  @role = role_name
end