Class: TRuby::ConstraintChecker

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

Overview

Main constraint checker class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConstraintChecker

Returns a new instance of ConstraintChecker.



181
182
183
184
# File 'lib/t_ruby/constraint_checker.rb', line 181

def initialize
  @constraints = {}
  @errors = []
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



179
180
181
# File 'lib/t_ruby/constraint_checker.rb', line 179

def constraints
  @constraints
end

#errorsObject (readonly)

Returns the value of attribute errors.



179
180
181
# File 'lib/t_ruby/constraint_checker.rb', line 179

def errors
  @errors
end

Instance Method Details

#generate_validation_code(type_name, var_name) ⇒ Object

Generate validation code for runtime checking



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/t_ruby/constraint_checker.rb', line 263

def generate_validation_code(type_name, var_name)
  return nil unless @constraints.key?(type_name)

  type_info = @constraints[type_name]
  conditions = []

  # Base type check
  case type_info[:base_type]
  when "Integer"
    conditions << "#{var_name}.is_a?(Integer)"
  when "String"
    conditions << "#{var_name}.is_a?(String)"
  when "Float"
    conditions << "#{var_name}.is_a?(Float)"
  when "Numeric"
    conditions << "#{var_name}.is_a?(Numeric)"
  when "Array"
    conditions << "#{var_name}.is_a?(Array)"
  end

  # Constraint checks
  type_info[:constraints].each do |constraint|
    if constraint.respond_to?(:validation_code)
      code = constraint.validation_code(var_name)
      conditions << code unless code.empty?
    end
  end

  conditions.join(" && ")
end

#parse_constraint(definition) ⇒ Object

Parse constraint syntax from source



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/t_ruby/constraint_checker.rb', line 195

def parse_constraint(definition)
  # Match: type Name <: BaseType where condition
  if definition.match?(/^(\w+)\s*<:\s*(\w+)\s*where\s*(.+)$/)
    match = definition.match(/^(\w+)\s*<:\s*(\w+)\s*where\s*(.+)$/)
    name = match[1]
    base_type = match[2]
    condition = match[3].strip

    constraints = parse_condition(base_type, condition)
    return { name: name, base_type: base_type, constraints: constraints }
  end

  # Match: type Name <: BaseType (bounds only)
  if definition.match?(/^(\w+)\s*<:\s*(\w+)\s*$/)
    match = definition.match(/^(\w+)\s*<:\s*(\w+)\s*$/)
    name = match[1]
    base_type = match[2]

    return {
      name: name,
      base_type: base_type,
      constraints: [BoundsConstraint.new(subtype: name, supertype: base_type)]
    }
  end

  # Match: type Name = BaseType where condition
  if definition.match?(/^(\w+)\s*=\s*(\w+)\s*where\s*(.+)$/)
    match = definition.match(/^(\w+)\s*=\s*(\w+)\s*where\s*(.+)$/)
    name = match[1]
    base_type = match[2]
    condition = match[3].strip

    constraints = parse_condition(base_type, condition)
    return { name: name, base_type: base_type, constraints: constraints }
  end

  nil
end

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

Register a constrained type



187
188
189
190
191
192
# File 'lib/t_ruby/constraint_checker.rb', line 187

def register(name, base_type:, constraints: [])
  @constraints[name] = {
    base_type: base_type,
    constraints: constraints
  }
end

#validate(type_name, value) ⇒ Object

Validate a value against a constrained type



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/t_ruby/constraint_checker.rb', line 235

def validate(type_name, value)
  @errors = []

  unless @constraints.key?(type_name)
    @errors << "Unknown constrained type: #{type_name}"
    return false
  end

  type_info = @constraints[type_name]

  # Check base type
  unless matches_base_type?(value, type_info[:base_type])
    @errors << "Value #{value.inspect} does not match base type #{type_info[:base_type]}"
    return false
  end

  # Check all constraints
  type_info[:constraints].each do |constraint|
    unless constraint.satisfied?(value)
      @errors << "Constraint violation: #{constraint.condition}"
      return false
    end
  end

  true
end