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)


72
73
74
75
# File 'lib/aws/templates/utils/parametrized.rb', line 72

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

Instance Attribute Details

#constraintObject

Returns the value of attribute constraint.



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

def constraint
  @constraint
end

#descriptionObject

Returns the value of attribute description.



32
33
34
# File 'lib/aws/templates/utils/parametrized.rb', line 32

def description
  @description
end

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#transformObject

Returns the value of attribute transform.



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

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



87
88
89
# File 'lib/aws/templates/utils/parametrized.rb', line 87

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

#getter(instance = nil) ⇒ Object



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

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



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

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