Class: TRuby::ConstrainedTypeRegistry
- Inherits:
-
Object
- Object
- TRuby::ConstrainedTypeRegistry
- Defined in:
- lib/t_ruby/constraint_checker.rb
Overview
Constrained type registry for managing type constraints
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all registrations.
-
#errors ⇒ Object
Get validation errors.
-
#get(name) ⇒ Object
Get type info.
-
#initialize ⇒ ConstrainedTypeRegistry
constructor
A new instance of ConstrainedTypeRegistry.
-
#list ⇒ Object
List all registered types.
-
#register(name, base_type:, constraints: []) ⇒ Object
Register a constrained type from parsed definition.
-
#register_from_source(line) ⇒ Object
Parse and register from source line.
-
#registered?(name) ⇒ Boolean
Check if a type is registered.
-
#validate(type_name, value) ⇒ Object
Validate value against type.
-
#validation_code(type_name, var_name) ⇒ Object
Generate validation code.
Constructor Details
#initialize ⇒ ConstrainedTypeRegistry
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
#types ⇒ Object (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
#clear ⇒ Object
Clear all registrations
436 437 438 439 |
# File 'lib/t_ruby/constraint_checker.rb', line 436 def clear @types.clear @checker = ConstraintChecker.new end |
#errors ⇒ Object
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 |
#list ⇒ Object
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
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 |