unitwise 0.2.0 → 0.2.1

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: 7b43a98361a2647155beb5a86d6bc1281e8f160c
4
- data.tar.gz: 25dbd408ecc497a0003592c17c41b3636537d2bb
3
+ metadata.gz: 7cf611ec88dbc4e07823a8237da220ddc2747dd0
4
+ data.tar.gz: 8efa28b4bbf55900b05cac6f25655b9fe6476f11
5
5
  SHA512:
6
- metadata.gz: 1e1b4d53bc2c1cbe081df9a5df31d82872ae390773831558137fba10139d27fb9e4fab531149f5f876a5c4a7bf57d97340ef70e671d0fbabaa5c930a953f96a1
7
- data.tar.gz: f17f89d11af392f7c9b520ad4defe197ddf9657ccb7bce721923f59a229caf5336ab892653c01fc9f6d5d546864bca1b7c298cd5e56ccb54e480e80fcc90bc8e
6
+ metadata.gz: 9e2dc02863b592f53e73912d43c536388a54dad320dff00c28855645aa9e59071e9328ea7c12a8c151db567e476160179efc404e6898df7955d5e940ca8f7b10
7
+ data.tar.gz: 5775f6a107d7b953bb40a38edcde038b88ba20c020049c67eb747cba29fdf9803f6b7a23c320d4db7f404eadb90a7cf7327a1d3e299f00d93bbca9b37c2e2298
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in unitwise.gemspec
4
+ gem 'pry'
4
5
  gemspec
@@ -14,10 +14,16 @@ module Unitwise
14
14
  end
15
15
 
16
16
  def decompose(expression)
17
- begin
18
- Decomposer.new(expression).terms
19
- rescue ExpressionError
20
- nil
17
+ expression = expression.to_s
18
+ @decompose ||= {}
19
+ if @decompose.has_key?(expression)
20
+ @decompose[expression]
21
+ else
22
+ @decompose[expression] = begin
23
+ Decomposer.new(expression).terms
24
+ rescue ExpressionError
25
+ nil
26
+ end
21
27
  end
22
28
  end
23
29
  end
@@ -4,10 +4,8 @@ class Numeric
4
4
  end
5
5
 
6
6
  def method_missing(meth, *args, &block)
7
- if Unitwise::Expression.decompose(meth)
8
- self.convert(meth)
9
- else
10
- super(meth, *args, &block)
11
- end
7
+ convert(meth)
8
+ rescue Unitwise::ExpressionError
9
+ super(meth, *args)
12
10
  end
13
11
  end
@@ -1,6 +1,13 @@
1
1
  module Unitwise
2
2
  class Measurement < Scale
3
3
 
4
+ def initialize(value, unit)
5
+ super(value, unit)
6
+ if terms.nil?
7
+ raise ExpressionError, "Could not evaluate `#{unit}`."
8
+ end
9
+ end
10
+
4
11
  def convert(other_unit)
5
12
  other_unit = Unit.new(other_unit)
6
13
  if similar_to?(other_unit)
@@ -21,6 +21,10 @@ module Unitwise
21
21
  unit.atoms
22
22
  end
23
23
 
24
+ def terms
25
+ unit.terms
26
+ end
27
+
24
28
  def special?
25
29
  unit.special?
26
30
  end
@@ -1,7 +1,6 @@
1
1
  module Unitwise
2
2
  class Unit
3
3
  include Unitwise::Composable
4
- attr_writer :terms, :expression
5
4
 
6
5
  def initialize(input)
7
6
  if input.respond_to?(:expression)
@@ -1,3 +1,3 @@
1
1
  module Unitwise
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -7,6 +7,9 @@ describe Unitwise::Measurement do
7
7
  subject.value.must_equal(1)
8
8
  subject.unit.to_s.must_equal('m/s')
9
9
  end
10
+ it "should raise an error for unknown units" do
11
+ ->{ Unitwise::Measurement.new(1,"funkitron") }.must_raise(Unitwise::ExpressionError)
12
+ end
10
13
  end
11
14
 
12
15
  describe "#unit" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unitwise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Lewis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-14 00:00:00.000000000 Z
11
+ date: 2013-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: signed_multiset
@@ -210,3 +210,4 @@ test_files:
210
210
  - test/unitwise/term_test.rb
211
211
  - test/unitwise/unit_test.rb
212
212
  - test/unitwise_test.rb
213
+ has_rdoc: