Project

weighable

0.01
No release in over 3 years
Low commit activity in last 3 years
Like BigDecimal, but for weight. Includes Rails integration.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.1
~> 2.13.0
~> 4.6.4
~> 10.0
~> 3.4.0
~> 0.49.0

Runtime

 Project Readme

Weighable

A gem for dealing with weights in ruby and rails.

Installation

Add this line to your application's Gemfile:

gem 'weighable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install weighable

Usage

Using the weight class is as simple as:

weight = Weighable::Weight.new(5, :gram)
weight = weight + Weighable::Weight.new(1, :ounce)
weight = weight.round(4)

This gets even cleaner when you include the core extensions (included by default with rails):

require 'weighable/core_ext'
weight = 5.grams
weight = weight + 1.ounce
weight = weight.round(4)

Usage with Rails

By default core extensions are included with rails. You also get some helpers to integrate weight into active record.

Add a weight attribute to a model:

class AddWeightToItem < ActiveRecord::Migration
  def change
    change_table :items do |t|
      t.weighable :my_weight
    end

    # OR

    add_weighable :items, :my_weight
  end
end

class Item < ActiveRecord::Base
  include Weighable::Model

  weighable :my_weight

  # OR require the field to be present

  weighable :my_weight, presence: true
end

# Now use it

item = Item.first
item.weight = 6.grams
item.save!
p item.weight == 6.grams # => true

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/greenbits/weighable.

License

The gem is available as open source under the terms of the MIT License.