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.
387 388 389 390 |
# File 'lib/t_ruby/constraint_checker.rb', line 387 def initialize @types = {} @checker = ConstraintChecker.new end |
Instance Attribute Details
#types ⇒ Object (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
#clear ⇒ Object
Clear all registrations
442 443 444 445 |
# File 'lib/t_ruby/constraint_checker.rb', line 442 def clear @types.clear @checker = ConstraintChecker.new end |
#errors ⇒ Object
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 |
#list ⇒ Object
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
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 |