Class: Highway::Steps::Parameters::Single

Inherits:
Base
  • Object
show all
Defined in:
lib/highway/steps/parameters/single.rb

Overview

This class is used in step definition classes to represent a single parameter of a step.

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#is_required?

Constructor Details

#initialize(name:, type:, required:, default: nil) ⇒ Single

Initialize an instance.

Parameters:

  • name (String)

    Name of the parameter.

  • type (Highway::Steps::Types::*)

    Type of the parameter.

  • required (Boolean)

    Whether parametr is required.

  • default (Object, nil) (defaults to: nil)

    Default value of the parameter.



24
25
26
27
28
29
30
# File 'lib/highway/steps/parameters/single.rb', line 24

def initialize(name:, type:, required:, default: nil)
  @name = name
  @required = required
  @type = type
  @default = default
  assert_default_value_valid()
end

Instance Attribute Details

#defaultObject? (readonly)

Default value of the parameter.

Returns:

  • (Object, nil)


40
41
42
# File 'lib/highway/steps/parameters/single.rb', line 40

def default
  @default
end

#typeHighway::Steps::Types::* (readonly)

Type of the parameter.



35
36
37
# File 'lib/highway/steps/parameters/single.rb', line 35

def type
  @type
end

Instance Method Details

#typecheck_and_validate(value, interface:, keypath: []) ⇒ Object

Typecheck and validate a value of the parameter.

This method returns typechecked, coerced and validated value or raises a fatal error if value has invalid type, can’t be coerced or is othweriwse invalid.

Parameters:

  • value (Object)

    A value.

  • interface (Highway::Interface)

    An interface instance.

  • keypath (Array<String>) (defaults to: [])

    A keypath to be used for debugging purposes.

Returns:

  • (Object)


53
54
55
56
57
58
59
60
# File 'lib/highway/steps/parameters/single.rb', line 53

def typecheck_and_validate(value, interface:, keypath: [])
  typechecked = @type.typecheck_and_validate(value)
  if typechecked != nil
    typechecked
  else
    interface.fatal!("Invalid value: '#{value}' for parameter: '#{Utilities::keypath_to_s(keypath)}'.")
  end
end