Module: Squib::Args::UnitConversion

Defined in:
lib/squib/args/unit_conversion.rb

Class Method Summary collapse

Class Method Details

.parse(arg, dpi = 300, cell_px = 37.5) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/squib/args/unit_conversion.rb', line 7

module_function def parse(arg, dpi=300, cell_px=37.5)
  case arg.to_s.rstrip
  when /in$/ # ends with "in"
    arg.rstrip[0..-2].to_f * dpi
  when /pt$/ # ends with "in"
    arg.rstrip[0..-2].to_f * dpi / POINTS_PER_IN
  when /cm$/ # ends with "cm"
    arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM
  when /mm$/ # ends with "mm"
    arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM / 10.0
  when /deg$/ # ends with "deg"
    arg.rstrip[0..-3].to_f * (Math::PI / 180.0)
  when /c(ell)?[s]?$/ # ends with 'c', 'cell', or 'cells'
    arg.sub(/c(ell)?[s]?$/, '').to_f * cell_px
  else
    arg
  end
end