class TapeMeasure::Parser

Parse Strings into units and math them

Attributes

match[R]
scalar[R]
unit[R]
value[R]

Public Class Methods

new(string) click to toggle source
# File lib/tape_measure/parser.rb, line 10
def initialize(string)
  @string  = string
  if @string.is_a?(String)
    parse
  else
    @value = string
  end
end

Public Instance Methods

parse() click to toggle source
# File lib/tape_measure/parser.rb, line 19
def parse
  begin
    mixed_value = LengthGrammar.parse(@string.strip).value
    @unit = mixed_value.units
    @scalar = mixed_value.scalar
    @value = mixed_value.compatible?("in") ? (mixed_value >> "in").scalar : @scalar.to_f

    @match = true
  rescue Citrus::ParseError => ex
    ex.message
    @match = false
  end
end