Project

weight

0.0
No commit activity in last 3 years
No release in over 3 years
Dead simple value object, with one purpose work with weight
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0
>= 0
 Project Readme

Weight (1.0.0 version)

  • Code Climate
  • Build Status
  • Gem Version

It's dead simple Value object, which provides convenient way to work with weight in a different unit systems. It could be useful if you have to work with different unit system, for example you have to work with :kg as well as :lb for USA

Compatible with:

  • mri 2.0.0
  • mri 1.9.3
  • mri 1.9.2
  • jruby-19mode
  • rbx-19mode
  • jruby-18mode
  • mri 1.8.7
  • ree

Installation

Add this line to your application's Gemfile:

gem 'weight'

And then execute:

$ bundle

Or install it yourself as:

$ gem install weight

Configuration

You could configure default unit system (:kg or :lb)

just put this in your initializer: config/initializers/weight.rb

  # new object in kg by default
  Weight.new # => <Weight: @input_value=0, @input_unit=:kg>

  Weight::Configuration.configure do |config|
    config.default_unit = :lb  # by default it uses :kg
  end

  # Now default object looks like
  Weight.new # => <Weight: @input_value=0, @input_unit=:lb>

Usage

Basic Math with Weight objects

 Weight.new(1, :kg) + Weight.new(1, :kg) == Weight.new(2, :kg)
 Weight.new(1, :kg) - Weight.new(1, :kg) == Weight.new(0, :kg)

 Weight.new(1, :kg) * 2 == Weight.new(2, :kg)
 Weight.new(2, :kg) / 2 == Weight.new(1, :kg)

Convert result to the first object unit system

 Weight.new(1, :kg) + Weight.new(1, :lb) # =>   #<Weight: @input_value=1.4536, @input_unit=:kg>
 Weight.new(1, :lb) + Weight.new(1, :kg) # =>   #<Weight: @input_value=3.2046, @input_unit=:lb>

Basic comparison with Weight objects

 Weight.new(3, :lb).between?(Weight.new(1, :kg), Weight.new(2, :kg))
 Weight.new(1, :kg) > Weight.new(2, :lb)
 Weight.new(1, :lb) <= Weight.new(0.5, :kg)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Bitdeli Badge