Module: Measured::Parser

Extended by:
Parser
Included in:
Parser
Defined in:
lib/measured/parser.rb

Constant Summary collapse

PARSE_REGEX =
/
  \A         # beginning of input
  \s*        # optionally any whitespace
  (          # capture the value
    -?       # number can be negative
    \d+      # must have some digits
    (?:      # do not capture
      [\.\/] # period or slash to split fractional part
      \d+    # some digits after it
    )?       # fractional part is optional
  )
  \s*        # optionally any space between number and unit
  (          # capture the unit
    [a-zA-Z] # unit must start with a letter
    [\w-]*   # any word characters or dashes
    (?:      # non capturing group that is optional for multiple words
      \s+    # space in the unit for multiple words
      [\w-]+ # there must be something after the space
    )*       # allow many words
  )
  \s*        # optionally any whitespace
  \Z         # end of unit
/x

Instance Method Summary collapse

Instance Method Details

#parse_string(string) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/measured/parser.rb', line 29

def parse_string(string)
  raise Measured::UnitError, "Cannot parse blank measurement" if string.blank?

  result = PARSE_REGEX.match(string)

  raise Measured::UnitError, "Cannot parse measurement from '#{string}'" unless result

  [result.captures[0].to_r, -result.captures[1]]
end