Class: Aws::Templates::Utils::Dependency

Inherits:
BasicObject
Includes:
Inspectable
Defined in:
lib/aws/templates/utils/dependency.rb,
lib/aws/templates/utils/dependency/refinements.rb

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

Defined Under Namespace

Modules: Refinements

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspectable

#inspect, #to_s

Constructor Details

#initialize(source_object) ⇒ Dependency

Initialize the proxy



113
114
115
116
117
118
119
120
121
# File 'lib/aws/templates/utils/dependency.rb', line 113

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



62
63
64
# File 'lib/aws/templates/utils/dependency.rb', line 62

def method_missing(name, *args, &block)
  object.respond_to?(name) ? object.send(name, *args, &block) : super
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



56
57
58
# File 'lib/aws/templates/utils/dependency.rb', line 56

def dependencies
  @dependencies
end

#objectObject (readonly) Also known as: not_a_dependency

Returns the value of attribute object.



55
56
57
# File 'lib/aws/templates/utils/dependency.rb', line 55

def object
  @object
end

Instance Method Details

#!=(other) ⇒ Object

Non-equality



36
37
38
# File 'lib/aws/templates/utils/dependency.rb', line 36

def !=(other)
  !eql?(other)
end

#==(other) ⇒ Object

Alias for #eql?



31
32
33
# File 'lib/aws/templates/utils/dependency.rb', line 31

def ==(other)
  eql?(other)
end

#as_a_dependencyObject

mark the object as dependency



51
52
53
# File 'lib/aws/templates/utils/dependency.rb', line 51

def as_a_dependency
  self
end

#classObject

BasicObject is so basic that this part is missing too



41
42
43
# File 'lib/aws/templates/utils/dependency.rb', line 41

def class
  Dependency
end

#dependency?Boolean

It’s a dependency

Returns:

  • (Boolean)


46
47
48
# File 'lib/aws/templates/utils/dependency.rb', line 46

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.

Returns:

  • (Boolean)


25
26
27
# File 'lib/aws/templates/utils/dependency.rb', line 25

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/aws/templates/utils/dependency.rb', line 68

def respond_to_missing?(name, include_private = false)
  object.respond_to?(name, include_private)
end

#to(target) ⇒ Object

Add dependency

Add a link to the target to the current Dependency object



76
77
78
79
80
81
82
83
84
# File 'lib/aws/templates/utils/dependency.rb', line 76

def to(target)
  if target.dependency?
    dependencies.merge(target.dependencies)
  else
    dependencies << target
  end

  self
end

#to_selfObject

Set dependency to the target



107
108
109
# File 'lib/aws/templates/utils/dependency.rb', line 107

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>)


95
96
97
98
99
100
101
102
103
# File 'lib/aws/templates/utils/dependency.rb', line 95

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