Class: Cfer::Core::Resource
Defined Under Namespace
Classes: Handle
Constant Summary collapse
- @@types =
{}
Constants inherited from BlockHash
BlockHash::NON_PROXIED_METHODS
Instance Attribute Summary collapse
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Class Method Summary collapse
-
.after(type, **options, &block) ⇒ Object
Registers a hook that will be run after properties have been set on a resource.
-
.before(type, **options, &block) ⇒ Object
Registers a hook that will be run before properties are set on a resource.
-
.extend_resource(type, &block) ⇒ Object
Patches code into DSL classes for CloudFormation resources.
-
.resource_class(type) ⇒ Class
Fetches the DSL class for a CloudFormation resource type.
Instance Method Summary collapse
-
#get_property(key) ⇒ Object
Gets the current value of a given property.
- #handle ⇒ Object
-
#initialize(name, type, stack, options, &block) ⇒ Resource
constructor
A new instance of Resource.
-
#properties(keyvals = {}) ⇒ Object
Directly sets raw properties in the underlying CloudFormation structure.
-
#tag(k, v, **options) ⇒ Object
Sets a tag on this resource.
Methods included from Hooks
included, #post_block, #pre_block
Methods inherited from BlockHash
Methods inherited from Block
#build_from_block, #build_from_file, #build_from_string, #include_file, #post_block, #pre_block
Constructor Details
#initialize(name, type, stack, options, &block) ⇒ Resource
Returns a new instance of Resource.
24 25 26 27 28 29 30 31 32 |
# File 'lib/cfer/core/resource.rb', line 24 def initialize(name, type, stack, , &block) @name = name @stack = stack self[:Type] = type self.merge!() self[:Properties] = HashWithIndifferentAccess.new build_from_block(&block) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Cfer::BlockHash
Instance Attribute Details
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
22 23 24 |
# File 'lib/cfer/core/resource.rb', line 22 def stack @stack end |
Class Method Details
.after(type, **options, &block) ⇒ Object
Registers a hook that will be run after properties have been set on a resource
82 83 84 |
# File 'lib/cfer/core/resource.rb', line 82 def after(type, **, &block) resource_class(type).post_hooks << .merge(block: block) end |
.before(type, **options, &block) ⇒ Object
Registers a hook that will be run before properties are set on a resource
76 77 78 |
# File 'lib/cfer/core/resource.rb', line 76 def before(type, **, &block) resource_class(type).pre_hooks << .merge(block: block) end |
.extend_resource(type, &block) ⇒ Object
Patches code into DSL classes for CloudFormation resources
70 71 72 |
# File 'lib/cfer/core/resource.rb', line 70 def extend_resource(type, &block) resource_class(type).class_eval(&block) end |
.resource_class(type) ⇒ Class
Fetches the DSL class for a CloudFormation resource type
64 65 66 |
# File 'lib/cfer/core/resource.rb', line 64 def resource_class(type) @@types[type] ||= "CferExt::#{type}".split('::').inject(Object) { |o, c| o.const_get c if o && o.const_defined?(c) } || Class.new(Cfer::Core::Resource) end |
Instance Method Details
#get_property(key) ⇒ Object
Gets the current value of a given property
56 57 58 |
# File 'lib/cfer/core/resource.rb', line 56 def get_property(key) self[:Properties].fetch key end |
#handle ⇒ Object
34 35 36 |
# File 'lib/cfer/core/resource.rb', line 34 def handle @handle ||= Handle.new(@name) end |
#properties(keyvals = {}) ⇒ Object
Directly sets raw properties in the underlying CloudFormation structure.
50 51 52 |
# File 'lib/cfer/core/resource.rb', line 50 def properties(keyvals = {}) self[:Properties].merge!(keyvals) end |
#tag(k, v, **options) ⇒ Object
Sets a tag on this resource. The resource must support the CloudFormation Tags
property.
42 43 44 45 46 |
# File 'lib/cfer/core/resource.rb', line 42 def tag(k, v, **) self[:Properties][:Tags] ||= [] self[:Properties][:Tags].delete_if { |kv| kv["Key"] == k } self[:Properties][:Tags].unshift({"Key" => k, "Value" => v}.merge()) end |