Class: TypedOperation::Operations::PropertyBuilder

Inherits:
Object
  • Object
show all
Includes:
Literal::Types
Defined in:
lib/typed_operation/operations/property_builder.rb

Overview

Builds typed properties for operations, handling optional parameters, defaults, and type conversions with Literal types.

Instance Method Summary collapse

Constructor Details

#initialize(typed_operation, parameter_name, type_signature, options) ⇒ PropertyBuilder

: (singleton(Base) | singleton(ImmutableBase), Symbol, Literal::Types::_Matchable, Hash[Symbol, untyped]) -> void



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/typed_operation/operations/property_builder.rb', line 23

def initialize(typed_operation, parameter_name, type_signature, options)
  @typed_operation = typed_operation
  @name = parameter_name
  @signature = type_signature
  @optional = options[:optional]
  @positional = options[:positional]
  @reader = options[:reader] || :public
  @has_default = options.key?(:default)
  @default = options[:default]

  prepare_type_signature
end

Instance Method Details

#define(&converter) ⇒ Object

: () { (untyped) -> untyped } -> void : () -> void



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/typed_operation/operations/property_builder.rb', line 38

def define(&converter)
  coerce_by = if type_nilable? && converter
    ->(value) { (value == Literal::Undefined || value.nil?) ? value : converter.call(value) }
  else
    converter
  end
  @typed_operation.prop(
    @name,
    @signature,
    @positional ? :positional : :keyword,
    default: resolved_default,
    reader: @reader,
    &coerce_by
  )
end