class Serverkit::Recipe

Attributes

recipe_data[R]
variables_path[R]

Public Class Methods

new(recipe_data, variables_path = nil) click to toggle source

@param [Hash] recipe_data @param [String, nil] variables_path Used for recipe resource to render ERB template

# File lib/serverkit/recipe.rb, line 12
def initialize(recipe_data, variables_path = nil)
  @recipe_data = recipe_data
  @variables_path = variables_path
end

Public Instance Methods

errors() click to toggle source

@return [Array<Serverkit::Errors::Base>]

# File lib/serverkit/recipe.rb, line 18
def errors
  @errors ||= begin
    if !has_valid_typed_recipe_data?
      errors_for_invalid_typed_recipe_data
    elsif !has_valid_typed_resources_property?
      errors_for_invalid_typed_resources_property
    else
      errors_in_resources
    end
  end
end
handlers() click to toggle source

@return [Array<Serverkit::Resource>]

# File lib/serverkit/recipe.rb, line 31
def handlers
  @handlers ||= Array(handlers_property).flat_map do |attributes|
    ResourceBuilder.new(self, attributes).build.to_a
  end
end
merge(recipe) click to toggle source

@param [Serverkit::Recipe] recipe @return [Serverkit::Recipe]

# File lib/serverkit/recipe.rb, line 39
def merge(recipe)
  self.class.new(
    recipe_data.deep_merge(recipe.recipe_data) do |key, a, b|
      if a.is_a?(Array)
        a | b
      else
        b
      end
    end
  )
end
resources() click to toggle source

@note recipe resource will be expanded into resources defined in its recipe @return [Array<Serverkit::Resources::Base>]

# File lib/serverkit/recipe.rb, line 53
def resources
  @resources ||= resources_property.flat_map do |attributes|
    ResourceBuilder.new(self, attributes).build.to_a
  end
end
to_hash() click to toggle source

@return [Hash] Fully-expanded recipe data

# File lib/serverkit/recipe.rb, line 60
def to_hash
  @recipe_data.merge("resources" => resources.map(&:attributes))
end
valid?() click to toggle source
# File lib/serverkit/recipe.rb, line 64
def valid?
  errors.empty?
end

Private Instance Methods

errors_for_invalid_typed_recipe_data() click to toggle source

@return [Array<Serverkit::Errors::Base>]

# File lib/serverkit/recipe.rb, line 71
def errors_for_invalid_typed_recipe_data
  [Errors::InvalidRecipeTypeError.new(@recipe_data.class)]
end
errors_for_invalid_typed_resources_property() click to toggle source

@return [Array<Serverkit::Errors::Base>]

# File lib/serverkit/recipe.rb, line 76
def errors_for_invalid_typed_resources_property
  [Errors::InvalidResourcesTypeError.new(resources_property.class)]
end
errors_in_resources() click to toggle source

@return [Array<Serverkit::Errors::AttributeValidationError>]

# File lib/serverkit/recipe.rb, line 81
def errors_in_resources
  resources.flat_map(&:all_errors)
end
handlers_property() click to toggle source

@return [Array<String>, nil]

# File lib/serverkit/recipe.rb, line 86
def handlers_property
  @recipe_data["handlers"]
end
has_valid_typed_recipe_data?() click to toggle source
# File lib/serverkit/recipe.rb, line 94
def has_valid_typed_recipe_data?
  @recipe_data.is_a?(Hash)
end
has_valid_typed_resources_property?() click to toggle source
# File lib/serverkit/recipe.rb, line 90
def has_valid_typed_resources_property?
  resources_property.is_a?(Array)
end
resources_property() click to toggle source

@return [Array<String>, nil]

# File lib/serverkit/recipe.rb, line 99
def resources_property
  @recipe_data["resources"]
end