measured 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a69b542ffb24ed97433592b86e4b9528adb4e95
4
- data.tar.gz: 1a7f476e754cdb1781fda2b31c630746f91977d3
3
+ metadata.gz: 81c1e2494e3547676ef4d146737363a2fbd34f84
4
+ data.tar.gz: 13dc02fd3f8f64a2e3efe86ec6c93d4c3e6530fe
5
5
  SHA512:
6
- metadata.gz: 0a864576e69e8a8df51ba081597ab43c684220c6485b1c04da07fb48cbabe0a15532055e197bfb43c3ad5a14066f8b2c949d103ffa10465fd3d068a5d7fe3cc9
7
- data.tar.gz: 955237150a140886a4be19ae03de0d8745befdfa076db4f9239b01964631f4064a6de13680bb846ff5cb058987e0a42801819b32159563234afc20d1926549df
6
+ metadata.gz: dba3840951fee6ac5bbe8bb42ca8ae4371d722b16acd48ca03f03c06dc68094d66d78b6cd659513bec5a8fa5c11c17048ca5789df09a634c6211bf76f9437050
7
+ data.tar.gz: 463f44d4a37facac37d45b1382d91ac176234500a4f1fdcc1bfb3420ee4672e29b014dbe78d6f40b052e118bebffb0745a6d653238d3d33512a7bd50ce19d492
data/README.md CHANGED
@@ -85,6 +85,13 @@ Measured::Weight.new(3, :g) * 2
85
85
  > #<Measured::Weight 6 g>
86
86
  ```
87
87
 
88
+ Converts units only as needed for equality comparison:
89
+
90
+ ```ruby
91
+ > Measured::Weight.new(1000, :g) == Measured::Weight.new(1, :kg)
92
+ true
93
+ ```
94
+
88
95
  Extract the unit and the value:
89
96
 
90
97
  ```ruby
@@ -23,6 +23,8 @@ class Measured::Measurable
23
23
 
24
24
  def convert_to(new_unit)
25
25
  new_unit_name = self.class.conversion.to_unit_name(new_unit)
26
+ return self if new_unit_name == self.class.conversion.to_unit_name(unit)
27
+
26
28
  value = self.class.conversion.convert(@value, from: @unit, to: new_unit_name)
27
29
 
28
30
  self.class.new(value, new_unit)
@@ -46,13 +48,17 @@ class Measured::Measurable
46
48
  end
47
49
 
48
50
  def <=>(other)
49
- if other.is_a?(self.class) && unit == other.unit
50
- value <=> other.value
51
+ if other.is_a?(self.class)
52
+ other_converted = other.convert_to(unit)
53
+ value <=> other_converted.value
51
54
  end
52
55
  end
53
56
 
54
57
  def ==(other)
55
- !!(other.is_a?(self.class) && unit == other.unit && value == other.value)
58
+ return false unless other.is_a?(self.class)
59
+
60
+ other_converted = other.convert_to(unit)
61
+ value == other_converted.value
56
62
  end
57
63
 
58
64
  alias_method :eql?, :==
@@ -75,5 +81,9 @@ class Measured::Measurable
75
81
  conversion.unit_names_with_aliases
76
82
  end
77
83
 
84
+ def name
85
+ to_s.split("::").last.underscore.humanize.downcase
86
+ end
87
+
78
88
  end
79
89
  end
@@ -1,3 +1,3 @@
1
1
  module Measured
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,3 @@
1
+ classification: library
2
+ ci_urls:
3
+ - https://travis-ci.org/Shopify/measured
@@ -77,6 +77,16 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
77
77
  refute Magic.valid_unit?("junk")
78
78
  end
79
79
 
80
+ test ".name looks at the class name" do
81
+ module Example
82
+ class VeryComplexThing < Measured::Measurable ; end
83
+ end
84
+
85
+ assert_equal "magic", Magic.name
86
+ assert_equal "measurable", Measured::Measurable.name
87
+ assert_equal "very complex thing", Example::VeryComplexThing.name
88
+ end
89
+
80
90
  test "#convert_to raises on an invalid unit" do
81
91
  assert_raises Measured::UnitError do
82
92
  @magic.convert_to(:punch)
@@ -86,13 +96,19 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
86
96
  test "#convert_to returns a new object of the same type in the new unit" do
87
97
  converted = @magic.convert_to(:arcane)
88
98
 
89
- refute_equal converted, @magic
99
+ assert_equal converted, @magic
100
+ refute_equal converted.object_id, @magic.object_id
90
101
  assert_equal BigDecimal(10), @magic.value
91
102
  assert_equal "magic_missile", @magic.unit
92
103
  assert_equal BigDecimal(1), converted.value
93
104
  assert_equal "arcane", converted.unit
94
105
  end
95
106
 
107
+ test "#convert_to from and to the same unit returns the same object" do
108
+ converted = @magic.convert_to(@magic.unit)
109
+ assert_equal converted.object_id, @magic.object_id
110
+ end
111
+
96
112
  test "#convert_to! replaces the existing object with a new version in the new unit" do
97
113
  converted = @magic.convert_to!(:arcane)
98
114
 
@@ -113,8 +129,8 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
113
129
  assert_equal "#<Magic: 0.1234E1 magic_missile>", Magic.new(1.234, :magic_missile).inspect
114
130
  end
115
131
 
116
- test "#<=> compares only if the class and unit are the same" do
117
- assert_nil @magic <=> Magic.new(10, :fire)
132
+ test "#<=> compares regardless of the unit" do
133
+ assert_equal -1, @magic <=> Magic.new(10, :fire)
118
134
  assert_equal 1, @magic <=> Magic.new(9, :magic_missile)
119
135
  assert_equal 0, @magic <=> Magic.new(10, :magic_missile)
120
136
  assert_equal -1, @magic <=> Magic.new(11, :magic_missile)
@@ -123,13 +139,29 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
123
139
  test "#== should be the same if the classes, unit, and amount match" do
124
140
  assert @magic == @magic
125
141
  assert Magic.new(10, :magic_missile) == Magic.new("10", "magic_missile")
126
- refute Magic.new(1, :arcane) == Magic.new(10, :magic_missile)
142
+ assert Magic.new(1, :arcane) == Magic.new(10, :magic_missile)
143
+ refute Magic.new(1, :arcane) == Magic.new(10.1, :magic_missile)
144
+ end
145
+
146
+ test "#== should be the same if the classes and amount match but the unit does not so they convert" do
147
+ assert Magic.new(2, :magic_missile) == Magic.new("1", "ice")
148
+ end
149
+
150
+ test "#> and #< should compare measurements" do
151
+ assert Magic.new(10, :magic_missile) < Magic.new(20, :magic_missile)
152
+ refute Magic.new(10, :magic_missile) > Magic.new(20, :magic_missile)
153
+ end
154
+
155
+ test "#> and #< should compare measurements of different units" do
156
+ assert Magic.new(10, :magic_missile) < Magic.new(100, :ice)
157
+ refute Magic.new(10, :magic_missile) > Magic.new(100, :ice)
127
158
  end
128
159
 
129
- test "#eql? should be the same if the classes, unit, and amount match" do
160
+ test "#eql? should be the same if the classes and amount match, and unit is converted" do
130
161
  assert @magic == @magic
131
162
  assert Magic.new(10, :magic_missile) == Magic.new("10", "magic_missile")
132
- refute Magic.new(1, :arcane) == Magic.new(10, :magic_missile)
163
+ assert Magic.new(1, :arcane) == Magic.new(10, :magic_missile)
164
+ refute Magic.new(1, :arcane) == Magic.new(10.1, :magic_missile)
133
165
  end
134
166
 
135
167
  end
@@ -9,6 +9,10 @@ class Measured::LengthTest < ActiveSupport::TestCase
9
9
  assert_equal ["cm", "ft", "in", "m", "mm", "yd"], Measured::Length.units
10
10
  end
11
11
 
12
+ test ".name" do
13
+ assert_equal "length", Measured::Length.name
14
+ end
15
+
12
16
  test ".convert_to from cm to cm" do
13
17
  assert_conversion Measured::Length, "2000 cm", "2000 cm"
14
18
  end
@@ -13,6 +13,10 @@ class Measured::WeightTest < ActiveSupport::TestCase
13
13
  assert_equal ["g", "kg", "lb", "oz"], Measured::Weight.units
14
14
  end
15
15
 
16
+ test ".name" do
17
+ assert_equal "weight", Measured::Weight.name
18
+ end
19
+
16
20
  test ".convert_to from g to g" do
17
21
  assert_conversion Measured::Weight, "2000 g", "2000 g"
18
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -119,6 +119,7 @@ files:
119
119
  - lib/measured/units/weight.rb
120
120
  - lib/measured/version.rb
121
121
  - measured.gemspec
122
+ - repodb.yml
122
123
  - test/arithmetic_test.rb
123
124
  - test/conversion_table_test.rb
124
125
  - test/conversion_test.rb