Ruby on Rails Tuesday, February 9, 2016


On 2016-Feb-8, at 19:19 , David McDonald <daveomcd@gmail.com> wrote:

I have a model named, Measurable, that stores many different types of of measurements for different things.  All the measurements can be represented as a decimal, so my value column is of datatype decimal.  My users though wish to view and input these value is either integers or mixed numbers (ex: 31 1/8) as well, depending on the measurable_type.  Right now I'm able to convert the mixed number to a decimal value and store it into my database as a proper decimal. But I'm trying to figure out how to convert it from a decimal value to the mixed number I'm wanting to show the user in my form, so they can make edits if necessary.  Below I have the setter method I made, but my attempts at creating a proper getter haven't work.  I'm not really sure how to go about it.

I think you want to at least consider using Ruby's Rational class to help. Here's a pure Ruby example:

class Measurable

  attr_accessor :id, :unit_id, :measurable_type_id, :reliability_id, :created_at, :updated_at
  attr_reader :value

  # belongs_to :measurable_type
  def initialize(input)
    self.value = input
  end

  def value=(input)
    case input
    when String
      # Has more than one value
      if (parts = input.split(" ")).size > 1
        result = parts.reduce(0) {|whole,part| whole + Rational(part) }
      else
        result = input.to_f
      end
    when Numeric
      result = input
    else
      result = input.to_f
    end

    @value = result
  end

  def to_s
    case self.value
    when Rational
      whole = self.value.floor
      "%d %s"%[whole, (self.value - whole).to_s]
    else
      self.value.to_s
    end
  end
end

irb2.2.4> puts Measurable.new("31 1/8")
31 1/8
#2.2.4 => nil
irb2.2.4> puts Measurable.new("31.125")
31.125
#2.2.4 => nil
irb2.2.4> puts Measurable.new(31.125)
31.125
#2.2.4 => nil

There's also a to_r method to convert types to a Rational if your "measurable_type" wanted to convert strictly to a Rational form.

-Rob

No comments:

Post a Comment