Class: HQMF1::Restriction

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/hqmf-parser/1.0/restriction.rb

Overview

Represents a restriction on the allowable values of a data item

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#attr_val, #clean_json, #clean_json_recursive

Methods included from HQMF::Conversion::Utilities

#build_hash, #check_equality, #json_array, #openstruct_to_json

Constructor Details

#initialize(entry, parent, doc) ⇒ Restriction

Returns a new instance of Restriction.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hqmf-parser/1.0/restriction.rb', line 10

def initialize(entry, parent, doc)
  @doc = doc
  @entry = entry
  @restrictions = []
  
  range_def = @entry.at_xpath('./cda:pauseQuantity')
  if range_def
    @range = Range.new(range_def)
  end
  
  local_restrictions = @entry.xpath('./*/cda:sourceOf[@typeCode!="PRCN" and @typeCode!="COMP"]').collect do |entry|
    Restriction.new(entry, self, @doc)
  end
  
  @restrictions.concat(local_restrictions)
  
  local_subset = attr_val('./cda:subsetCode/@code')
  if local_subset
    @subset = local_subset
  end
  
  if @entry.at_xpath('./*/cda:derivationExpr')
    @expression = Expression.new(@entry) 
  end
  
  comparison_def = @entry.at_xpath('./*/cda:sourceOf[@typeCode="COMP"]')
  if comparison_def
    data_criteria_id = attr_val('./*/cda:id/@root')
    data_criteria_id = comparison_def.at_xpath('./*/cda:id/@root').value if (data_criteria_id.nil? and comparison_def.at_xpath('./*/cda:id/@root'))
    @comparison = Comparison.new(data_criteria_id, comparison_def, self, @doc)
  end
  
  @preconditions = @entry.xpath('./*/cda:sourceOf[@typeCode="PRCN"]').collect do |entry|
    # create a dummy parent with a single restriction copied from self minus the
    # nested preconditions to avoid an infinite loop
    prior_comparison = nil
    if parent.class==HQMF1::Precondition
      prior_comparison = parent.first_comparison
    else
      prior_comparison = @comparsion
    end
    current_restriction = OpenStruct.new(
      'range' => @range,
      'comparison' => prior_comparison,
      'restrictions' => [],
      'preconditions' => [],
      'subset' => @subset,
      'type' => type,
      'target_id' => target_id,
      'field' => field,
      'field_code' => field_code,
      'field_time' => field_time,
      'value' => value)
    all_restrictions = []
    all_restrictions.concat @restrictions
    all_restrictions << current_restriction
    parent = OpenStruct.new(
      'restrictions' => all_restrictions,
      'subset' => @subset
    )
    p = Precondition.new(entry, parent, @doc)
    
  end
  
end

Instance Attribute Details

#comparisonObject (readonly)

Returns the value of attribute comparison.



7
8
9
# File 'lib/hqmf-parser/1.0/restriction.rb', line 7

def comparison
  @comparison
end

#expressionObject (readonly)

Returns the value of attribute expression.



7
8
9
# File 'lib/hqmf-parser/1.0/restriction.rb', line 7

def expression
  @expression
end

#from_parentObject

Returns the value of attribute from_parent.



8
9
10
# File 'lib/hqmf-parser/1.0/restriction.rb', line 8

def from_parent
  @from_parent
end

#preconditionsObject (readonly)

Returns the value of attribute preconditions.



7
8
9
# File 'lib/hqmf-parser/1.0/restriction.rb', line 7

def preconditions
  @preconditions
end

#rangeObject (readonly)

Returns the value of attribute range.



7
8
9
# File 'lib/hqmf-parser/1.0/restriction.rb', line 7

def range
  @range
end

#restrictionsObject (readonly)

Returns the value of attribute restrictions.



7
8
9
# File 'lib/hqmf-parser/1.0/restriction.rb', line 7

def restrictions
  @restrictions
end

#subsetObject (readonly)

Returns the value of attribute subset.



7
8
9
# File 'lib/hqmf-parser/1.0/restriction.rb', line 7

def subset
  @subset
end

Instance Method Details

#fieldObject



92
93
94
# File 'lib/hqmf-parser/1.0/restriction.rb', line 92

def field
  attr_val('./cda:observation/cda:code/@displayName')
end

#field_codeObject



96
97
98
# File 'lib/hqmf-parser/1.0/restriction.rb', line 96

def field_code
  attr_val('./cda:observation/cda:code/@code') || attr_val('./cda:encounter/cda:participant/cda:roleParticipant/@classCode')
end

#field_timeObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/hqmf-parser/1.0/restriction.rb', line 100

def field_time
  effectiveTime = @entry.at_xpath('./cda:observation/cda:effectiveTime') || @entry.at_xpath('./cda:encounter/cda:participant/cda:roleParticipant/cda:effectiveTime')
  
  time = nil
  if effectiveTime
    time = :start if effectiveTime.at_xpath('./cda:low')
    time = :end if effectiveTime.at_xpath('./cda:high')
  end
  time
end

#negationObject

is this type negated? true or false



82
83
84
# File 'lib/hqmf-parser/1.0/restriction.rb', line 82

def negation
  attr_val('./@inversionInd') == "true"
end

#target_idObject

The id of the data criteria or measurement property that the value will be compared against



88
89
90
# File 'lib/hqmf-parser/1.0/restriction.rb', line 88

def target_id
  attr_val('./*/cda:id/@root')
end

#to_jsonObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/hqmf-parser/1.0/restriction.rb', line 141

def to_json 
  return nil if from_parent
  json = build_hash(self, [:subset,:type,:target_id,:field,:field_code,:from_parent, :negation, :field_time])
  json[:range] = range.to_json if range
  if value
    if value.is_a? String
      json[:value] = value
    else
      json[:value] = value.to_json
    end
  end
  json[:comparison] = comparison.to_json if comparison
  json[:restrictions] = json_array(self.restrictions)
  json[:preconditions] = json_array(self.preconditions)
  json[:expression] = self.expression.to_json if self.expression
  json
end

#typeObject

The type of restriction, e.g. SBS, SBE etc



77
78
79
# File 'lib/hqmf-parser/1.0/restriction.rb', line 77

def type
  attr_val('./@typeCode')
end

#valueObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hqmf-parser/1.0/restriction.rb', line 112

def value
  value = nil
  type = attr_val('./cda:observation/cda:value/@xsi:type') || 'CD'
  case type
  when 'IVL_PQ'
    value = HQMF1::Range.new(@entry.xpath('./cda:observation/cda:value'))
  when 'PQ'
    value = HQMF1::Value.new(@entry.xpath('./cda:observation/cda:value'))
  when 'CD'
    if field && field.downcase == 'status'
      code = attr_val('./cda:observation/cda:value/@displayName').downcase
      value = HQMF::Coded.for_single_code('status',code,code)
    elsif attr_val('./cda:observation/cda:value/@code')
      oid = attr_val('./cda:observation/cda:value/@code')
      title = attr_val('./cda:observation/cda:value/@displayName')
      value = HQMF::Coded.for_code_list(oid,title)
    elsif attr_val('./cda:encounter/cda:participant/cda:roleParticipant/cda:code/@code')
      oid = attr_val('./cda:encounter/cda:participant/cda:roleParticipant/cda:code/@code')
      title = attr_val('./cda:encounter/cda:participant/cda:roleParticipant/cda:code/@displayName')
      value = HQMF::Coded.for_code_list(oid,title)
    end
  when 'ANYNonNull'
    value = HQMF::AnyValue.new
  else
    raise "Unknown restriction value type #{type}"
  end if type
  value
end