unitwise 0.9.1 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/unitwise.rb +2 -2
- data/lib/unitwise/search.rb +44 -0
- data/lib/unitwise/unit.rb +7 -0
- data/lib/unitwise/version.rb +1 -1
- data/test/unitwise/search_test.rb +18 -0
- metadata +33 -34
- data/lib/unitwise/compound.rb +0 -83
- data/test/unitwise/compound_test.rb +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b218dca039e22556a558a1ec6e20887a9d1776fe
|
4
|
+
data.tar.gz: a0015ccf978b027d0adba04e331055dfddcaa069
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49d3df5c02e5c6cc71df3d82c39cd586b18482c08e1cf14b5091bf2f047a4f73156f91b4b623ddd2997fa5b7bdc8b9fa7d341fb547112ec25258c7003b1cbd92
|
7
|
+
data.tar.gz: 06e22d56046f7d5cbaac2f69c68e416f4b652865fa0dd46d3c52d9e66ffbecb7b708899054f1945785f7d6ba5121e24e2e4a51f87affa627c49319fdb8640b3c
|
data/lib/unitwise.rb
CHANGED
@@ -16,7 +16,7 @@ require 'unitwise/atom'
|
|
16
16
|
require 'unitwise/prefix'
|
17
17
|
require 'unitwise/term'
|
18
18
|
require 'unitwise/unit'
|
19
|
-
require 'unitwise/
|
19
|
+
require 'unitwise/search'
|
20
20
|
require 'unitwise/errors'
|
21
21
|
|
22
22
|
# Unitwise is a library for performing mathematical operations and conversions
|
@@ -29,7 +29,7 @@ module Unitwise
|
|
29
29
|
# @return [Array]
|
30
30
|
# @api public
|
31
31
|
def self.search(term)
|
32
|
-
|
32
|
+
Search.search(term)
|
33
33
|
end
|
34
34
|
|
35
35
|
# The system path for the installed gem
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Unitwise
|
2
|
+
module Search
|
3
|
+
class << self
|
4
|
+
# An abbreviated list of possible units. These are known combinations
|
5
|
+
# of atoms and prefixes. Since units can be combined to create more
|
6
|
+
# complex units (and thus an infinite number), a full list can't be
|
7
|
+
# provided.
|
8
|
+
# @return [Array] A list of known units
|
9
|
+
# @api public
|
10
|
+
def all
|
11
|
+
@all ||= begin
|
12
|
+
units = []
|
13
|
+
Atom.all.each do |a|
|
14
|
+
units << build(a)
|
15
|
+
Unitwise::Prefix.all.each { |p| units << build(a,p) } if a.metric?
|
16
|
+
end
|
17
|
+
units
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Search the list of known units for a match. Note that this cannot
|
22
|
+
# find all possible units, only simple combinations of atoms and prefixes.
|
23
|
+
# @param term [String, Regexp] The term to search for
|
24
|
+
# @return [Array] A list of matching units.
|
25
|
+
# @api public
|
26
|
+
def search(term)
|
27
|
+
all.select do |unit|
|
28
|
+
unit.aliases.any? { |str| Regexp.new(term).match(str) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
# Helper method for building a new unit by a known atom and prefix.
|
35
|
+
# @param atom [Unitwise::Atom]
|
36
|
+
# @param prefix [Unitwise::Prefix, nil]
|
37
|
+
# @return [Unitwise::Unit]
|
38
|
+
# @api private
|
39
|
+
def build(atom, prefix=nil)
|
40
|
+
Unit.new([Term.new(:atom => atom, :prefix => prefix)])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/unitwise/unit.rb
CHANGED
@@ -102,6 +102,13 @@ module Unitwise
|
|
102
102
|
expression(mode || self.mode)
|
103
103
|
end
|
104
104
|
|
105
|
+
def aliases
|
106
|
+
[:names, :primary_code, :secondary_code, :symbol].map do |mode|
|
107
|
+
to_s(mode)
|
108
|
+
end.uniq
|
109
|
+
end
|
110
|
+
memoize :aliases
|
111
|
+
|
105
112
|
def mode
|
106
113
|
terms
|
107
114
|
@mode || :primary_code
|
data/lib/unitwise/version.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Unitwise::Search do
|
4
|
+
describe :class_methods do
|
5
|
+
describe :all do
|
6
|
+
subject { Unitwise::Search.all }
|
7
|
+
it { subject.must_be_kind_of Enumerable }
|
8
|
+
it { subject.first.must_be_instance_of Unitwise::Unit }
|
9
|
+
end
|
10
|
+
describe :search do
|
11
|
+
it "should return a list of units" do
|
12
|
+
search = Unitwise::Search.search('foo')
|
13
|
+
search.must_be_kind_of Enumerable
|
14
|
+
search.first.must_be_instance_of Unitwise::Unit
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,167 +1,167 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unitwise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Lewis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: liner
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: signed_multiset
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0.2'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0.2'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: memoizable
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0.4'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.4'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: bigdecimal
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '1.2'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.2'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: parslet
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '1.5'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '1.5'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: nokogiri
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ~>
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '1.5'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ~>
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '1.5'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: coveralls
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - ~>
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0.6'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - ~>
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0.6'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: pry
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ~>
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0.9'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - ~>
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0.9'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: minitest
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
|
-
- -
|
129
|
+
- - ~>
|
130
130
|
- !ruby/object:Gem::Version
|
131
131
|
version: '5.0'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
|
-
- -
|
136
|
+
- - ~>
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '5.0'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: rake
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
|
-
- -
|
143
|
+
- - ~>
|
144
144
|
- !ruby/object:Gem::Version
|
145
145
|
version: '10.0'
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
|
-
- -
|
150
|
+
- - ~>
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '10.0'
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: nori
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
|
-
- -
|
157
|
+
- - ~>
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '2.3'
|
160
160
|
type: :development
|
161
161
|
prerelease: false
|
162
162
|
version_requirements: !ruby/object:Gem::Requirement
|
163
163
|
requirements:
|
164
|
-
- -
|
164
|
+
- - ~>
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '2.3'
|
167
167
|
description: Physical quantity and units of measure conversion and math library
|
@@ -171,9 +171,9 @@ executables: []
|
|
171
171
|
extensions: []
|
172
172
|
extra_rdoc_files: []
|
173
173
|
files:
|
174
|
-
-
|
175
|
-
-
|
176
|
-
-
|
174
|
+
- .gitignore
|
175
|
+
- .ruby-version
|
176
|
+
- .travis.yml
|
177
177
|
- Gemfile
|
178
178
|
- LICENSE.txt
|
179
179
|
- README.md
|
@@ -185,7 +185,6 @@ files:
|
|
185
185
|
- lib/unitwise/atom.rb
|
186
186
|
- lib/unitwise/base.rb
|
187
187
|
- lib/unitwise/compatible.rb
|
188
|
-
- lib/unitwise/compound.rb
|
189
188
|
- lib/unitwise/errors.rb
|
190
189
|
- lib/unitwise/expression.rb
|
191
190
|
- lib/unitwise/expression/composer.rb
|
@@ -199,6 +198,7 @@ files:
|
|
199
198
|
- lib/unitwise/measurement.rb
|
200
199
|
- lib/unitwise/prefix.rb
|
201
200
|
- lib/unitwise/scale.rb
|
201
|
+
- lib/unitwise/search.rb
|
202
202
|
- lib/unitwise/standard.rb
|
203
203
|
- lib/unitwise/standard/base.rb
|
204
204
|
- lib/unitwise/standard/base_unit.rb
|
@@ -215,7 +215,6 @@ files:
|
|
215
215
|
- test/test_helper.rb
|
216
216
|
- test/unitwise/atom_test.rb
|
217
217
|
- test/unitwise/base_test.rb
|
218
|
-
- test/unitwise/compound_test.rb
|
219
218
|
- test/unitwise/expression/decomposer_test.rb
|
220
219
|
- test/unitwise/expression/matcher_test.rb
|
221
220
|
- test/unitwise/expression/parser_test.rb
|
@@ -224,6 +223,7 @@ files:
|
|
224
223
|
- test/unitwise/measurement_test.rb
|
225
224
|
- test/unitwise/prefix_test.rb
|
226
225
|
- test/unitwise/scale_test.rb
|
226
|
+
- test/unitwise/search_test.rb
|
227
227
|
- test/unitwise/term_test.rb
|
228
228
|
- test/unitwise/unit_test.rb
|
229
229
|
- test/unitwise_test.rb
|
@@ -238,17 +238,17 @@ require_paths:
|
|
238
238
|
- lib
|
239
239
|
required_ruby_version: !ruby/object:Gem::Requirement
|
240
240
|
requirements:
|
241
|
-
- -
|
241
|
+
- - '>='
|
242
242
|
- !ruby/object:Gem::Version
|
243
243
|
version: '0'
|
244
244
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
245
245
|
requirements:
|
246
|
-
- -
|
246
|
+
- - '>='
|
247
247
|
- !ruby/object:Gem::Version
|
248
248
|
version: '0'
|
249
249
|
requirements: []
|
250
250
|
rubyforge_project:
|
251
|
-
rubygems_version: 2.2.
|
251
|
+
rubygems_version: 2.2.2
|
252
252
|
signing_key:
|
253
253
|
specification_version: 4
|
254
254
|
summary: Convert between and perform mathematical operations on physical quantities
|
@@ -258,7 +258,6 @@ test_files:
|
|
258
258
|
- test/test_helper.rb
|
259
259
|
- test/unitwise/atom_test.rb
|
260
260
|
- test/unitwise/base_test.rb
|
261
|
-
- test/unitwise/compound_test.rb
|
262
261
|
- test/unitwise/expression/decomposer_test.rb
|
263
262
|
- test/unitwise/expression/matcher_test.rb
|
264
263
|
- test/unitwise/expression/parser_test.rb
|
@@ -267,7 +266,7 @@ test_files:
|
|
267
266
|
- test/unitwise/measurement_test.rb
|
268
267
|
- test/unitwise/prefix_test.rb
|
269
268
|
- test/unitwise/scale_test.rb
|
269
|
+
- test/unitwise/search_test.rb
|
270
270
|
- test/unitwise/term_test.rb
|
271
271
|
- test/unitwise/unit_test.rb
|
272
272
|
- test/unitwise_test.rb
|
273
|
-
has_rdoc:
|
data/lib/unitwise/compound.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
module Unitwise
|
2
|
-
# A compound is a combination of an atom and a prefix.
|
3
|
-
# This class is isolated from the rest of the code base and primarily exists
|
4
|
-
# for the convenience of listing and searching possible combinations
|
5
|
-
# of atoms and prefixes.
|
6
|
-
class Compound < Liner.new(:atom, :prefix)
|
7
|
-
# List all possible compounds
|
8
|
-
# @return [Array]
|
9
|
-
# @api public
|
10
|
-
def self.all
|
11
|
-
@all || build
|
12
|
-
end
|
13
|
-
|
14
|
-
# Builds up the list of possible compounds. Only required if you are
|
15
|
-
# defining your own atoms/prefixes.
|
16
|
-
# @return [Array]
|
17
|
-
# @api public
|
18
|
-
def self.build
|
19
|
-
compounds = Atom.all.map { |a| new(a) }
|
20
|
-
Atom.all.select(&:metric).each do |a|
|
21
|
-
Prefix.all.each do |p|
|
22
|
-
compounds << new(a, p)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
@all = compounds
|
26
|
-
end
|
27
|
-
|
28
|
-
# Search for compounds with a search term
|
29
|
-
# @param [String, Regexp] What to search for
|
30
|
-
# @return [Array]
|
31
|
-
# @api public
|
32
|
-
def self.search(term)
|
33
|
-
all.select do |compound|
|
34
|
-
compound.search_strings.any? { |str| Regexp.new(term).match(str) }
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
[:primary_code, :secondary_code, :symbol].each do |attr|
|
39
|
-
define_method attr do
|
40
|
-
instance_variable_get("@#{attr}") ||
|
41
|
-
instance_variable_set("@#{attr}",
|
42
|
-
prefix ? "#{prefix.send attr}#{atom.send attr}" : atom.send(attr))
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
[:names, :slugs].each do |attr|
|
47
|
-
define_method attr do
|
48
|
-
instance_variable_get("@#{attr}") ||
|
49
|
-
instance_variable_set("@#{attr}",
|
50
|
-
if prefix
|
51
|
-
prefix.send(attr).zip(atom.send(attr)).map { |set| set.join('') }
|
52
|
-
else
|
53
|
-
atom.send(attr)
|
54
|
-
end)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def atom=(value)
|
59
|
-
value.is_a?(Atom) ? super(value) : super(Atom.find value)
|
60
|
-
end
|
61
|
-
|
62
|
-
def prefix=(value)
|
63
|
-
value.is_a?(Prefix) ? super(value) : super(Prefix.find value)
|
64
|
-
end
|
65
|
-
|
66
|
-
def search_strings
|
67
|
-
@search_strings ||= [primary_code, secondary_code, symbol,
|
68
|
-
names, slugs].flatten.uniq
|
69
|
-
end
|
70
|
-
|
71
|
-
def attribute_string
|
72
|
-
[:atom, :prefix, :primary_code, :secondary_code,
|
73
|
-
:symbol, :names, :slugs].map do |attr|
|
74
|
-
"#{attr}='#{send attr}'"
|
75
|
-
end.join(', ')
|
76
|
-
end
|
77
|
-
|
78
|
-
def to_s(mode = :primary_code)
|
79
|
-
res = send(mode)
|
80
|
-
res.respond_to?(:each) ? res.first : res
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Unitwise::Compound do
|
4
|
-
describe :class_methods do
|
5
|
-
describe :all do
|
6
|
-
subject { Unitwise::Compound.all }
|
7
|
-
it { subject.must_be_kind_of Enumerable }
|
8
|
-
it { subject.first.must_be_instance_of Unitwise::Compound }
|
9
|
-
end
|
10
|
-
describe :search do
|
11
|
-
it "should return a list of units" do
|
12
|
-
search = Unitwise::Compound.search('foo')
|
13
|
-
search.must_be_kind_of Enumerable
|
14
|
-
search.first.must_be_instance_of Unitwise::Compound
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
describe :instance_methods do
|
20
|
-
let(:prefixed) { Unitwise::Compound.new('m','k') }
|
21
|
-
let(:unprefixed) { Unitwise::Compound.new("[in_i]") }
|
22
|
-
it "should have an atom" do
|
23
|
-
[prefixed, unprefixed].each { |ex| ex.atom.must_be_instance_of Unitwise::Atom }
|
24
|
-
end
|
25
|
-
it "should have a prefix when appropriate" do
|
26
|
-
prefixed.prefix.must_be_instance_of Unitwise::Prefix
|
27
|
-
unprefixed.prefix.must_equal nil
|
28
|
-
end
|
29
|
-
it "should concatenate common methods" do
|
30
|
-
prefixed.primary_code.must_equal 'km'
|
31
|
-
unprefixed.secondary_code.must_equal '[IN_I]'
|
32
|
-
prefixed.names.must_equal ['kilometer']
|
33
|
-
unprefixed.slugs.must_equal ['inch']
|
34
|
-
end
|
35
|
-
it "should have an array of strings for searching" do
|
36
|
-
prefixed.search_strings.must_be_kind_of(Array)
|
37
|
-
end
|
38
|
-
it "should look ok for inspection" do
|
39
|
-
prefixed.inspect.must_include 'km'
|
40
|
-
unprefixed.inspect.must_include '[IN_I]'
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|