Sha256: 4c51482cbbc6ed6b341ab34d4b5683a861d2ad5211bba1280ca5945dc9783a61

Contents?: true

Size: 1.39 KB

Versions: 47

Compression:

Stored size: 1.39 KB

Contents

module Itamae
  class RecipeChildren < Array
    NotFoundError = Class.new(StandardError)

    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

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

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

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

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

    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
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
itamae-1.0.0.beta33 lib/itamae/recipe_children.rb
itamae-1.0.0.beta32 lib/itamae/recipe_children.rb
itamae-1.0.0.beta31 lib/itamae/recipe_children.rb
itamae-1.0.0.beta30 lib/itamae/recipe_children.rb
itamae-1.0.0.beta29 lib/itamae/recipe_children.rb
itamae-1.0.0.beta28 lib/itamae/recipe_children.rb
itamae-1.0.0.beta27 lib/itamae/recipe_children.rb