Module: Jade::Codegen::Boundary::Specialized::Scalar

Extended by:
Scalar
Included in:
Scalar
Defined in:
lib/jade/codegen/boundary/specialized/scalar.rb

Overview

Int / Float / Bool / String. The runtime validators in ‘Jade::Interop::Boundary` do the check + conversion (e.g. `String#dup`, `Float#to_f`) in one call.

Constant Summary collapse

HELPER =
{
  'Basics.Int'    => 'integer',
  'Basics.Float'  => 'float',
  'Basics.Bool'   => 'bool',
  'String.String' => 'string',
}.freeze
LABEL =
{
  'Basics.Int'    => 'Int',
  'Basics.Float'  => 'Float',
  'Basics.Bool'   => 'Bool',
  'String.String' => 'String',
}.freeze
LIST_ELEM_CLASS =

Ruby class used by ‘List.decode` to validate a list of scalars in a single `Array#all?` C-loop.

{
  'Basics.Int'    => '::Integer',
  'Basics.Float'  => '::Numeric',
  'Basics.Bool'   => '::TrueClass',
  'String.String' => '::String',
}.freeze

Instance Method Summary collapse

Instance Method Details

#decode(type, input) ⇒ Object



34
35
36
37
38
# File 'lib/jade/codegen/boundary/specialized/scalar.rb', line 34

def decode(type, input)
  qname = qname_for(type) or return nil
  label = LABEL[qname].inspect
  "Jade::Interop::Boundary.#{HELPER[qname]}(#{label}, #{input})"
end

#encode(_type, _value_expr, _registry) ⇒ Object

Scalar encoders are identity (Ruby int IS the JSON int, etc.), so ‘encode` returns nil — callers handle that via `Specialized.identity_encoder?`.



43
44
45
# File 'lib/jade/codegen/boundary/specialized/scalar.rb', line 43

def encode(_type, _value_expr, _registry)
  nil
end

#identity_encoder?(type) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/jade/codegen/boundary/specialized/scalar.rb', line 47

def identity_encoder?(type)
  qname_for(type) ? true : false
end

#qname_for(type) ⇒ Object

The scalar’s qname (e.g. ‘“Basics.Int”`) if `type` is a 0-arg application of a known scalar constructor, else nil. Used by `List.decode` to pick `LIST_ELEM_CLASS` for the all? check.



58
59
60
61
62
# File 'lib/jade/codegen/boundary/specialized/scalar.rb', line 58

def qname_for(type)
  return nil unless Specialized.args_of(type) == []

  Specialized.qname_of(type).then { HELPER.key?(it) ? it : nil }
end

#specializable?(type, _registry, _seen) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/jade/codegen/boundary/specialized/scalar.rb', line 51

def specializable?(type, _registry, _seen)
  identity_encoder?(type)
end