Module: Stannum::Support::Coercion

Defined in:
lib/stannum/support/coercion.rb

Overview

Shared functionality for coercing values to and from constraints.

Class Method Summary collapse

Class Method Details

.error_key(value) ⇒ Integer, ...

Coerces an arbitrary object into a valid Stannum::Errors key.

If the value is an Integer, a String, or a Symbol, returns the value. Otherwise, returns the result of calling #inspect on the value.

Parameters:

  • value (Object)

    The value to coerce.

Returns:

  • (Integer, String, Symbol)

    the value or result of #inspect.



22
23
24
25
26
# File 'lib/stannum/support/coercion.rb', line 22

def error_key(value)
  return value if ERROR_KEY_TYPES.include?(value.class)

  value.inspect
end

.presence_constraint(present, allow_nil: false, as: 'present', **options) {|present, options| ... } ⇒ Stannum::Constraints:Base?

Coerce a Boolean value to a Presence constraint.

Parameters:

  • present (true, false, Stannum::Constraints::Base, nil)

    The expected presence or absence of the value. If true, will return a Presence constraint. If false, will return an Absence constraint.

  • allow_nil (true, false) (defaults to: false)

    If true, a nil value will be returned instead of raising an exception.

  • as (String) (defaults to: 'present')

    A short name for the coerced value, used in generating an error message. Defaults to “present”.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.

Yields:

  • Builds a constraint from true or false. If no block is given, creates a Stannum::Constraints::Presence or Stannum::Constraints::Absence constraint.

Yield Parameters:

  • present (true, false)

    The expected presence or absence of the value.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.

Yield Returns:

Returns:

  • (Stannum::Constraints:Base, nil)

    the generated or given constraint.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/stannum/support/coercion.rb', line 51

def presence_constraint(
  present,
  allow_nil: false,
  as:        'present',
  **options,
  &block
)
  return nil if allow_nil && present.nil?

  if present.is_a?(Stannum::Constraints::Base)
    return present.with_options(**options)
  end

  if present == true || present == false # rubocop:disable Style/MultipleComparison
    return build_presence_constraint(present, **options, &block)
  end

  raise ArgumentError,
    "#{as} must be true or false or a constraint",
    caller(1..-1)
end

.type_constraint(value, allow_nil: false, as: 'type', **options) {|value, options| ... } ⇒ Stannum::Constraints:Base?

Coerce a Class or Module to a Type constraint.

Parameters:

  • value (Class, Module, Stannum::Constraints::Base, nil)

    The value to coerce.

  • allow_nil (true, false) (defaults to: false)

    If true, a nil value will be returned instead of raising an exception.

  • as (String) (defaults to: 'type')

    A short name for the coerced value, used in generating an error message. Defaults to “type”.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.

Yields:

  • Builds a constraint from a Class or Module. If no block is given, creates a Stannum::Constraints::Type constraint.

Yield Parameters:

  • value (Class, Module)

    The Class or Module used to build the constraint.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.

Yield Returns:

Returns:

  • (Stannum::Constraints:Base, nil)

    the generated or given constraint.

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/stannum/support/coercion.rb', line 94

def type_constraint(
  value,
  allow_nil: false,
  as:        'type',
  **options,
  &block
)
  return nil if allow_nil && value.nil?

  if value.is_a?(Stannum::Constraints::Base)
    return value.with_options(**options)
  end

  if value.is_a?(Module)
    return build_type_constraint(value, **options, &block)
  end

  raise ArgumentError,
    "#{as} must be a Class or Module or a constraint",
    caller(1..-1)
end