class UnitSoup::Measurement

Attributes

amount[R]
unit[R]

Public Class Methods

from(*args) click to toggle source
# File lib/unit_soup/measurement.rb, line 14
def self.from(*args)
  self.new *args
end
new(*args) click to toggle source
# File lib/unit_soup/measurement.rb, line 20
def initialize(*args)
  case args.length
  when 1
    if args[0].is_a? Measurement
      @amount = args[0].amount
      @unit = args[0].unit
    else
      str = args[0]
      raise ArgumentError.new("No argument provided") unless str
      str = str.to_s
      match_data = str.to_s.gsub("\s", "").match(@@measurement_format)
      raise ArgumentError.new("Format: 12 inch") unless match_data
      @amount = match_data[1].to_r
      @unit = Unit.new(match_data[2])
    end
  else
    @amount = args[0].is_a?(String) ? args[0].to_r : args[0].rationalize
    @unit = Unit.new(args[1].to_sym)
  end
end
valid?(str) click to toggle source
# File lib/unit_soup/measurement.rb, line 10
def self.valid?(str)
  str && !str.to_s.gsub("\s", "").match(@@measurement_format).nil?
end

Public Instance Methods

==(o) click to toggle source
# File lib/unit_soup/measurement.rb, line 45
def ==(o)
  amount == o.amount && unit == o.unit
end
eql?(o) click to toggle source
# File lib/unit_soup/measurement.rb, line 49
def eql?(o)
  amount == o.amount && unit == o.unit
end
hash() click to toggle source
# File lib/unit_soup/measurement.rb, line 53
def hash
  to_s.hash
end
to_s() click to toggle source
# File lib/unit_soup/measurement.rb, line 41
def to_s
  "#{amount} #{unit}"
end