Open sferik opened 8 years ago
I've found you can write an initializer that will override the existing definitions. For example:
# config/initializers/ruby_measurement/volume.rb
...
Measurement.define(:'c.') do |unit|
unit.alias :c, :cup, :cups
unit.convert_to(:gal) { |value| value / 16.0 }
unit.convert_to(:qt) { |value| value / 4.0 }
unit.convert_to(:pt) { |value| value / 2.0 }
unit.convert_to(:'fl oz') { |value| value * 8.0 }
unit.convert_to(:tbsp) { |value| value * 16.0 }
unit.convert_to(:tsp) { |value| value * 48.0 }
unit.convert_to(:ml) { |value| value * 236.5882365 }
unit.convert_to(:l) { |value| value * 0.2365882365 }
end
...
Hardly ideal, but might be useful. Perhaps make it easier to monkey patch existing measurements as well. Though perhaps I'm missing something in that regard. Perhaps include an extend
method or something that could be used for adding additional aliases and conversions. So that one could simply use:
Measurement.extend(:'c.') do |unit|
unit.alias :cuppa, :cuppas # Couldn't think of a better example here
unit.convert_to(:ml) { |value| value * 236.5882365 }
unit.convert_to(:l) { |value| value * 0.2365882365 }
end
@MyklClason That's something I always intended to add, but never got around to. I haven't needed this library in awhile, but I'm open to accepting changes for it!
Unit conversion numbers taken from Wikipedia.
In addition to adding this new functionality (and specs), I also cleaned up a bunch of RSpec warnings about error expectations without a specified error class.