Skip to content

Classification of units #17

@emersonthis

Description

@emersonthis

I have logic in place to prevent my users from trying to convert between incompatible units. For example: pounds --> gallons. I currently throw an error message, which explains that incompatible unit conversion is probably the issue. But I can't be certain, because the way I'm validating is by simply attempting the conversion and rescueing if it fails:

begin
    Measurement.parse("#{self.quantity} #{self.unit}").convert_to(self.ingredient.unit)
rescue
    errors.add(:unit, "is incompatible with associated ingredient unit (#{ingredient.unit}) in: #{ingredient.recipe.name}.  You are probably trying to mix weight and volume units.")
end

So this same validation error could also occur if a user attempted a nonsense unit. Ex: 37 blrblbs

I'm trying to figure out a way to be even more clear, and say something like:

"Impossible conversion! Pounds are a unit of weight but gallons are a unit of volume..."

I suspect that the library is already tracking this classification of units somewhere, but it does not appear to be exposed anywhere in the API? Would this be hard to do?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions