Class: ODDB::Drugs::Dose

Inherits:
Quanty
  • Object
show all
Includes:
Comparable, OddbUri
Defined in:
lib/oddb/drugs/dose.rb,
lib/oddb/export/yaml.rb

Constant Summary collapse

@@range_ptrn =
%r{(#{np})\s*-\s*(#{np})}
@@cnc_ptrn =
%r{([^/]*)/\s*(#{np})\s*(.*)}

Constants included from OddbUri

OddbUri::YAML_URI

Constants inherited from Quanty

Quanty::RadianUnit, Quanty::Self

Instance Attribute Summary

Attributes inherited from Quanty

#fact, #unit, #val

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OddbUri

#to_yaml, #to_yaml_map, #to_yaml_type

Methods inherited from Quanty

#**, #+@, #-@, #<, #<=, #==, #>, #>=, #adjust, #coerce, #inspect, #to_si, #want

Constructor Details

#initialize(qty, unit = nil) ⇒ Dose

Returns a new instance of Dose.



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
# File 'lib/oddb/drugs/dose.rb', line 17

def initialize(qty, unit=nil)
  qty_str = ''
  if(match = @@range_ptrn.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 = @@cnc_ptrn.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.tr('L', 'l')
  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
    end
  end
end

Class Method Details

.from_quanty(other) ⇒ Object



14
15
16
# File 'lib/oddb/drugs/dose.rb', line 14

def Dose.from_quanty(other)
  Dose.new(other.val, other.unit)
end

Instance Method Details

#*(other) ⇒ Object



77
78
79
# File 'lib/oddb/drugs/dose.rb', line 77

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

#+(other) ⇒ Object



80
81
82
# File 'lib/oddb/drugs/dose.rb', line 80

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

#-(other) ⇒ Object



86
87
88
# File 'lib/oddb/drugs/dose.rb', line 86

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

#/(other) ⇒ Object



83
84
85
# File 'lib/oddb/drugs/dose.rb', line 83

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

#<=>(other) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/oddb/drugs/dose.rb', line 89

def <=>(other)
  begin
    (@val * 1000).round <=> (adjust(other) * 1000).round
  rescue StandardError
    @unit <=> other.unit
  end
end

#to_fObject



56
57
58
59
60
61
62
# File 'lib/oddb/drugs/dose.rb', line 56

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

#to_iObject



63
64
65
# File 'lib/oddb/drugs/dose.rb', line 63

def to_i
  @val.to_i
end

#to_sObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/oddb/drugs/dose.rb', line 66

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

#to_yaml_propertiesObject



88
89
90
# File 'lib/oddb/export/yaml.rb', line 88

def to_yaml_properties
  [ '@val', '@unit' ]
end