Class: Serverkit::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/serverkit/recipe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe_data, variables_path = nil) ⇒ Recipe

Returns a new instance of Recipe.

Parameters:

  • recipe_data (Hash)
  • variables_path (String, nil) (defaults to: nil)

    Used for recipe resource to render ERB template



12
13
14
15
# File 'lib/serverkit/recipe.rb', line 12

def initialize(recipe_data, variables_path = nil)
  @recipe_data = recipe_data
  @variables_path = variables_path
end

Instance Attribute Details

#recipe_dataObject (readonly)

Returns the value of attribute recipe_data.



8
9
10
# File 'lib/serverkit/recipe.rb', line 8

def recipe_data
  @recipe_data
end

#variables_pathObject (readonly)

Returns the value of attribute variables_path.



8
9
10
# File 'lib/serverkit/recipe.rb', line 8

def variables_path
  @variables_path
end

Instance Method Details

#errorsArray<Serverkit::Errors::Base>

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/serverkit/recipe.rb', line 18

def errors
  @errors ||= begin
    case
    when !has_valid_typed_recipe_data?
      errors_for_invalid_typed_recipe_data
    when !has_valid_typed_resources_property?
      errors_for_invalid_typed_resources_property
    else
      errors_in_resources
    end
  end
end

#handlersArray<Serverkit::Resource>

Returns:

  • (Array<Serverkit::Resource>)


32
33
34
35
36
# File 'lib/serverkit/recipe.rb', line 32

def handlers
  @handlers ||= Array(handlers_property).flat_map do |attributes|
    ResourceBuilder.new(self, attributes).build.to_a
  end
end

#merge(recipe) ⇒ Serverkit::Recipe

Parameters:

Returns:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/serverkit/recipe.rb', line 40

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

#resourcesArray<Serverkit::Resources::Base>

Note:

recipe resource will be expanded into resources defined in its recipe

Returns:



54
55
56
57
58
# File 'lib/serverkit/recipe.rb', line 54

def resources
  @resources ||= resources_property.flat_map do |attributes|
    ResourceBuilder.new(self, attributes).build.to_a
  end
end

#to_hashHash

Returns Fully-expanded recipe data.

Returns:

  • (Hash)

    Fully-expanded recipe data



61
62
63
# File 'lib/serverkit/recipe.rb', line 61

def to_hash
  @recipe_data.merge("resources" => resources.map(&:attributes))
end

#valid?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/serverkit/recipe.rb', line 65

def valid?
  errors.empty?
end