Skip to content

shemerey/weight

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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