Class: Cfer::Core::Resource
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.
-
#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.
9 10 11 12 13 14 15 16 17 |
# File 'lib/cfer/core/resource.rb', line 9 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.
7 8 9 |
# File 'lib/cfer/core/resource.rb', line 7 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
63 64 65 |
# File 'lib/cfer/core/resource.rb', line 63 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
57 58 59 |
# File 'lib/cfer/core/resource.rb', line 57 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
51 52 53 |
# File 'lib/cfer/core/resource.rb', line 51 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
45 46 47 |
# File 'lib/cfer/core/resource.rb', line 45 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
37 38 39 |
# File 'lib/cfer/core/resource.rb', line 37 def get_property(key) self[:Properties].fetch key end |
#properties(keyvals = {}) ⇒ Object
Directly sets raw properties in the underlying CloudFormation structure.
31 32 33 |
# File 'lib/cfer/core/resource.rb', line 31 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.
23 24 25 26 27 |
# File 'lib/cfer/core/resource.rb', line 23 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 |