Class: Aws::Templates::Utils::Dependency
Overview
Dependency marker proxy
Used internally in the framework to mark an object as potential dependency. There are other alternatives for doing the same like singleton class and reference object. There are a few advantages of the approach taken:
-
Dependency can be used whereever original object is expected
-
Dependecy can be applied case-by-case basis whereas singleton is attached to the object itself
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
-
#!=(other) ⇒ Object
Non-equality.
-
#==(other) ⇒ Object
Alias for #eql?.
-
#as_a_dependency ⇒ Object
mark the object as dependency.
-
#class ⇒ Object
BasicObject is so basic that this part is missing too.
-
#dependency? ⇒ Boolean
It’s a dependency.
-
#eql?(other) ⇒ Boolean
Equality.
-
#initialize(source_object) ⇒ Dependency
constructor
Initialize the proxy.
-
#method_missing(name, *args, &block) ⇒ Object
Redirect every method call to the proxied object if the object supports it.
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
It supports every method proxied object supports.
-
#to(target) ⇒ Object
Add dependency.
-
#to_self ⇒ Object
Set dependency to the target.
-
#with(source = nil, &source_calculation_block) ⇒ Object
Link the value to the source.
Methods included from Inspectable
Constructor Details
#initialize(source_object) ⇒ Dependency
Initialize the proxy
112 113 114 115 116 117 118 119 120 |
# File 'lib/aws/templates/utils/dependency.rb', line 112 def initialize(source_object) @object = source_object.object @dependencies = if source_object.dependency? source_object.dependencies.dup else ::Set.new end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Redirect every method call to the proxied object if the object supports it
61 62 63 |
# File 'lib/aws/templates/utils/dependency.rb', line 61 def method_missing(name, *args, &block) object.respond_to?(name) ? object.send(name, *args, &block) : super end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
57 58 59 |
# File 'lib/aws/templates/utils/dependency.rb', line 57 def dependencies @dependencies end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
56 57 58 |
# File 'lib/aws/templates/utils/dependency.rb', line 56 def object @object end |
Instance Method Details
#!=(other) ⇒ Object
Non-equality
37 38 39 |
# File 'lib/aws/templates/utils/dependency.rb', line 37 def !=(other) !eql?(other) end |
#==(other) ⇒ Object
Alias for #eql?
32 33 34 |
# File 'lib/aws/templates/utils/dependency.rb', line 32 def ==(other) eql?(other) end |
#as_a_dependency ⇒ Object
mark the object as dependency
52 53 54 |
# File 'lib/aws/templates/utils/dependency.rb', line 52 def as_a_dependency self end |
#class ⇒ Object
BasicObject is so basic that this part is missing too
42 43 44 |
# File 'lib/aws/templates/utils/dependency.rb', line 42 def class Dependency end |
#dependency? ⇒ Boolean
It’s a dependency
47 48 49 |
# File 'lib/aws/templates/utils/dependency.rb', line 47 def dependency? true end |
#eql?(other) ⇒ Boolean
Equality
Two Dependency objects are equal if it’s the same object or if they are pointing to the same target.
26 27 28 |
# File 'lib/aws/templates/utils/dependency.rb', line 26 def eql?(other) equal?(other) || ((self.class == other.class) && (object == other.object)) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
It supports every method proxied object supports
67 68 69 |
# File 'lib/aws/templates/utils/dependency.rb', line 67 def respond_to_missing?(name, include_private = false) object.respond_to?(name, include_private) || super(name, include_private) end |
#to(target) ⇒ Object
Add dependency
Add a link to the target to the current Dependency object
75 76 77 78 79 80 81 82 83 |
# File 'lib/aws/templates/utils/dependency.rb', line 75 def to(target) if target.dependency? dependencies.merge(target.dependencies) else dependencies << target end self end |
#to_self ⇒ Object
Set dependency to the target
106 107 108 |
# File 'lib/aws/templates/utils/dependency.rb', line 106 def to_self to(object) end |
#with(source = nil, &source_calculation_block) ⇒ Object
Link the value to the source
Links source or result of calculation of the block to the target object of the dependency. The mecahanism is a middle ground between extreme case of indefinite recursive dependency propagation and no propagation at all
some_artifact.as_a_dependency.with { some_attribute }
# => Dependency(@object = <some_attribute value>, <link to some_artifact>)
94 95 96 97 98 99 100 101 102 |
# File 'lib/aws/templates/utils/dependency.rb', line 94 def with(source = nil, &source_calculation_block) value = if source_calculation_block.nil? source else object.instance_exec(value, &source_calculation_block) end value.as_a_dependency.to(self) end |