Module: Cda::Constraints::ClassMethods

Defined in:
lib/cda/constraints.rb

Instance Method Summary collapse

Instance Method Details

#add_constraint(on, new_constraint = {}) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/cda/constraints.rb', line 19

def add_constraint(on, new_constraint = {})
  constraints = self.constraints[on] || []
  return if constraints.find { |c| constraint_covered_by(new_constraint, c) }
  constraints = constraints.reject { |c| constraint_covered_by(c, new_constraint) }
  constraints << new_constraint
  self.constraints = self.constraints.merge(on => constraints)
end

#defaultsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cda/constraints.rb', line 27

def defaults
  @defaults ||= self.constraints.reduce({}) do |acc, (path, constraints)|
    values = constraints.
      select { |c| c[:cardinality] == '1..1' }.
      map { |c| c[:value].presence }.
      compact
    if values.present?
      normalized_value = values.length > 1 ? values : values.first
      default_value = Utility.inference(path.split('.'), normalized_value, self)
      Cda::Utility.merge_json(acc, default_value)
    else
      acc
    end
  end
end