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 +4 -4
- data/lib/unitwise/atom.rb +5 -6
- data/lib/unitwise/composable.rb +11 -1
- data/lib/unitwise/expression/parser.rb +1 -1
- data/lib/unitwise/expression/transformer.rb +4 -0
- data/lib/unitwise/version.rb +1 -1
- data/test/unitwise/atom_test.rb +10 -9
- data/test/unitwise/expression/parser_test.rb +18 -0
- data/test/unitwise/unit_test.rb +8 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59c17c683ada5799884a58c772a51eb20b59a5b8
|
4
|
+
data.tar.gz: f8fb3b7099821b43a090d3253edb6e93a728dad3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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? &&
|
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
|
51
|
-
|
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
|
data/lib/unitwise/composable.rb
CHANGED
@@ -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.
|
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
|
@@ -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
|
data/lib/unitwise/version.rb
CHANGED
data/test/unitwise/atom_test.rb
CHANGED
@@ -23,11 +23,12 @@ describe Unitwise::Atom do
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
let(:second)
|
27
|
-
let(:yard)
|
28
|
-
let(: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)
|
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 "#
|
110
|
-
it "must return the dim
|
111
|
-
second.
|
112
|
-
|
113
|
-
|
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
|
data/test/unitwise/unit_test.rb
CHANGED
@@ -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)
|
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.
|
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-
|
11
|
+
date: 2013-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: signed_multiset
|