measurement 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.2.2 Better formatting functions
2
+
1
3
  v0.2.1 Added methods to Float, Fixnum and String - to_measurement
2
4
 
3
5
  v0.2 Documentation
@@ -21,7 +21,8 @@ module Measurement
21
21
  end
22
22
 
23
23
  def format(amount, precision = 2)
24
- prefix + ("%.#{precision}f" % to(amount)) + suffix
24
+ number = ("%.#{precision}f" % to(amount)).sub(/(\.\d*)0/, '\1')
25
+ prefix + number + suffix
25
26
  end
26
27
 
27
28
  def prefix
@@ -17,7 +17,7 @@ require File.join(File.dirname(__FILE__), '..', 'measurement')
17
17
  #
18
18
  class Weight < Measurement::Base
19
19
  base :gram, :grams, :suffix => 'g'
20
- unit 1000.0, :kilograms, :kg, :kgs, :suffix => 'kg'
20
+ unit 1000.0, :kilogram, :kilograms, :kg, :kgs, :suffix => 'kg'
21
21
  unit 453.59236, :pounds, :pound, :lbs, :suffix => 'lbs'
22
22
  unit 28.3495231, :ounces, :ounce, :oz, :suffix => 'oz'
23
23
  unit 6350.29318, :stone, :st, :suffix => 'st'
data/lib/measurement.rb CHANGED
@@ -1,7 +1,18 @@
1
1
  require File.join(File.dirname(__FILE__), 'measurement', 'unit')
2
2
 
3
3
  module Measurement
4
- class NoUnitFoundException < Exception; end
4
+ class NoUnitFoundException < Exception
5
+ attr_reader :unit
6
+
7
+ def initialize(unit)
8
+ @unit = unit
9
+ super
10
+ end
11
+
12
+ def to_s
13
+ "No unit found: #{@unit}"
14
+ end
15
+ end
5
16
 
6
17
  # The Measurement::Base class provides a basis for types of
7
18
  # measurement. For example, length or weight. It should
@@ -100,15 +111,21 @@ module Measurement
100
111
  end
101
112
 
102
113
  def self.fetch_scale(scale = nil) # :nodoc:
103
- scale.nil? ? base : units.detect do |unit|
104
- unit.has_name?(scale)
114
+ unit = (scale.nil? ? base : units.detect do |unit|
115
+ unit.has_name?(scale.to_sym)
116
+ end)
117
+
118
+ unless unit
119
+ raise NoUnitFoundException.new(scale)
120
+ else
121
+ unit
105
122
  end
106
123
  end
107
124
 
108
125
  def self.find_scale(scale) # :nodoc:
109
126
  units.detect do |unit|
110
- unit.has_name?(scale) ||
111
- unit.suffix == scale
127
+ unit.has_name?(scale.to_sym) ||
128
+ unit.suffix == scale.to_s
112
129
  end
113
130
  end
114
131
 
@@ -135,7 +152,7 @@ module Measurement
135
152
  #
136
153
  # If a valid unit cannot be found an error is raised:
137
154
  #
138
- # Weight.parse("180cm") => Measurement::NoUnitException
155
+ # Weight.parse("180cm") => Measurement::NoUnitFoundException
139
156
  #
140
157
  def self.parse(string)
141
158
  string = string.dup
data/measurement.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{measurement}
5
- s.version = "0.2.1"
5
+ s.version = "0.2.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jeremy Wells"]
@@ -17,6 +17,24 @@ describe Measurement::Base do
17
17
  end
18
18
  end
19
19
 
20
+ describe '::find_scale' do
21
+ it 'should be able to find the unit by name' do
22
+ Weight.find_scale(:kilogram).should_not be_nil
23
+ Weight.find_scale(:kilogram).has_name?(:kilogram).should be_true
24
+ end
25
+ it 'should be able to find the unit if the name passed is a string' do
26
+ Weight.find_scale("kilogram").should_not be_nil
27
+ Weight.find_scale(:kilogram).has_name?(:kilogram).should be_true
28
+ end
29
+ it 'should be able to find the unit by suffix' do
30
+ Weight.unit 2000, :quit, :qt, :suffix => 'q'
31
+ Weight.find_scale('q').should_not be_nil
32
+ end
33
+ it 'should return nil if no unit is found' do
34
+ Weight.find_scale('gigboot').should be_nil
35
+ end
36
+ end
37
+
20
38
  describe '::parse' do
21
39
  it 'should parse in the base unit if no unit is specified' do
22
40
  Length.parse('113').to_f.should == 113.0
@@ -28,6 +28,10 @@ describe Measurement::Unit do
28
28
  @unit.format(200,2).should == "1.33"
29
29
  end
30
30
 
31
+ it 'should remove the end 0' do
32
+ @unit.format(255, 2).should == '1.7'
33
+ end
34
+
31
35
  it 'should apply the suffix' do
32
36
  @unit = Measurement::Unit.new(150.0, :test, :suffix => 'test')
33
37
  @unit.format(150, 0).should == "1test"
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Weight do
4
- it 'should use metres as a base' do
4
+ it 'should use grams as a base' do
5
5
  Weight.new(1).to_s(nil, 0).should == '1g'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measurement
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Wells