unitwise 0.2.1 → 0.2.2

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: 7cf611ec88dbc4e07823a8237da220ddc2747dd0
4
- data.tar.gz: 8efa28b4bbf55900b05cac6f25655b9fe6476f11
3
+ metadata.gz: 59c17c683ada5799884a58c772a51eb20b59a5b8
4
+ data.tar.gz: f8fb3b7099821b43a090d3253edb6e93a728dad3
5
5
  SHA512:
6
- metadata.gz: 9e2dc02863b592f53e73912d43c536388a54dad320dff00c28855645aa9e59071e9328ea7c12a8c151db567e476160179efc404e6898df7955d5e940ca8f7b10
7
- data.tar.gz: 5775f6a107d7b953bb40a38edcde038b88ba20c020049c67eb747cba29fdf9803f6b7a23c320d4db7f404eadb90a7cf7327a1d3e299f00d93bbca9b37c2e2298
6
+ metadata.gz: 7ebe5b4426d7adf2a107fa4ab122ebffffcc79468b342e9592b06e0dc434fe90582db96a5e563c1c755bdd8b66fa26c5146c59879c8e66c9e2d697a9d1e4762f
7
+ data.tar.gz: 685a1a81769b575066d608572038ebaf43eab8c04bf9e635971ec7ff8080fee248ea1f549ba1d9e94d95af8032959f5080fcbec091846e346cb4e8d28fa7b8dc
data/lib/unitwise/atom.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Unitwise
2
2
  class Atom < Base
3
- attr_accessor :classification, :property, :metric, :special
4
- attr_accessor :arbitrary, :dim
3
+ attr_accessor :classification, :property, :metric, :special, :arbitrary
4
+ attr_writer :dim
5
5
 
6
6
  include Unitwise::Composable
7
7
 
@@ -16,7 +16,7 @@ module Unitwise
16
16
  end
17
17
 
18
18
  def base?
19
- scale.nil? && !dim.nil?
19
+ scale.nil? && !@dim.nil?
20
20
  end
21
21
 
22
22
  def derived?
@@ -47,8 +47,8 @@ module Unitwise
47
47
  depth <= 3
48
48
  end
49
49
 
50
- def key
51
- base? ? dim : property
50
+ def dim
51
+ terminal? ? @dim || property : composition_string
52
52
  end
53
53
 
54
54
  def scale=(attrs)
@@ -67,7 +67,6 @@ module Unitwise
67
67
  scale.functional(x, direction)
68
68
  end
69
69
 
70
-
71
70
  def root_terms
72
71
  base? ? [Term.new(atom: self)] : scale.root_terms
73
72
  end
@@ -7,10 +7,20 @@ module Unitwise
7
7
 
8
8
  def composition
9
9
  root_terms.reduce(SignedMultiset.new) do |s, t|
10
- s.increment(t.atom.key, t.exponent) if t.atom; s
10
+ s.increment(t.atom.dim, t.exponent) if t.atom; s
11
11
  end
12
12
  end
13
13
 
14
+ def dim
15
+ composition_string
16
+ end
17
+
18
+ def composition_string
19
+ composition.sort.map do |k,v|
20
+ v == 1 ? k.to_s : "#{k}#{v}"
21
+ end.join('.')
22
+ end
23
+
14
24
  def similar_to?(other)
15
25
  self.composition == other.composition
16
26
  end
@@ -45,7 +45,7 @@ module Unitwise
45
45
  end
46
46
 
47
47
  rule (:expression) do
48
- (group | term).as(:left) >> (operator >> expression.as(:right)).maybe
48
+ ((group | term).as(:left)).maybe >> (operator >> expression.as(:right)).maybe
49
49
  end
50
50
 
51
51
  end
@@ -8,6 +8,10 @@ module Unitwise
8
8
  rule(atom_code: simple(:c)) { Atom.find(c) }
9
9
  rule(term: subtree(:h)) { Term.new(h) }
10
10
 
11
+ rule(operator: simple(:o), right: simple(:r)) do
12
+ o == '/' ? r ** -1 : r
13
+ end
14
+
11
15
  rule(left: simple(:l), operator: simple(:o), right: simple(:r)) do
12
16
  o == '/' ? l / r : l * r
13
17
  end
@@ -1,3 +1,3 @@
1
1
  module Unitwise
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -23,11 +23,12 @@ describe Unitwise::Atom do
23
23
  end
24
24
  end
25
25
 
26
- let(:second) { Unitwise::Atom.find("s") }
27
- let(:yard) { Unitwise::Atom.find("[yd_i]")}
28
- let(:pi) { Unitwise::Atom.find("[pi]")}
26
+ let(:second) { Unitwise::Atom.find("s") }
27
+ let(:yard) { Unitwise::Atom.find("[yd_i]")}
28
+ let(:pi) { Unitwise::Atom.find("[pi]")}
29
29
  let(:celsius) { Unitwise::Atom.find("Cel")}
30
- let(:pfu) { Unitwise::Atom.find("[PFU]")}
30
+ let(:pfu) { Unitwise::Atom.find("[PFU]")}
31
+ let(:joule) { Unitwise::Atom.find("J")}
31
32
  describe "#scale" do
32
33
  it "must be nil for base atoms" do
33
34
  second.scale.must_equal nil
@@ -106,11 +107,11 @@ describe Unitwise::Atom do
106
107
  end
107
108
  end
108
109
 
109
- describe "#key" do
110
- it "must return the dim or the property" do
111
- second.key.must_equal "T"
112
- pi.key.must_equal "number"
113
- celsius.key.must_equal "temperature"
110
+ describe "#dim" do
111
+ it "must return the dim" do
112
+ second.dim.must_equal 'T'
113
+ yard.dim.must_equal 'L'
114
+ joule.dim.must_equal 'L2.M.T-2'
114
115
  end
115
116
  end
116
117
 
@@ -87,5 +87,23 @@ describe Unitwise::Expression::Parser do
87
87
  end
88
88
  end
89
89
 
90
+ describe "#expression" do
91
+ it "must match left only" do
92
+ match = subject.expression.parse('m')
93
+ match[:left][:term][:atom][:atom_code].must_equal("m")
94
+ end
95
+ it "must match left + right + operator" do
96
+ match = subject.expression.parse('m.s')
97
+ match[:left][:term][:atom][:atom_code].must_equal("m")
98
+ match[:operator].must_equal('.')
99
+ match[:right][:left][:term][:atom][:atom_code].must_equal('s')
100
+ end
101
+ it "must match operator + right" do
102
+ match = subject.expression.parse("/s")
103
+ match[:operator].must_equal('/')
104
+ match[:right][:left][:term][:atom][:atom_code].must_equal('s')
105
+ end
106
+ end
107
+
90
108
 
91
109
  end
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  describe Unitwise::Unit do
4
4
 
5
5
  let(:ms2) { Unitwise::Unit.new("m/s2") }
6
- let(:kg) { Unitwise::Unit.new("kg") }
6
+ let(:kg) { Unitwise::Unit.new("kg") }
7
7
  let(:psi) { Unitwise::Unit.new("[psi]")}
8
8
  let(:deg) { Unitwise::Unit.new("deg")}
9
9
 
@@ -40,6 +40,13 @@ describe Unitwise::Unit do
40
40
  end
41
41
  end
42
42
 
43
+ describe "#dim" do
44
+ it "must be a string representing it's dimensional makeup" do
45
+ ms2.dim.must_equal 'L.T-2'
46
+ psi.dim.must_equal 'L-1.M.T-2'
47
+ end
48
+ end
49
+
43
50
  describe "#*" do
44
51
  it "should multiply units" do
45
52
  mult = kg * ms2
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.1
4
+ version: 0.2.2
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-12-14 00:00:00.000000000 Z
11
+ date: 2013-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: signed_multiset