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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ac7c868153deabc832a90c6b5cc93bee8a728526
4
- data.tar.gz: 2805144c6b6d959f13988a4ad37e3c2a949f56f8
3
+ metadata.gz: bf930ad9ebd6fd8480349ba1c78f893aa6d45003
4
+ data.tar.gz: ccc86704e031efe9d3bb5b01078d0e7093750364
5
5
  SHA512:
6
- metadata.gz: 9798202fcd229b5a334267bd8bf6f987e36918c26f357323c169483e389998cd23fe7883b3ed17cb157a4a9a8d3b5660fe0a46fde37cc454746d0c2d327f795f
7
- data.tar.gz: 59cc89386afca5024221c78ecd126c826ad001982206b8570b65f140a3f8045834636f9735aa5fe3f6c3060478531615ae52b4a731dce5e25b590d6358758d65
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"
@@ -1,3 +1,3 @@
1
1
  module Measured
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
@@ -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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips