ruby-measurement 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +6 -0
- data/README.md +6 -0
- data/lib/ruby-measurement/measurement.rb +31 -18
- data/lib/ruby-measurement/unit.rb +4 -0
- data/lib/ruby-measurement/version.rb +1 -1
- data/spec/ruby-measurement/unit_spec.rb +43 -0
- metadata +23 -7
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -162,6 +162,12 @@ that will be used when displaying a measurement.
|
|
162
162
|
Measurement.parse('3 year') # => 3.0 yr
|
163
163
|
Measurement.parse('3 years') # => 3.0 yr
|
164
164
|
|
165
|
+
### List all units names defined
|
166
|
+
|
167
|
+
List all keys you can use as unit
|
168
|
+
|
169
|
+
Measurement.names # => ['count','doz','dozen',...]
|
170
|
+
|
165
171
|
## Contributing
|
166
172
|
|
167
173
|
1. Fork it
|
@@ -82,24 +82,11 @@ class Measurement
|
|
82
82
|
def self.parse(str = '0')
|
83
83
|
str = str.strip
|
84
84
|
|
85
|
-
|
86
|
-
|
87
|
-
quantity =
|
88
|
-
|
89
|
-
|
90
|
-
quantity = whole.to_f
|
91
|
-
elsif str =~ RATIONAL_REGEX
|
92
|
-
whole, _, numerator, denominator, unit_name = str.scan(RATIONAL_REGEX).first
|
93
|
-
|
94
|
-
if numerator && denominator
|
95
|
-
numerator = numerator.to_f + (denominator.to_f * whole.to_f)
|
96
|
-
denominator = denominator.to_f
|
97
|
-
quantity = Rational(numerator, denominator).to_f
|
98
|
-
else
|
99
|
-
quantity = whole.to_f
|
100
|
-
end
|
101
|
-
else
|
102
|
-
raise ArgumentError, "Unable to parse: '#{str}'"
|
85
|
+
case str
|
86
|
+
when COMPLEX_REGEX then unit_name, quantity = parse_complex(str)
|
87
|
+
when SCIENTIFIC_REGEX then unit_name, quantity = parse_scientific(str)
|
88
|
+
when RATIONAL_REGEX then unit_name, quantity = parse_rational(str)
|
89
|
+
else raise ArgumentError, "Unable to parse: '#{str}'"
|
103
90
|
end
|
104
91
|
|
105
92
|
unit_name ||= 'count'
|
@@ -115,6 +102,32 @@ class Measurement
|
|
115
102
|
|
116
103
|
private
|
117
104
|
|
105
|
+
def self.parse_complex(str)
|
106
|
+
real, imaginary, unit_name = str.scan(COMPLEX_REGEX).first
|
107
|
+
quantity = Complex(real.to_f, imaginary.to_f).to_f
|
108
|
+
return unit_name, quantity
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.parse_scientific(str)
|
112
|
+
whole, unit_name = str.scan(SCIENTIFIC_REGEX).first
|
113
|
+
quantity = whole.to_f
|
114
|
+
return unit_name, quantity
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.parse_rational(str)
|
118
|
+
whole, _, numerator, denominator, unit_name = str.scan(RATIONAL_REGEX).first
|
119
|
+
|
120
|
+
if numerator && denominator
|
121
|
+
numerator = numerator.to_f + (denominator.to_f * whole.to_f)
|
122
|
+
denominator = denominator.to_f
|
123
|
+
quantity = Rational(numerator, denominator).to_f
|
124
|
+
else
|
125
|
+
quantity = whole.to_f
|
126
|
+
end
|
127
|
+
|
128
|
+
return unit_name, quantity
|
129
|
+
end
|
130
|
+
|
118
131
|
define(:count) do |unit|
|
119
132
|
unit.convert_to(:dozen) { |value| value / 12.0 }
|
120
133
|
end
|
@@ -117,4 +117,47 @@ describe Measurement::Unit do
|
|
117
117
|
end
|
118
118
|
end
|
119
119
|
end
|
120
|
+
|
121
|
+
describe '.names' do
|
122
|
+
subject { Measurement::Unit }
|
123
|
+
|
124
|
+
it 'returns all defined unit names' do
|
125
|
+
unit_names = subject.names
|
126
|
+
unit_names.should be_kind_of Array
|
127
|
+
unit_names.should include 'count'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe '.define' do
|
132
|
+
subject { Measurement::Unit }
|
133
|
+
|
134
|
+
it 'returns a unit builder' do
|
135
|
+
builder = subject.define(:count)
|
136
|
+
builder.should be_a Measurement::Unit::Builder
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'accepts a block' do
|
140
|
+
builder = subject.define(:count) { |b| b.alias :ct }
|
141
|
+
builder.should be_a Measurement::Unit::Builder
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe '.[]' do
|
146
|
+
subject { Measurement::Unit }
|
147
|
+
|
148
|
+
describe 'for a unit name that is defined' do
|
149
|
+
it 'returns the unit' do
|
150
|
+
unit = subject[:dozen]
|
151
|
+
unit.should be_a Measurement::Unit
|
152
|
+
unit.name.should eq 'doz'
|
153
|
+
unit.aliases.should eq %w(doz dozen).to_set
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe 'for a unit name that is not defined' do
|
158
|
+
it 'returns nil' do
|
159
|
+
subject[:potato].should be_nil
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
120
163
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-measurement
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rspec
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,7 +37,12 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
description: Simple gem for calculating and converting measurements
|
37
47
|
email: matt.huggins@gmail.com
|
38
48
|
executables: []
|
@@ -99,15 +109,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
109
|
- - ! '>='
|
100
110
|
- !ruby/object:Gem::Version
|
101
111
|
version: '0'
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
hash: -4225981439654296545
|
102
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
116
|
none: false
|
104
117
|
requirements:
|
105
118
|
- - ! '>='
|
106
119
|
- !ruby/object:Gem::Version
|
107
120
|
version: '0'
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
hash: -4225981439654296545
|
108
124
|
requirements: []
|
109
125
|
rubyforge_project:
|
110
|
-
rubygems_version: 1.8.
|
126
|
+
rubygems_version: 1.8.25
|
111
127
|
signing_key:
|
112
128
|
specification_version: 3
|
113
129
|
summary: Simple gem for calculating and converting measurements
|