Class: ActiveFacts::CQL::Compiler::ValueConstraint

Inherits:
Constraint show all
Defined in:
lib/activefacts/cql/compiler/constraint.rb

Instance Attribute Summary

Attributes inherited from Definition

#constellation, #tree, #vocabulary

Instance Method Summary collapse

Methods inherited from Constraint

#bind_clauses, #common_bindings, #loose_bind, #loose_bind_wherever_possible, #loose_binding, #warn_ignored_queries

Methods inherited from Definition

#all_bindings_in_clauses, #build_all_steps, #build_steps, #build_variables, #source

Constructor Details

#initialize(ast, enforcement) ⇒ ValueConstraint

Returns a new instance of ValueConstraint.



496
497
498
499
500
501
# File 'lib/activefacts/cql/compiler/constraint.rb', line 496

def initialize ast, enforcement
  super nil, enforcement
  @value_ranges = ast[:ranges]
  @units = ast[:units]
  @regular_expression = ast[:regular_expression]
end

Instance Method Details

#assert_value(val) ⇒ Object



503
504
505
506
507
508
509
510
511
# File 'lib/activefacts/cql/compiler/constraint.rb', line 503

def assert_value(val)
  if val.is_a?(String)
    @constellation.Value(eval(val), true, nil)
  elsif val
    @constellation.Value(val.to_s, false , nil)
  else
    nil
  end
end

#compileObject



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/activefacts/cql/compiler/constraint.rb', line 513

def compile
         @constraint = @constellation.ValueConstraint(:new)
         raise "Units on value constraints are not yet processed (at line #{'REVISIT'})" if @units
             # @string.line_of(node.interval.first)

  if @value_ranges
    @value_ranges.each do |range|
      min, max = Array === range ? range : [range, range]
      v_range = @constellation.ValueRange(
	min && @constellation.Bound(:value => assert_value(min), :is_inclusive => true),
	max && @constellation.Bound(:value => assert_value(max), :is_inclusive => true))
      ar = @constellation.AllowedRange(@constraint, v_range)
    end
  else
    @constraint.regular_expression = @regular_expression
  end
  @enforcement.compile(@constellation, @constraint) if @enforcement
  super
end

#to_sObject



551
552
553
554
555
556
557
# File 'lib/activefacts/cql/compiler/constraint.rb', line 551

def to_s
  "#{super} to " +
	  (@value_ranges ?
	    "(#{@value_ranges.map{|vr| vrto_s(vr) }.inspect })#{ @units ? " in #{@units.inspect}" : ''}" :
	    @regular_expression
	  )
end

#vrto_s(vr) ⇒ Object



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/activefacts/cql/compiler/constraint.rb', line 533

def vrto_s vr
  if Array === vr
    min = vr[0]
    max = vr[1]
    if Numeric === min or Numeric === max
      infinite = 1.0/0
      min ||= -infinite
      max ||= infinite
    else
      min ||= 'MIN'
      max ||= 'MAX'
    end
    Range.new(min, max)
  else
    vr
  end
end