Class: Aws::Templates::Utils::Parametrized::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/templates/utils/parametrized.rb

Overview

Parameter object

The object incorporates parameter specification and basic logic for value extraction, checking and transformation. Parameter objects are created at each parameter description.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, enclosing_class, specification = {}) ⇒ Parameter

Create a parameter object with given specification

  • name - parameter name

  • enclosing_class - the class the parameter was declared at

  • specification - parameter specification; it includes:

** :description - human-readable parameter description ** :getter - getter Proc which will be used for parameter extraction

from input hash or plain value. The Proc shouldn't
expect any arguments and it will be executed in
the instance context. If a plain value is passed
it will be used as is. If the argument is not specified
then value will be extracted from the input hash by
the parameter name (see Getter for more information)

** :transform - transform Proc which will be used for transforming

extracted value. It should expect single parameter
and it will be executed in instance context.
if not specified not transformation will be
performed (see Transformation for more information)

** :constraint - constraint Proc which will be used to check

the value after transformation step. The Proc
is expected to receive one arg and throw an
exception if constraints are not met
(see Constraint for more information)


70
71
72
73
# File 'lib/aws/templates/utils/parametrized.rb', line 70

def initialize(name, enclosing_class, specification = {})
  @name = name
  set_specification(enclosing_class, specification)
end

Instance Attribute Details

#constraintObject

Returns the value of attribute constraint.



43
44
45
# File 'lib/aws/templates/utils/parametrized.rb', line 43

def constraint
  @constraint
end

#descriptionObject

Returns the value of attribute description.



30
31
32
# File 'lib/aws/templates/utils/parametrized.rb', line 30

def description
  @description
end

#klassObject

Returns the value of attribute klass.



44
45
46
# File 'lib/aws/templates/utils/parametrized.rb', line 44

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/aws/templates/utils/parametrized.rb', line 29

def name
  @name
end

#transformObject

Returns the value of attribute transform.



42
43
44
# File 'lib/aws/templates/utils/parametrized.rb', line 42

def transform
  @transform
end

Instance Method Details

#get(instance) ⇒ Object

Get the parameter value from the instance

It is used internally in auto-generated accessors to get the value from input hash. The method extracts value from the hash and pushes it through transformation and constraint stages. Also, you can specify value as the optional parameter so getter even if present will be ignored. It relies on presence of options accessor in the instance.

  • instance - instance to extract the parameter value from



85
86
87
# File 'lib/aws/templates/utils/parametrized.rb', line 85

def get(instance)
  process_value(instance, extract_value(instance))
end

#getter(instance = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/aws/templates/utils/parametrized.rb', line 32

def getter(instance = nil)
  @getter || (
    instance &&
    (
      (instance.respond_to?(:getter) && instance.getter) ||
      (instance.class.respond_to?(:getter) && instance.class.getter)
    )
  )
end

#process_value(instance, value) ⇒ Object



89
90
91
92
93
# File 'lib/aws/templates/utils/parametrized.rb', line 89

def process_value(instance, value)
  value = instance.instance_exec(self, value, &transform) if transform
  instance.instance_exec(self, value, &constraint) if constraint
  value
end