Module: ODDB::SizeParser

Included in:
Part
Defined in:
src/model/part.rb

Constant Summary

UNIT =
ODDB::Dose.new(1)
@@parser =
Parse.generate_parser "Grammar OddbSize\nTokens\nDESCRIPTION = /(?!\#{unit_pattern}\\s)\#{iso_pattern}(\\s+\#{iso_pattern})*/u\nNUMERIC     = /\#{numeric_pattern}/u\nSPACE       = /\\s+/u [:Skip]\nUNIT        = /\#{unit_pattern}/u\nProductions\nSize      -> Multiple* Addition? Count? Measure? Scale? Dose? DESCRIPTION?\nCount     ->  'je'? NUMERIC\nMultiple  -> NUMERIC UNIT? /[xX\303\240]|Set/u\nMeasure   -> NUMERIC UNIT UNIT?\nAddition  -> NUMERIC UNIT? '+'\nScale     ->  '/' NUMERIC? UNIT\nDose      -> '(' NUMERIC UNIT ')'\n"

Instance Method Summary (collapse)

Instance Method Details

- (Object) _composition_scale



96
97
98
99
100
# File 'src/model/part.rb', line 96

def _composition_scale
  if @composition && dose = @composition.doses.compact.first
    dose.scale
  end
end

- (Object) active_agents



31
32
33
# File 'src/model/part.rb', line 31

def active_agents
  @composition ? @composition.active_agents : []
end

- (Object) comparable_size



34
35
36
# File 'src/model/part.rb', line 34

def comparable_size
  ODDB::Dose.from_quanty(@comparable_size)
end

- (Object) dose_from_measure(measure)



77
78
79
80
# File 'src/model/part.rb', line 77

def dose_from_measure(measure)
  values = measure ? measure.childrens[0,2].collect{ |c| c.value } : [1,nil]
  Dose.new(*values)
end

- (Object) dose_from_multi(multi)



85
86
87
88
89
90
91
92
93
94
95
# File 'src/model/part.rb', line 85

def dose_from_multi(multi)
  unless(multi.nil?)
    multi.childrens.inject(UNIT) { |inj, node|
      unit = (node[1].value if node[1])
      dose = Dose.new(node[0].value, unit)
      inj *= dose
    }
  else
    UNIT
  end
end

- (Object) dose_from_scale(scale)



81
82
83
84
# File 'src/model/part.rb', line 81

def dose_from_scale(scale)
  values = scale ? scale.childrens[1,2].collect{ |c| c.value } : [1,nil]
  Dose.new(*values)
end

- (Object) multiplier



37
38
39
40
41
# File 'src/model/part.rb', line 37

def multiplier
  count = @count || 1
  addition = @addition || 0
  [@descr.to_f, 1].max * (@multi || 1).to_f * (count.to_i + addition.to_i)
end

- (Object) parse_size(size)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'src/model/part.rb', line 59

def parse_size(size)
  multi, addition, count, measure, scale, dose, comform = nil
  begin
    ast = @@parser.parse(size)
    multi, addition, count, measure, scale, dose, comform = ast.flatten
    count = (count ? count[1].value.to_i : 1)
  rescue ParseException, AmbigousParseException => e
    count = size.to_i
  end
  [
    (addition ? addition.first.value.to_i : 0),
    dose_from_multi(multi),
    count,
    dose_from_measure(measure),
    dose_from_scale(scale),
    (comform.value if comform),
  ]
end

- (Object) set_comparable_size!



42
43
44
45
46
47
48
49
# File 'src/model/part.rb', line 42

def set_comparable_size!
  measure = (@measure.nil? || @measure == UNIT) \
          ? _composition_scale \
          : @measure
  measure ||= UNIT
  scale = @scale || UNIT
  @comparable_size = multiplier * measure / scale
end

- (Object) size=(size)



50
51
52
53
54
55
56
57
58
# File 'src/model/part.rb', line 50

def size=(size)
  unless size.to_s.strip.empty?
    @addition, @multi, @count, @measure, @scale, @comform = parse_size(size) 
    if @count == 0
      @count, @multi = @multi, nil
    end
    set_comparable_size!
  end
end