Class: TRuby::ConstrainedTypeRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/constraint_checker.rb

Overview

Constrained type registry for managing type constraints

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConstrainedTypeRegistry

Returns a new instance of ConstrainedTypeRegistry.



387
388
389
390
# File 'lib/t_ruby/constraint_checker.rb', line 387

def initialize
  @types = {}
  @checker = ConstraintChecker.new
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



385
386
387
# File 'lib/t_ruby/constraint_checker.rb', line 385

def types
  @types
end

Instance Method Details

#clearObject

Clear all registrations



442
443
444
445
# File 'lib/t_ruby/constraint_checker.rb', line 442

def clear
  @types.clear
  @checker = ConstraintChecker.new
end

#errorsObject

Get validation errors



427
428
429
# File 'lib/t_ruby/constraint_checker.rb', line 427

def errors
  @checker.errors
end

#get(name) ⇒ Object

Get type info



417
418
419
# File 'lib/t_ruby/constraint_checker.rb', line 417

def get(name)
  @types[name]
end

#listObject

List all registered types



437
438
439
# File 'lib/t_ruby/constraint_checker.rb', line 437

def list
  @types.keys
end

#register(name, base_type:, constraints: []) ⇒ Object

Register a constrained type from parsed definition



393
394
395
396
397
398
399
400
# File 'lib/t_ruby/constraint_checker.rb', line 393

def register(name, base_type:, constraints: [])
  @types[name] = {
    base_type: base_type,
    constraints: constraints,
    defined_at: caller_locations(1, 1).first,
  }
  @checker.register(name, base_type: base_type, constraints: constraints)
end

#register_from_source(line) ⇒ Object

Parse and register from source line



403
404
405
406
407
408
409
# File 'lib/t_ruby/constraint_checker.rb', line 403

def register_from_source(line)
  result = @checker.parse_constraint(line)
  return false unless result

  register(result[:name], base_type: result[:base_type], constraints: result[:constraints])
  true
end

#registered?(name) ⇒ Boolean

Check if a type is registered

Returns:

  • (Boolean)


412
413
414
# File 'lib/t_ruby/constraint_checker.rb', line 412

def registered?(name)
  @types.key?(name)
end

#validate(type_name, value) ⇒ Object

Validate value against type



422
423
424
# File 'lib/t_ruby/constraint_checker.rb', line 422

def validate(type_name, value)
  @checker.validate(type_name, value)
end

#validation_code(type_name, var_name) ⇒ Object

Generate validation code



432
433
434
# File 'lib/t_ruby/constraint_checker.rb', line 432

def validation_code(type_name, var_name)
  @checker.generate_validation_code(type_name, var_name)
end