Class: Aws::Templates::Composite

Inherits:
Artifact
  • Object
show all
Includes:
Utils::Contextualized
Defined in:
lib/aws/templates/composite.rb

Overview

Composite

Composite is an artifact which contain other artifacts and provide DSL syntax sugar to define it

A composite can represent complex entities as CloudFormation stacks which can contain settings and infrastructure artifacts inside it. However, it’s still a single entity which may be versioned as a whole and can represent the current state of deployed application.

Composite is still an artifact and has all methods inherited so besides grouping different artifacts alltogether you can process input parameters too.

Composite is a recursive structure as a result of being an artifact so you can construct arbitrary deep hierarchies of objects. Also it supports inheritance as artifact does. So every component defined in the parent class will be initialized properly in all children too.

Instance Attribute Summary

Attributes inherited from Artifact

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Inheritable::ClassMethods

#_class_scope_defined?, #_define_class_scope, #class_scope, #included, #instance_scope

Methods inherited from Artifact

features, featuring, getter, #lookup_path, #meta, #parent, #proc, #root, to_s

Methods included from Utils::Dependent

#depends_on

Methods included from Utils::Inspectable

#inspect, #to_s

Methods included from Utils::Guarded

#guarded_for

Constructor Details

#initialize(*params, &blk) ⇒ Composite

Provisions parameters and initializes nested artifacts



142
143
144
145
146
# File 'lib/aws/templates/composite.rb', line 142

def initialize(*params, &blk)
  super(*params)
  create_components
  instance_exec(&blk) if blk
end

Class Method Details

.components(*args, &blk) ⇒ Object

Artifacts definition block for DSL

An element of the framework DSL. Allows you to define composite’s artifacts declaratively with using standard language features.



75
76
77
78
79
80
81
82
# File 'lib/aws/templates/composite.rb', line 75

def self.components(*args, &blk)
  define_method(:create_components) do
    super()
    instance_exec(*args, &blk)
  end

  self
end

.for(*args, &blk) ⇒ Object

Syntax sugar to create composite classes on spot

Create a new child class of the current class and executes a block in the context of the class object optionally passing a list of arguments to it.



101
102
103
104
105
# File 'lib/aws/templates/composite.rb', line 101

def self.for(*args, &blk)
  klass = Class.new(self)
  klass.instance_eval(*args, &blk) unless blk.nil?
  klass
end

Instance Method Details

#[](artifact_label) ⇒ Object

Shortcut for accessing artifacts by their labels

The method returns either stored artifact object or throws a descriptive exception.



47
48
49
50
51
52
53
54
# File 'lib/aws/templates/composite.rb', line 47

def [](artifact_label)
  unless artifacts.key?(artifact_label)
    raise "There is no artifact #{artifact_label}" \
      " in composite #{label}"
  end

  artifacts[artifact_label].as_a_dependency.to_self
end

#[]=(artifact_label, artifact_object) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aws/templates/composite.rb', line 56

def []=(artifact_label, artifact_object)
  if artifacts.key?(artifact_label)
    if artifacts[artifact_label] != artifact_object.object
      raise "Artifact #{artifact_label} is already present " \
        "in composite #{label}"
    end
  else
    artifacts[artifact_label] = artifact_object.object
  end

  artifact_object.as_a_dependency.to_self
end

#artifact(type, params = nil, &blk) ⇒ Object

Artifact definition constructor in DSL

Defines a single artifact in composite’s definition block. This method was designed to be used inside of composite block but you can use it elsewhere else applied on a class instance.

  • type - artifact type (class)

  • params - optional map of artifact options

  • blk - a block which will be passed to artifacts constructor;

    applications may vary but particular one is adding
    artifacts into composite during instantiation
    


119
120
121
122
123
# File 'lib/aws/templates/composite.rb', line 119

def artifact(type, params = nil, &blk)
  artifact_object = create_artifact_object(type, params, &blk)
  self[artifact_object.label] = artifact_object
  artifact_object.as_a_dependency.to_self
end

#artifactsObject

Dictionary of artifacts and their labels the composite is consisting of

Accessor returning dictionary of artifacts currently residing in composite instance with labels as keys



38
39
40
# File 'lib/aws/templates/composite.rb', line 38

def artifacts
  @artifacts ||= Templates::Utils::ArtifactStorage.new
end

#components(&blk) ⇒ Object

Add components into the composite’s instance

Analog of class-level “components” method to add components after artifact creation when using class-level definitions are not appropriate



90
91
92
93
# File 'lib/aws/templates/composite.rb', line 90

def components(&blk)
  instance_exec(&blk)
  self
end

#label_as(artifact_object, *labels) ⇒ Object

Put labels on the artifact

Put the artifact into the artifact storage under arbitrary aliases.

  • artifact_object - artifact object to put

  • labels - labels to assign to the artifact



132
133
134
135
136
137
138
# File 'lib/aws/templates/composite.rb', line 132

def label_as(artifact_object, *labels)
  labels.flatten.each do |artifact_label|
    self[artifact_label] = artifact_object
  end

  artifact_object
end

#search(search_params = {}) ⇒ Object

Find artifacts by criteria

The method allows flexible introspection of the artifacts enclosed into the composite’s storage. The method is just a proxy for the storage method with the same name

  • search_params - map of search parameters:

** recursive - if true, search will be performed recusrsively

in nested composites

** label - search for artifacts which have the label ** parameters - search for artifacts which have specified

parameters values; it's a multi-level map so
you can check for nested values also


162
163
164
# File 'lib/aws/templates/composite.rb', line 162

def search(search_params = {})
  artifacts.search(search_params)
end