对于我的Rails项目,我正在寻找一个可以转换质量,体积和其他单位的库。
我需要将千克转换成克,将升转换成大汤匙等。
我认为应该看起来像这样:
class Product < ActiveRecord:Base
acts_as_physical_unit, :volume, :mass, :count
end
class Ingredient < ActiveRecord:Base
acts_as_physical_unit, :volume, :mass, :count
end
olive_oil = Product.new(:name => "Olive Oil", :volume => "1000 ml")
caesar_salad = Recipe.new(:name => "Caesar salad",
:ingredients => [
Ingredient.new(:product => [olive_oil], :volume => "5 tablespoons")
]
# In ingredients of "Caesar Salad" are 5 tablespoons of "Olive Oil" specified.
# We should create "Caesar Salad" for 50 persons.
# How mutch bottles of "Olive Oil" should be ordered ?
order = OrderItem.new(
:product => olive_oil,
:count => olive_oil.count_for(caesar_salad.ingredients.first)) * 50
)
这样的宝石是否存在?
谢谢。
最佳答案
您可能要尝试ruby-units:
您可以检查unit definitions看看它是否适合您!