Class: HQMF2::Range

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

Overview

Represents a HQMF physical quantity which can have low and high bounds

Direct Known Subclasses

EffectiveTime

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#attr_val, attr_val, #strip_tokens, #to_xml

Methods included from HQMF::Conversion::Utilities

#build_hash, #check_equality, #json_array, #openstruct_to_json

Constructor Details

#initialize(entry, type = nil) ⇒ Range

Returns a new instance of Range.



116
117
118
119
120
121
122
123
124
125
# File 'lib/hqmf-parser/2.0/types.rb', line 116

def initialize(entry, type = nil)
  @type = type
  @entry = entry
  return unless @entry
  @low = optional_value("#{default_element_name}/cda:low", default_bounds_type)
  @high = optional_value("#{default_element_name}/cda:high", default_bounds_type)
  # Unset low bound to resolve verbose value bounds descriptions
  @low = nil if (@high.try(:value) && @high.value.to_i > 0) && (@low.try(:value) && @low.value.try(:to_i) == 0)
  @width = optional_value("#{default_element_name}/cda:width", 'PQ')
end

Instance Attribute Details

#highObject

Returns the value of attribute high.



114
115
116
# File 'lib/hqmf-parser/2.0/types.rb', line 114

def high
  @high
end

#lowObject

Returns the value of attribute low.



114
115
116
# File 'lib/hqmf-parser/2.0/types.rb', line 114

def low
  @low
end

#widthObject

Returns the value of attribute width.



114
115
116
# File 'lib/hqmf-parser/2.0/types.rb', line 114

def width
  @width
end

Instance Method Details

#generate_any_value?(lm, hm) ⇒ Boolean

Check if are only AnyValue elements for low and high

Returns:

  • (Boolean)


153
154
155
# File 'lib/hqmf-parser/2.0/types.rb', line 153

def generate_any_value?(lm, hm)
  (lm.nil? || lm.is_a?(HQMF::AnyValue)) && (hm.nil? || hm.is_a?(HQMF::AnyValue))
end

#generate_value?(lm, hm) ⇒ Boolean

Check if the value for the range should actually produce a single value instead of a range (if low and high are the same)

Returns:

  • (Boolean)


159
160
161
# File 'lib/hqmf-parser/2.0/types.rb', line 159

def generate_value?(lm, hm)
  !lm.nil? && lm.try(:value) == hm.try(:value) && lm.try(:unit).nil? && hm.try(:unit).nil?
end

#to_modelObject

Generates this classes hqmf-model equivalent



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hqmf-parser/2.0/types.rb', line 132

def to_model
  lm = low.try(:to_model)
  hm = high.try(:to_model)
  wm = width.try(:to_model)
  model_type = type
  if @entry.at_xpath('./cda:uncertainRange', HQMF2::Document::NAMESPACES)
    model_type = 'IVL_PQ'
  end

  if generate_any_value?(lm, hm)
    # Generate AnyValue if the only elements in the range are AnyValues.
    HQMF::AnyValue.new
  elsif generate_value?(lm, hm)
    # Generate a singel value if both low and high are the same
    HQMF::Value.new(lm.type, nil, lm.value, lm.inclusive?, lm.derived?, lm.expression)
  else
    HQMF::Range.new(model_type, lm, hm, wm)
  end
end

#typeObject



127
128
129
# File 'lib/hqmf-parser/2.0/types.rb', line 127

def type
  @type || attr_val('./@xsi:type')
end