Class: AbideDevUtils::CEM::Generate::Reference::TypeExprValueFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/cem/generate/reference.rb

Overview

Holds methods for formmating values based on type expressions

Constant Summary collapse

UNDEF_VAL =
'undef'

Class Method Summary collapse

Class Method Details

.format(value, type_expr, optional_strategy: :undef) ⇒ Any

Formats a value based on a type expression.

Parameters:

  • value (Any)

    the value to format

  • type_expr (String)

    the type expression to use for formatting

  • optional_strategy (Symbol) (defaults to: :undef)

    the strategy to use for optional values

Returns:

  • (Any)

    the formatted value



421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 421

def self.format(value, type_expr, optional_strategy: :undef)
  return value if value == 'No parameters'

  case type_expr
  when /^(String|Stdlib::(Unix|Windows|Absolute)path|Enum)/
    quote(value)
  when /^Optional\[/
    optional(value, type_expr, strategy: optional_strategy)
  else
    return type_expr_placeholder(type_expr) if value.nil?

    quote(value)
  end
end

.optional(value, type_expr, strategy: :undef) ⇒ String, Any

Returns the display representation of the value with an Optional type expression. If the value is not nil or ‘undef’, returns the quoted form of the value.

Parameters:

  • value (Any)

    the value to format.

  • type_expr (String)

    the type expression.

  • strategy (Symbol) (defaults to: :undef)

    the strategy to use. Valid strategies are :undef and :placeholder. :undef will return ‘undef’ if the value is nil or ‘undef’. :placeholder will return a peeled type expression placeholder if the value is nil or ‘undef’.

Returns:

  • (String)

    the formatted value.

  • (Any)

    the quoted value if it is not nil.



464
465
466
467
468
469
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 464

def self.optional(value, type_expr, strategy: :undef)
  return UNDEF_VAL if undef?(value) && strategy == :undef
  return type_expr_placeholder(peel_type_expr(type_expr)) if undef?(value) && strategy == :placeholder

  quote(value)
end

.peel_type_expr(type_expr) ⇒ String

Returns a “peeled” type expression. Peeling a type expression removes the first layer of the type expression. For example, if the type expression is Optional, the peeled type expression is String.

Parameters:

  • type_expr (String)

    the type expression to peel.

Returns:

  • (String)

    the peeled type expression.



476
477
478
479
480
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 476

def self.peel_type_expr(type_expr)
  return type_expr unless type_expr.include?('[')

  type_expr.match(/^[A-Z][a-z0-9_]*\[(?<peeled>[A-Za-z0-9:,_{}=>\[\]\\\s]+)\]$/)[:peeled]
end

.quote(value) ⇒ String, Any

Escapes and quotes a string. If value is not a string, returns value.

Parameters:

  • value (Any)

    the string to quote.

Returns:

  • (String)

    the quoted string.

  • (Any)

    the value if it is not a string.



440
441
442
443
444
445
446
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 440

def self.quote(value)
  if value.is_a?(String)
    value.inspect
  else
    value
  end
end

.type_expr_placeholder(type_expr) ⇒ String

Formats the type expression as a placeholder.

Parameters:

  • type_expr (String)

    The type expression to format.

Returns:

  • (String)

    The formatted type expression.



485
486
487
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 485

def self.type_expr_placeholder(type_expr)
  "<<Type #{type_expr}>>"
end

.undef?(value) ⇒ Boolean

Checks if a value is considered undef.

Parameters:

  • value (Any)

    the value to check.

Returns:

  • (Boolean)

    true if value is considered undef (nil or ‘undef’).



451
452
453
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 451

def self.undef?(value)
  value.nil? || value == UNDEF_VAL
end