measured 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/measured/base.rb +18 -0
- data/lib/measured/version.rb +1 -1
- data/test/units/length_test.rb +5 -0
- data/test/units/weight_test.rb +6 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf930ad9ebd6fd8480349ba1c78f893aa6d45003
|
4
|
+
data.tar.gz: ccc86704e031efe9d3bb5b01078d0e7093750364
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37afaff5fef0534f3687c717894f42f043b4896e3ac7de13e245a9a69372cf659153f77825fbc3afa0281b12b1d0b02af48374e0197a016db6f7dfcef721a43d
|
7
|
+
data.tar.gz: 7668e545f8c31b50c44ee41de0f5cb516d0a0752fe6fec6d10813964633012852d9f0c4806ebb88345a921c07334865ccba75f0b45d03d193df888a5814b127f
|
data/README.md
CHANGED
@@ -162,6 +162,14 @@ You can skip these and only define your own units by doing:
|
|
162
162
|
gem 'measured', require: 'measured/base'
|
163
163
|
```
|
164
164
|
|
165
|
+
### Shortcut syntax
|
166
|
+
|
167
|
+
There is a shortcut initialization syntax for modules inside the `Measured` namespace, similar to `BigDecimal(123)` vs `BigDecimal.new(123)`:
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
Measured::Weight(1, :g)
|
171
|
+
```
|
172
|
+
|
165
173
|
### Adding new units
|
166
174
|
|
167
175
|
Extending this library to support other units is simple. To add a new conversion, subclass `Measured::Measurable`, define your base units, then add your conversion units.
|
data/lib/measured/base.rb
CHANGED
@@ -4,6 +4,24 @@ require "bigdecimal"
|
|
4
4
|
|
5
5
|
module Measured
|
6
6
|
class UnitError < StandardError ; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def method_missing(method, *args)
|
10
|
+
class_name = "Measured::#{ method }"
|
11
|
+
|
12
|
+
if Measured::Measurable.subclasses.map(&:to_s).include?(class_name)
|
13
|
+
klass = class_name.constantize
|
14
|
+
|
15
|
+
Measured.define_singleton_method(method) do |value, unit|
|
16
|
+
klass.new(value, unit)
|
17
|
+
end
|
18
|
+
|
19
|
+
klass.new(*args)
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
7
25
|
end
|
8
26
|
|
9
27
|
require "measured/arithmetic"
|
data/lib/measured/version.rb
CHANGED
data/test/units/length_test.rb
CHANGED
@@ -13,6 +13,11 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
13
13
|
assert_equal "length", Measured::Length.name
|
14
14
|
end
|
15
15
|
|
16
|
+
test "Measured::Length() delegates automatically to .new" do
|
17
|
+
assert_equal Measured::Length.new(1, :in), Measured::Length(1, :in)
|
18
|
+
assert_equal Measured::Length.new(200, :mm), Measured::Length(20, :cm)
|
19
|
+
end
|
20
|
+
|
16
21
|
test ".convert_to from cm to cm" do
|
17
22
|
assert_conversion Measured::Length, "2000 cm", "2000 cm"
|
18
23
|
end
|
data/test/units/weight_test.rb
CHANGED
@@ -13,6 +13,12 @@ class Measured::WeightTest < ActiveSupport::TestCase
|
|
13
13
|
assert_equal ["g", "kg", "lb", "oz"], Measured::Weight.units
|
14
14
|
end
|
15
15
|
|
16
|
+
|
17
|
+
test "Measured::Weight() delegates automatically to .new" do
|
18
|
+
assert_equal Measured::Weight.new(1, :lb), Measured::Weight(1, :lb)
|
19
|
+
assert_equal Measured::Weight.new(2000, :g), Measured::Weight(2, :kg)
|
20
|
+
end
|
21
|
+
|
16
22
|
test ".name" do
|
17
23
|
assert_equal "weight", Measured::Weight.name
|
18
24
|
end
|