ruby-units 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,9 @@
1
1
  Change Log for Ruby-units
2
2
  =========================
3
+ 2012-05-13 1.4.1 * Fix issue #40 -- Unit parsing truncates invalid portions of the unit
4
+ * Fix issue #41 -- initializing with a nil gives unexpected result
5
+ 2012-02-01 * Fix issue #34 -- Time.at takes more than one parameter
6
+ * Fix issue #35 -- 'pt' is ambiguous
3
7
  2012-01-02 1.4.0 * Fix some definitions that were just wrong (amu, dalton)
4
8
  * Definition uses name of unit if no aliases provided
5
9
  * Refactor definition process. New units are immediately available
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Ruby Units
2
2
 
3
+ [![Dependency Status](https://gemnasium.com/olbrich/ruby-units.png)](https://gemnasium.com/olbrich/ruby-units)
4
+
3
5
  Kevin C. Olbrich, Ph.D.
4
6
 
5
7
  [Sciwerks.com](http://www.sciwerks.com)
@@ -146,7 +148,7 @@ Conversions are probably better done like this...
146
148
 
147
149
  Unit('0 tempC') + Unit('100 degC') #=> Unit('100 tempC')
148
150
 
149
- ## Defininig Units
151
+ ## Defining Units
150
152
 
151
153
  It is possible to define new units or redefine existing ones.
152
154
 
data/RakeFile CHANGED
@@ -41,16 +41,6 @@ rescue LoadError
41
41
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
42
42
  end
43
43
 
44
- begin
45
- require 'rcov/rcovtask'
46
- desc "Generate code coverage"
47
- Rcov::RcovTask.new do |t|
48
- t.test_files = FileList['test/test*.rb']
49
- #t.verbose = true # uncomment to see the executed command
50
- end
51
- rescue LoadError
52
- end
53
-
54
44
  begin
55
45
  require 'simplecov'
56
46
  desc "code coverage report using simplecov (ruby 1.9+)"
@@ -79,41 +69,23 @@ begin
79
69
  rescue LoadError
80
70
  end
81
71
 
82
-
83
- desc "Run unit tests"
84
- Rake::TestTask.new do |t|
85
- t.libs << "test"
86
- t.test_files = FileList['test/test*.rb']
87
- end
88
-
89
72
  task :specs => :spec
90
73
 
91
- desc "Run tests against several ruby versions, requires rvm"
92
- task :multitest do
93
- rubies = %w{
94
- ruby-1.8.7-p352@ruby-units
95
- ruby-1.8.7-p352@ruby-units-with-chronic
96
- ruby-1.9.2-p290@ruby-units
97
- ruby-1.9.2-p290@ruby-units-with-chronic
98
- rbx-head@ruby-units
99
- jruby-1.6.5@ruby-units
100
- }
101
- exec "rvm #{rubies.join(',')} do rake tests"
74
+ rubies = {
75
+ "ruby-1.8.7" => %w{ruby-1.8.7-p357@ruby-units ruby-1.8.7-p357@ruby-units-with-chronic},
76
+ "ruby-1.9.2" => %w{ruby-1.9.2-p290@ruby-units ruby-1.9.2-p290@ruby-units-with-chronic},
77
+ "rbx" => %w{rbx-head@ruby-units},
78
+ "jruby" => %w{jruby-1.6.7@ruby-units}
79
+ }
80
+
81
+ rubies.each do |name, ruby|
82
+ desc "Run Spec against #{name}"
83
+ task "spec:#{name}" do
84
+ sh "rvm #{ruby.join(',')} do rake spec"
85
+ end
102
86
  end
103
87
 
104
88
  desc "Run specs against several ruby versions, requires rvm"
105
- task :multispec do
106
- rubies = %w{
107
- ruby-1.8.7-p352@ruby-units
108
- ruby-1.8.7-p352@ruby-units-with-chronic
109
- ruby-1.9.2-p290@ruby-units
110
- ruby-1.9.2-p290@ruby-units-with-chronic
111
- rbx-head@ruby-units
112
- jruby-1.6.5@ruby-units
113
- }
114
- exec "rvm #{rubies.join(',')} do rake spec"
115
- end
116
-
117
- task :default => :test
89
+ task "spec:all" => rubies.keys.map {|name| "spec:#{name}"}
118
90
 
119
- task :tests => :test
91
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.0
1
+ 1.4.1
@@ -11,13 +11,15 @@ class Time
11
11
 
12
12
  # Convert a duration to a Time value by considering the duration to be the number of seconds since the
13
13
  # epoch
14
+ # @param [Time] arg
15
+ # @param [Integer] ms
14
16
  # @return [Unit, Time]
15
- def self.at(arg)
17
+ def self.at(arg,ms=0)
16
18
  case arg
17
19
  when Unit
18
- unit_time_at(arg.convert_to("s").scalar)
20
+ unit_time_at(arg.convert_to("s").scalar, ms)
19
21
  else
20
- unit_time_at(arg)
22
+ unit_time_at(arg, ms)
21
23
  end
22
24
  end
23
25
 
@@ -314,6 +314,7 @@ class Unit < Numeric
314
314
  @unit_name = nil
315
315
  @signature = nil
316
316
  @output = {}
317
+ raise ArgumentError, "Invalid Unit Format" if options[0].nil?
317
318
  if options.size == 2
318
319
  # options[0] is the scalar
319
320
  # options[1] is a unit string
@@ -427,10 +428,6 @@ class Unit < Numeric
427
428
  # @return [Boolean]
428
429
  def is_base?
429
430
  return @is_base if defined? @is_base
430
- # return @is_base = true if self.degree? &&
431
- # self.numerator.size == 1 &&
432
- # self.denominator == UNITY_ARRAY &&
433
- # self.units =~ /(?:deg|temp)K/
434
431
  @is_base = (@numerator + @denominator).compact.uniq.
435
432
  map {|unit| Unit.definition(unit)}.
436
433
  all? {|element| element.unity? || element.base? }
@@ -1436,6 +1433,7 @@ class Unit < Numeric
1436
1433
  return
1437
1434
  end
1438
1435
 
1436
+ # more than one per. I.e., "1 m/s/s"
1439
1437
  raise( ArgumentError, "'#{passed_unit_string}' Unit not recognized") if unit_string.count('/') > 1
1440
1438
  raise( ArgumentError, "'#{passed_unit_string}' Unit not recognized") if unit_string.scan(/\s[02-9]/).size > 0
1441
1439
  @scalar, top, bottom = unit_string.scan(UNIT_STRING_REGEX)[0] #parse the string into parts
@@ -1459,8 +1457,10 @@ class Unit < Numeric
1459
1457
  @numerator = top.scan(Unit.unit_match_regex).delete_if {|x| x.empty?}.compact if top
1460
1458
  @denominator = bottom.scan(Unit.unit_match_regex).delete_if {|x| x.empty?}.compact if bottom
1461
1459
 
1462
- us = "#{(top || '' + bottom || '')}".to_s.gsub(Unit.unit_match_regex,'').gsub(/[\d\*, "'_^\/\$]/,'')
1463
- raise( ArgumentError, "'#{passed_unit_string}' Unit not recognized") unless us.empty?
1460
+ # eliminate all known terms from this string. This is a quick check to see if the passed unit
1461
+ # contains terms that are not defined.
1462
+ used = "#{top} #{bottom}".to_s.gsub(Unit.unit_match_regex,'').gsub(/[\d\*, "'_^\/\$]/,'')
1463
+ raise( ArgumentError, "'#{passed_unit_string}' Unit not recognized") unless used.empty?
1464
1464
 
1465
1465
  @numerator = @numerator.map do |item|
1466
1466
  @@PREFIX_MAP[item[0]] ? [@@PREFIX_MAP[item[0]], @@UNIT_MAP[item[1]]] : [@@UNIT_MAP[item[1]]]
@@ -71,11 +71,11 @@ end
71
71
 
72
72
  Unit.define('point') do |point|
73
73
  point.definition = Unit('1/72 ft')
74
- point.aliases = %w{pt point points}
74
+ point.aliases = %w{point points}
75
75
  end
76
76
 
77
77
  Unit.define('pica') do |pica|
78
- pica.definition = Unit('12 pt')
78
+ pica.definition = Unit('12 point')
79
79
  pica.aliases = %w{P pica picas}
80
80
  end
81
81
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ruby-units"
8
- s.version = "1.4.0"
8
+ s.version = "1.4.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kevin Olbrich, Ph.D."]
12
- s.date = "2012-01-02"
12
+ s.date = "2012-05-13"
13
13
  s.description = "Provides classes and methods to perform unit math and conversions"
14
14
  s.email = ["kevin.olbrich+ruby_units@gmail.com"]
15
15
  s.extra_rdoc_files = [
@@ -35,7 +35,6 @@ Gem::Specification.new do |s|
35
35
  "lib/ruby_units/numeric.rb",
36
36
  "lib/ruby_units/object.rb",
37
37
  "lib/ruby_units/string.rb",
38
- "lib/ruby_units/string/extra.rb",
39
38
  "lib/ruby_units/time.rb",
40
39
  "lib/ruby_units/unit.rb",
41
40
  "lib/ruby_units/unit_definitions.rb",
@@ -49,7 +48,7 @@ Gem::Specification.new do |s|
49
48
  s.licenses = ["MIT"]
50
49
  s.post_install_message = "====================\nDeprecation Warning\n====================\n\nSeveral convenience methods that ruby-units added to the string class have\nbeen deprecated in this release. These methods include String#to, String#from, String#ago, String#before and others.\nIf your code relies on these functions, they can be added back by adding this line to your code.\n\nrequire 'ruby-units/string/extras'\n# note that these methods do not play well with Rails, which is one of the reasons they are being removed.\n\nThe extra functions mostly work the same, but will no longer properly handle cases when they are called with strings..\n\n'min'.from(\"4-1-2011\") # => Exception\n\nPass in a Date, Time, or DateTime object to get the expected result.\n\nThey will go away completely in the next release, so it would be a good idea to refactor your code\nto avoid using them. They will also throw deprecation warnings when they are used.\n"
51
50
  s.require_paths = ["lib"]
52
- s.rubygems_version = "1.8.10"
51
+ s.rubygems_version = "1.8.24"
53
52
  s.summary = "A class that performs unit conversions and unit math"
54
53
 
55
54
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-units
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.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: 2012-01-02 00:00:00.000000000Z
12
+ date: 2012-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &2157320760 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '1.0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2157320760
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rcov
27
- requirement: &2157320240 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *2157320240
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: simplecov
38
- requirement: &2157319660 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *2157319660
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: simplecov-html
49
- requirement: &2157319100 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *2157319100
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: jeweler
60
- requirement: &2157318340 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ! '>='
@@ -65,10 +85,15 @@ dependencies:
65
85
  version: '0'
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *2157318340
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: rspec
71
- requirement: &2157317580 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
99
  - - ~>
@@ -76,10 +101,15 @@ dependencies:
76
101
  version: '2.5'
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *2157317580
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '2.5'
80
110
  - !ruby/object:Gem::Dependency
81
111
  name: autotest
82
- requirement: &2157316680 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
83
113
  none: false
84
114
  requirements:
85
115
  - - ! '>='
@@ -87,10 +117,15 @@ dependencies:
87
117
  version: '0'
88
118
  type: :development
89
119
  prerelease: false
90
- version_requirements: *2157316680
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
91
126
  - !ruby/object:Gem::Dependency
92
127
  name: autotest-growl
93
- requirement: &2157316020 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
94
129
  none: false
95
130
  requirements:
96
131
  - - ! '>='
@@ -98,10 +133,15 @@ dependencies:
98
133
  version: '0'
99
134
  type: :development
100
135
  prerelease: false
101
- version_requirements: *2157316020
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
102
142
  - !ruby/object:Gem::Dependency
103
143
  name: autotest-fsevent
104
- requirement: &2157315280 !ruby/object:Gem::Requirement
144
+ requirement: !ruby/object:Gem::Requirement
105
145
  none: false
106
146
  requirements:
107
147
  - - ! '>='
@@ -109,7 +149,12 @@ dependencies:
109
149
  version: '0'
110
150
  type: :development
111
151
  prerelease: false
112
- version_requirements: *2157315280
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
113
158
  description: Provides classes and methods to perform unit math and conversions
114
159
  email:
115
160
  - kevin.olbrich+ruby_units@gmail.com
@@ -137,7 +182,6 @@ files:
137
182
  - lib/ruby_units/numeric.rb
138
183
  - lib/ruby_units/object.rb
139
184
  - lib/ruby_units/string.rb
140
- - lib/ruby_units/string/extra.rb
141
185
  - lib/ruby_units/time.rb
142
186
  - lib/ruby_units/unit.rb
143
187
  - lib/ruby_units/unit_definitions.rb
@@ -204,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
204
248
  version: '0'
205
249
  requirements: []
206
250
  rubyforge_project:
207
- rubygems_version: 1.8.10
251
+ rubygems_version: 1.8.24
208
252
  signing_key:
209
253
  specification_version: 3
210
254
  summary: A class that performs unit conversions and unit math
@@ -1,166 +0,0 @@
1
- require 'time'
2
-
3
- #
4
- # String Extras
5
- #
6
- # These are some extra string syntax sugar methods. In some cases they conflict with methods defined in other
7
- # gems (notably with Rails). I could make them more compatilble, but honestly they should not be here in the first place.
8
- #
9
- # in most cases it is better to do something like
10
- #
11
- # @example
12
- # Unit("1 m").convert_to("ft")
13
- # instead of
14
- # "1 m".convert_to("ft")
15
- #
16
- # to use these methods:
17
- # require 'ruby-units/string/extra'
18
- class String
19
-
20
- #needed for compatibility with Rails, which defines a String.from method
21
- if self.public_instance_methods.include? 'from'
22
- alias :old_from :from
23
- end
24
-
25
- # @example
26
- # "5 min".from("now")
27
- # @param [Time] time_point
28
- # @return (see Unit#from)
29
- # @deprecated
30
- def from(time_point = ::Time.now)
31
- return old_from(time_point) if self.respond_to?(:old_from) && time_point.instance_of?(Integer)
32
- warn Kernel.caller.first + " called ruby-units/string#from, which is deprecated. Use Unit(string).from(string) instead"
33
- self.unit.from(time_point)
34
- end
35
-
36
- alias :after :from
37
-
38
- # @example
39
- # "5 min".from_now
40
- # @return (see Unit#from)
41
- # @deprecated
42
- def from_now
43
- warn Kernel.caller.first + " called ruby-units/string#from_now, which is deprecated. Use Unit(string).from(Time.now) instead"
44
- self.from(Time.now)
45
- end
46
-
47
- # @example
48
- # "5 min".ago
49
- # @return (see Unit#ago)
50
- # @deprecated
51
- def ago
52
- warn Kernel.caller.first + " called ruby-units/string#ago, which is deprecated. Use Unit(string).ago instead"
53
- self.unit.ago
54
- end
55
-
56
- # @example
57
- # "5 min".before("now")
58
- # @param [Time] time_point
59
- # @return (see Unit#before)
60
- # @deprecated
61
- def before(time_point = ::Time.now)
62
- warn Kernel.caller.first + " called ruby-units/string#before, which is deprecated. Use Unit(string).before(Time.now) instead"
63
- self.unit.before(time_point)
64
- end
65
-
66
- # @example
67
- # "5 min".before_now
68
- # @return (see Unit#before)
69
- # @deprecated
70
- def before_now
71
- warn Kernel.caller.first + " called ruby-units/string#before_now, which is deprecated. Use Unit(string).before(Time.now) instead"
72
- self.before(Time.now)
73
- end
74
-
75
- # @return (see Unit#since)
76
- # @deprecated
77
- def since(time_point = ::Time.now)
78
- warn Kernel.caller.first + " called ruby-units/string#since, which is deprecated. Use Unit(string).since(Time.now) instead"
79
- self.unit.since(time_point)
80
- end
81
-
82
- # @return (see Unit#until)
83
- # @deprecated
84
- def until(time_point = ::Time.now)
85
- warn Kernel.caller.first + " called ruby-units/string#until, which is deprecated. Use Unit(string).until(Time.now) instead"
86
- self.unit.until(time_point)
87
- end
88
-
89
- unless String.instance_methods.include?(:to)
90
- # @deprecated
91
- # @return (see #convert_to)
92
- def to(other)
93
- warn "string.to is deprecated, use string.convert_to if you must, but the best would be Unit('unit').convert_to('target unit')"
94
- convert_to(other)
95
- end
96
- end
97
-
98
- # @deprecated
99
- # @return (see #to_time)
100
- def time(options = {})
101
- self.to_time(options) rescue self.to_datetime(options)
102
- end
103
-
104
- # @deprecated
105
- # @return [Time]
106
- def to_time(options = {})
107
- begin
108
- #raises exception when Chronic not defined or when it returns a nil (i.e., can't parse the input)
109
- r = Chronic.parse(self,options)
110
- raise(ArgumentError, 'Invalid Time String') unless r
111
- return r
112
- rescue
113
- case
114
- when self == "now"
115
- Time.now
116
- when Time.respond_to?(:parse)
117
- Time.parse(self)
118
- else
119
- Time.local(*ParseDate.parsedate(self))
120
- end
121
- end
122
- end
123
-
124
- # @deprecated
125
- # @return [DateTime]
126
- def to_datetime(options = {})
127
- begin
128
- # raises an exception if Chronic.parse = nil or if Chronic not defined
129
- r = Chronic.parse(self,options).send(:to_datetime)
130
- rescue NameError => e
131
- r = case
132
- when self.to_s == "now"
133
- DateTime.now
134
- else
135
- DateTime.parse(self)
136
- end
137
- end
138
- raise RuntimeError, "Invalid Time String (#{self.to_s})" if r == DateTime.new
139
- return r
140
- end
141
-
142
- # @deprecated
143
- # @return [Date]
144
- def to_date(options={})
145
- begin
146
- r = Chronic.parse(self,options).to_date
147
- rescue
148
- r = case
149
- when self == "today"
150
- Date.today
151
- when RUBY_VERSION < "1.9"
152
- Date.civil(*ParseDate.parsedate(self)[0..5].compact)
153
- else
154
- Date.parse(self)
155
- end
156
- end
157
- raise RuntimeError, 'Invalid Date String' if r == Date.new
158
- return r
159
- end
160
-
161
- # @deprecated
162
- # @return (see #to_datetime)
163
- def datetime(options = {})
164
- self.to_datetime(options) rescue self.to_time(options)
165
- end
166
- end