Class: Itamae::RecipeChildren

Inherits:
Array
  • Object
show all
Defined in:
lib/itamae/recipe_children.rb

Constant Summary collapse

NotFoundError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#find_recipe_by_path(path) ⇒ Object



25
26
27
28
29
# File 'lib/itamae/recipe_children.rb', line 25

def find_recipe_by_path(path)
  recipes.find do |recipe|
    recipe.path == path
  end
end

#find_resource_by_description(desc) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/itamae/recipe_children.rb', line 5

def find_resource_by_description(desc)
  # desc is like 'resource_type[name]'
  resources.find do |resource|
    type, name = Itamae::Resource.parse_description(desc)
    resource.resource_type == type && resource.resource_name == name
  end.tap do |resource|
    unless resource
      raise NotFoundError, "'#{desc}' resource is not found."
    end
  end
end

#recipesObject



42
43
44
45
46
47
48
# File 'lib/itamae/recipe_children.rb', line 42

def recipes
  self.select do |item|
    item.is_a?(Recipe)
  end.map do |recipe|
    [recipe] + recipe.children.recipes
  end.flatten
end

#resourcesObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/itamae/recipe_children.rb', line 31

def resources
  self.map do |item|
    case item
    when Resource::Base
      item
    when Recipe
      item.children.resources
    end
  end.flatten
end

#run(options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/itamae/recipe_children.rb', line 50

def run(options)
  self.each do |resource|
    case resource
    when Resource::Base
      resource.run(nil, dry_run: options[:dry_run])
    when Recipe
      resource.run(options)
    end
  end
end

#subscribing(target) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/itamae/recipe_children.rb', line 17

def subscribing(target)
  resources.map do |resource|
    resource.subscriptions.select do |subscription|
      subscription.resource == target
    end
  end.flatten
end