Overview

This gem provides 2 things:

  1. a simple unit conversion library, and

  2. a pattern for transparently converting to/from base units.

Example scenario: You have a model that wants to store values with units (e.g., 200 lbs), but you want (or at least should want) to store all the values in the same base unit (e.g., 90.718474 kg). This gem makes using this design pattern totally transparent.

The main goal behind this library is to enable transparent handling of unit conversions on a model. The obvious use-case for this is to ensure all values of a similar system are stored in the same unit. The benefit is that you can then do batch computations on the data-set without having to worry about the unit each record’s value is stored in.

The unit conversions in this library are done as simply as possible by extending Numeric. Comprehensiveness and precision of the unit conversion capabilities are not on par with Ruby libraries such as Ruby-Units and Alchemist. If your needs are more based on converting in many different crazy systems then I suggest checking out those libraries.

One last quick note. The simple conversion approach taken here is faster than ruby-units or alchemist. In most cases this won’t matter, but it’s still worth noting…

Using It

  1. gem install convertable_values

  2. include ConvertableValues and “convert” your attributes

In a standard class:

class MyNormalClass include ConvertableValues attr_accessor :value, :unit convert(:value, :unit) end Including for your entire Rails project: # Stick in a file in initializers or in environment file ActiveRecord::Base.send(:include, ConvertableValues)

Contributing to convertable_values

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2010 Stephen Blankenship. See LICENSE.txt for further details.