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.



381
382
383
384
# File 'lib/t_ruby/constraint_checker.rb', line 381

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

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



379
380
381
# File 'lib/t_ruby/constraint_checker.rb', line 379

def types
  @types
end

Instance Method Details

#clearObject

Clear all registrations



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

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

#errorsObject

Get validation errors



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

def errors
  @checker.errors
end

#get(name) ⇒ Object

Get type info



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

def get(name)
  @types[name]
end

#listObject

List all registered types



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

def list
  @types.keys
end

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

Register a constrained type from parsed definition



387
388
389
390
391
392
393
394
# File 'lib/t_ruby/constraint_checker.rb', line 387

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



397
398
399
400
401
402
403
# File 'lib/t_ruby/constraint_checker.rb', line 397

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)


406
407
408
# File 'lib/t_ruby/constraint_checker.rb', line 406

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

#validate(type_name, value) ⇒ Object

Validate value against type



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

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

#validation_code(type_name, var_name) ⇒ Object

Generate validation code



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

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