class Aws::Templates::Utils::Parametrized::Parameter

Parameter object

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

Constants

ALLOWED_SPECIFICATION_ENTRIES

Attributes

constraint[RW]
description[RW]
klass[RW]
name[R]
transform[RW]

Public Class Methods

new(name, enclosing_class, specification = {}) click to toggle source

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)
# File lib/aws/templates/utils/parametrized.rb, line 70
def initialize(name, enclosing_class, specification = {})
  @name = name
  set_specification(enclosing_class, specification)
end

Public Instance Methods

get(instance) click to toggle source

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

# File lib/aws/templates/utils/parametrized.rb, line 85
def get(instance)
  process_value(instance, extract_value(instance))
end
getter(instance = nil) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

execute_getter(instance, getter) click to toggle source
# File lib/aws/templates/utils/parametrized.rb, line 107
def execute_getter(instance, getter)
  if getter.respond_to?(:to_hash)
    getter
  elsif getter.respond_to?(:to_proc)
    instance.instance_exec(self, &getter)
  else
    getter
  end
end
extract_value(instance) click to toggle source
# File lib/aws/templates/utils/parametrized.rb, line 97
def extract_value(instance)
  unless getter(instance)
    raise(
      Templates::Exception::ParameterGetterIsNotDefined.new(self)
    )
  end

  execute_getter(instance, getter(instance))
end
process_specification(spec) click to toggle source
# File lib/aws/templates/utils/parametrized.rb, line 135
def process_specification(spec)
  @description = spec[:description] if spec.key?(:description)
  @getter = spec[:getter] if spec.key?(:getter)
  @transform = spec[:transform] if spec.key?(:transform)
  @constraint = spec[:constraint] if spec.key?(:constraint)
end
raise_wrong_options(wrong_options) click to toggle source
# File lib/aws/templates/utils/parametrized.rb, line 131
def raise_wrong_options(wrong_options)
  raise Templates::Exception::ParameterSpecificationIsInvalid.new(self, wrong_options)
end