Method: PhotoUtils::Length#initialize

Defined in:
lib/photo_utils/length.rb

#initialize(x) ⇒ Length

Returns a new instance of Length.



7
8
9
10
11
12
13
14
15
16
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
# File 'lib/photo_utils/length.rb', line 7

def initialize(x)
  case x
  when Length
    super(x)
  when Numeric
    super(x.to_f)
  when ''
    super(Math::Infinity)
  when String
    n = 0
    s = x.dup
    until s.empty?
      if s.gsub!(/^\s*(\d+(\.\d+)?)\s*/, '')
        n2 = $1.to_f
        n2 = if s.gsub!(/^('|ft\b)/, '')
          n2.feet
        elsif s.gsub!(/^("|in\b)/, '')
          n2.inches
        elsif s.gsub!(/^\s*m\b/, '')
          n2.meters
        elsif s.gsub!(/^\s*(mm\b)?/, '')
          n2.mm
        else
          raise "Can't parse unit: #{s.inspect}"
        end
        n += n2
      else
        raise "Can't parse number: #{s.inspect}"
      end
    end
    super(n)
  else
    raise "Can't make length from #{x.class}: #{x.inspect}"
  end
end