Class: AbideDevUtils::CEM::Generate::Reference::TypeExprValueFormatter
- Inherits:
-
Object
- Object
- AbideDevUtils::CEM::Generate::Reference::TypeExprValueFormatter
- 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
-
.format(value, type_expr, optional_strategy: :undef) ⇒ Any
Formats a value based on a type expression.
-
.optional(value, type_expr, strategy: :undef) ⇒ String, Any
Returns the display representation of the value with an Optional type expression.
-
.peel_type_expr(type_expr) ⇒ String
Returns a “peeled” type expression.
-
.quote(value) ⇒ String, Any
Escapes and quotes a string.
-
.type_expr_placeholder(type_expr) ⇒ String
Formats the type expression as a placeholder.
-
.undef?(value) ⇒ Boolean
Checks if a value is considered undef.
Class Method Details
.format(value, type_expr, optional_strategy: :undef) ⇒ Any
Formats a value based on a type expression.
500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 500 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.
543 544 545 546 547 548 |
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 543 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.
555 556 557 558 559 |
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 555 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.
519 520 521 522 523 524 525 |
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 519 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.
564 565 566 |
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 564 def self.type_expr_placeholder(type_expr) "<<Type #{type_expr}>>" end |
.undef?(value) ⇒ Boolean
Checks if a value is considered undef.
530 531 532 |
# File 'lib/abide_dev_utils/cem/generate/reference.rb', line 530 def self.undef?(value) value.nil? || value == UNDEF_VAL end |