class MixedNumber

Public Class Methods

dump(obj) click to toggle source
# File lib/mixed_number_rails/mixed_number.rb, line 19
    def dump(obj)
            return nil if obj.nil?

            unless obj.is_a?(self) || obj.respond_to?(:to_m)
  raise ::ActiveRecord::SerializationTypeMismatch,
    "Attribute was supposed to be a #{self}, but was a #{obj.class}. -- #{obj.inspect}"
end

encode(obj.to_m)
    end
load(n) click to toggle source
# File lib/mixed_number_rails/mixed_number.rb, line 15
def load(n)
        self.parse(decode(n))
end
parse(input) click to toggle source
# File lib/mixed_number_rails/mixed_number.rb, line 9
def parse(input)
        parse_with_errors(input)
rescue MixedNumber::MixedNumberFormatError
        NullMixedNumber.new
end
Also aliased as: parse_with_errors
parse_with_errors(input)
Alias for: parse

Private Class Methods

decode(n) click to toggle source
# File lib/mixed_number_rails/mixed_number.rb, line 40
def decode(n)
        return n if !n

        if n =~ /--encoded--/
                f, w, n, d = *n.split(':')
                "#{w} #{n}/#{d}"
        else
                n.to_m
        end

end
encode(mixed) click to toggle source
# File lib/mixed_number_rails/mixed_number.rb, line 32
def encode(mixed)
        f = ("%.32f" % mixed.to_f)[0..32]
        w = mixed.whole.to_s
        n = mixed.fraction.numerator.to_s
        d = mixed.fraction.denominator.to_s
        [f, w, n, d].join(":") + ":--encoded--"
end