Class: ODDB::Dose

Inherits:
Quanty show all
Includes:
Comparable, OddbYaml
Defined in:
ext/export/src/oddb_yaml.rb,
src/model/dose.rb

Overview

< Quanty

Direct Known Subclasses

ODDB::Drugs::Dose, TestOuwerkerkPlugin::StubDose

Constant Summary

DISABLE_DIFF =
true
EXPORT_PROPERTIES =
[
  '@not_normalized',
  '@val',
  '@unit',
]

Constants included from OddbYaml

EXPORT_PROPERTIES, YAML_URI

Constants inherited from Quanty

RadianUnit, Self

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from OddbYaml

#to_yaml_properties, #to_yaml_type

Methods inherited from Quanty

#+@, #-@, #<, #<=, #==, #>, #>=, #adjust, #coerce, #inspect

Constructor Details

- (Dose) initialize(qty, unit = nil)

A new instance of Dose



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
# File 'src/model/dose.rb', line 20

def initialize(qty, unit=nil)
  np = '-?[0-9]+(?:[.,][0-9]+)?' ## numerical pattern
  qty_str = ''
  if(match = %r{(#{np})\s*-\s*(#{np})}u.match(qty.to_s))
    qty = round(match[1].to_f)..round(match[2].to_f)
  end
  if(qty.is_a?(Range))
    qty_str = "#{qty.first}-#{qty.last}"
    qty = (qty.first + qty.last) / 2.0
    @not_normalized = [qty_str, unit].compact.join(' ')
  end
  if(match = %r{([^/]*)/\s*(#{np})\s*(.*)}u.match(unit.to_s))
    qty_str = round(qty).to_s
    div = round(match[2])
    @not_normalized = [
      qty_str, 
      [match[1].strip, div].join(' / '), 
      match[3],
    ].join
    qty = qty.to_f/div.to_f
    unit = [match[1].strip,match[3].strip].join('/')
  end
  qty = round(qty)
  unit = unit.to_s
  unit.tr!('L', 'l')
  unit.gsub!(/U\.\s*Ph\.\s*Eur\./, 'UPhEur')
  unit.gsub!(/\./, '')
  unit.gsub!(/\s*\/\s*/, '/')
  unit.strip!
  fuzzy_retry = true
  strict_retry = true
  begin
    super(qty, unit)
  rescue StandardError => e
    if(fuzzy_retry)
      unit = unit[0,2]
      fuzzy_retry = false
      retry
    elsif(strict_retry)
      unit = ''
      strict_retry = false
      retry
    else
      raise
    end
  end
end

Class Method Details

+ (Object) from_quanty(other)



11
12
13
14
15
16
17
18
19
# File 'src/model/dose.rb', line 11

def Dose.from_quanty(other)
  if other.is_a?(Dose)
    other
  elsif other.is_a?(Quanty)
    Dose.new(other.val, other.unit)
  else
    Dose.new(other)
  end
end

Instance Method Details

- (Object) *(other)



96
97
98
# File 'src/model/dose.rb', line 96

def * (other)
  Dose.from_quanty(super)
end

- (Object) **(other)



108
109
110
# File 'src/model/dose.rb', line 108

def ** (other)
  Dose.from_quanty(super)
end

- (Object) +(other)



99
100
101
# File 'src/model/dose.rb', line 99

def + (other)
  Dose.from_quanty(super)
end

- (Object) -(other)



105
106
107
# File 'src/model/dose.rb', line 105

def - (other)
  Dose.from_quanty(super)
end

- (Object) /(other)



102
103
104
# File 'src/model/dose.rb', line 102

def / (other)
  Dose.from_quanty(super)
end

- (Object) <=>(other)



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'src/model/dose.rb', line 111

def <=>(other)
  begin
    (@val * 1000).round <=> (adjust(other) * 1000).round
  rescue StandardError
    if(@unit.nil?)
      1
    elsif(other.unit.nil?)
      -1
    else
      @unit <=> other.unit
    end
  end
end

- (Object) scale



67
68
69
70
71
72
# File 'src/model/dose.rb', line 67

def scale
  if @not_normalized && str = @not_normalized[%r{/.*}u]
    Dose.new str[/[\d.]+/u], str[/\D+$/u]
  end
rescue
end

- (Object) to_f



73
74
75
76
77
78
79
# File 'src/model/dose.rb', line 73

def to_f
  begin
    super
  rescue RuntimeError
    @val * @fact.factor
  end
end

- (Object) to_i



80
81
82
# File 'src/model/dose.rb', line 80

def to_i
  @val.to_i
end

- (Object) to_s



83
84
85
86
87
88
89
90
91
92
# File 'src/model/dose.rb', line 83

def to_s
  @not_normalized or begin
    val = if(@val.is_a? Float)
      sprintf('%.5f', @val).gsub(/0+$/u, '')
    else
      @val
    end
    [val, @unit].join(' ')
  end
end

- (Object) to_yaml(opts = {})



221
222
223
224
225
226
227
228
229
230
# File 'ext/export/src/oddb_yaml.rb', line 221

def to_yaml( opts = {} )
  YAML::quick_emit( self.object_id, opts ) { |out|
    out.map( taguri ) { |map|
      to_yaml_properties.each { |m|
        map.add( m[1..-1], instance_variable_get( m ) )
      }
      map.add('scale', self.scale)
    }
  }
end

- (Object) want(unit)



93
94
95
# File 'src/model/dose.rb', line 93

def want(unit)
  Dose.from_quanty(super)
end