ruby-units 1.3.1 → 1.3.2.a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.txt CHANGED
@@ -1,5 +1,13 @@
1
1
  Change Log for Ruby-units
2
2
  =========================
3
+ 2011-10-17 1.3.2 * deprecate some string helper functions
4
+ * tighten up some time helper functions so they don't make as many assumptions
5
+ * time helpers no longer attempt to convert strings to time/date objects
6
+ 011-10-09 * Farads are not a base unit
7
+ * CFM added to default units
8
+ * multi specs run against ruby-1.9.3
9
+ * internally change Unit#to to Unit#convert_to, which is the preferred form
10
+
3
11
  2011-04-23 1.3.0.a * Some internal restructuring
4
12
  * Implement specs for core behaviors
5
13
  * fixed several bugs found by specs
data/Gemfile CHANGED
@@ -5,4 +5,8 @@ group :development do
5
5
  gem 'rcov', :platforms => :mri_18
6
6
  gem 'jeweler'
7
7
  gem 'rspec', '~>2.5'
8
+ gem 'autotest', :platforms => [:mri_18, :mri_19]
9
+ gem 'autotest-growl', :platforms => [:mri_18, :mri_19]
10
+ gem 'autotest-fsevent', :platforms => [:mri_18, :mri_19]
8
11
  end
12
+
data/README.md CHANGED
@@ -73,7 +73,7 @@ Units can be converted to other units in a couple of ways.
73
73
  unit1 === unit2 # true if units and quantity are the same, even if 'equivalent' by <=>
74
74
  unit.to('ft') # convert
75
75
  unit1 + unit2 >> "ft" # converts result of math to 'ft'
76
- (unit1 + unit2).to('ft') # converts result to 'ft'
76
+ (unit1 + unit2).convert_to('ft') # converts result to 'ft'
77
77
 
78
78
  Any object that defines a 'to_unit' method will be automatically coerced to a unit during calculations.
79
79
 
@@ -81,7 +81,7 @@ Any object that defines a 'to_unit' method will be automatically coerced to a un
81
81
  Units will display themselves nicely based on the preferred abbreviation for the units and prefixes.
82
82
  Since Unit implements a Unit#to_s, all that is needed in most cases is:
83
83
 
84
- "#{Unit.new('1 mm')}" #=> "1 mm"
84
+ "#{Unit('1 mm')}" #=> "1 mm"
85
85
 
86
86
  The to_s also accepts some options.
87
87
 
@@ -101,7 +101,7 @@ Several helpers have also been defined.
101
101
  Note: If you include the 'Chronic' gem, you can specify times in natural
102
102
  language.
103
103
 
104
- 'min'.since('9/18/06 3:00pm')
104
+ Unit('min').since(DateTime.parse('9/18/06 3:00pm'))
105
105
  'min'.before('9/18/08 3:00pm')
106
106
  'days'.until('1/1/07')
107
107
  '5 min'.from(Time.now)
data/RakeFile CHANGED
@@ -13,6 +13,27 @@ begin
13
13
  gem.email = ["kevin.olbrich+ruby_units@gmail.com"]
14
14
  gem.homepage = "https://github.com/olbrich/ruby-units"
15
15
  gem.files.exclude(".*")
16
+ gem.post_install_message = <<-EOS
17
+ ====================
18
+ Deprecation Warning
19
+ ====================
20
+
21
+ Several convenience methods that ruby-units added to the string class have
22
+ been deprecated in this release. These methods include String#to, String#from, String#ago, String#before and others.
23
+ If your code relies on these functions, they can be added back by adding this line to your code.
24
+
25
+ require 'ruby-units/string/extras'
26
+ # note that these methods do not play well with Rails, which is one of the reasons they are being removed.
27
+
28
+ The extra functions mostly work the same, but will no longer properly handle cases when they are called with strings..
29
+
30
+ 'min'.from("4-1-2011") # => Exception
31
+
32
+ Pass in a Date, Time, or DateTime object to get the expected result.
33
+
34
+ They will probably go away completely in an upcoming release, so it would be a good idea to refactor your code
35
+ to avoid using them. They will also throw deprecation warnings when they are used.
36
+ EOS
16
37
  end
17
38
  Jeweler::GemcutterTasks.new
18
39
  rescue LoadError
@@ -33,7 +54,11 @@ begin
33
54
  require 'rspec/core/rake_task'
34
55
 
35
56
  desc "Run specs"
36
- RSpec::Core::RakeTask.new
57
+ RSpec::Core::RakeTask.new do |spec|
58
+ puts
59
+ puts %x{rvm current}
60
+ puts
61
+ end
37
62
 
38
63
  desc "Run all specs with rcov"
39
64
  RSpec::Core::RakeTask.new("spec:rcov") do |t|
@@ -57,10 +82,11 @@ task :multitest do
57
82
  rubies = %w{
58
83
  ruby-1.8.7@ruby-units
59
84
  ruby-1.8.7@ruby-units-with-chronic
60
- ruby-1.9.2-head@ruby-units
61
- ruby-1.9.2-head@ruby-units-with-chronic
62
- rbx-head@ruby-units
63
- jruby-head@ruby-units
85
+ ruby-1.9.2@ruby-units
86
+ ruby-1.9.2@ruby-units-with-chronic
87
+ ruby-1.9.3@ruby-units
88
+ rbx@ruby-units
89
+ jruby-1.6.4@ruby-units
64
90
  }
65
91
  exec "rvm #{rubies.join(',')} tests"
66
92
  end
@@ -70,10 +96,11 @@ task :multispec do
70
96
  rubies = %w{
71
97
  ruby-1.8.7@ruby-units
72
98
  ruby-1.8.7@ruby-units-with-chronic
73
- ruby-1.9.2-head@ruby-units
74
- ruby-1.9.2-head@ruby-units-with-chronic
75
- rbx-head@ruby-units
76
- jruby-head@ruby-units
99
+ ruby-1.9.2@ruby-units
100
+ ruby-1.9.2@ruby-units-with-chronic
101
+ ruby-1.9.3@ruby-units
102
+ rbx@ruby-units
103
+ jruby-1.6.4@ruby-units
77
104
  }
78
105
  exec "rvm #{rubies.join(',')} specs"
79
106
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.1
1
+ 1.3.2.a
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -2,7 +2,7 @@
2
2
  # [1, 'mm'].unit => 1 mm
3
3
  class Array
4
4
  def to_unit(other = nil)
5
- other ? Unit.new(self).to(other) : Unit.new(self)
5
+ other ? Unit.new(self).convert_to(other) : Unit.new(self)
6
6
  end
7
7
  alias :unit :to_unit
8
8
  alias :u :to_unit
@@ -8,8 +8,8 @@ class Date
8
8
  def +(unit)
9
9
  case unit
10
10
  when Unit
11
- unit = unit.to('d').round if ['y', 'decade', 'century'].include? unit.units
12
- unit_date_add(unit.to('day').scalar)
11
+ unit = unit.convert_to('d').round if ['y', 'decade', 'century'].include? unit.units
12
+ unit_date_add(unit.convert_to('day').scalar)
13
13
  else
14
14
  unit_date_add(unit)
15
15
  end
@@ -20,15 +20,15 @@ class Date
20
20
  def -(unit)
21
21
  case unit
22
22
  when Unit
23
- unit = unit.to('d').round if ['y', 'decade', 'century'].include? unit.units
24
- unit_date_sub(unit.to('day').scalar)
23
+ unit = unit.convert_to('d').round if ['y', 'decade', 'century'].include? unit.units
24
+ unit_date_sub(unit.convert_to('day').scalar)
25
25
  else
26
26
  unit_date_sub(unit)
27
27
  end
28
28
  end
29
29
 
30
30
  def to_unit(other = nil)
31
- other ? Unit.new(self).to(other) : Unit.new(self)
31
+ other ? Unit.new(self).convert_to(other) : Unit.new(self)
32
32
  end
33
33
  alias :unit :to_unit
34
34
 
@@ -32,42 +32,42 @@ module Math
32
32
 
33
33
  alias :unit_sin :sin
34
34
  def sin(n)
35
- Unit === n ? unit_sin(n.to('radian').scalar) : unit_sin(n)
35
+ Unit === n ? unit_sin(n.convert_to('radian').scalar) : unit_sin(n)
36
36
  end
37
37
  module_function :unit_sin
38
38
  module_function :sin
39
39
 
40
40
  alias :unit_cos :cos
41
41
  def cos(n)
42
- Unit === n ? unit_cos(n.to('radian').scalar) : unit_cos(n)
42
+ Unit === n ? unit_cos(n.convert_to('radian').scalar) : unit_cos(n)
43
43
  end
44
44
  module_function :unit_cos
45
45
  module_function :cos
46
46
 
47
47
  alias :unit_sinh :sinh
48
48
  def sinh(n)
49
- Unit === n ? unit_sinh(n.to('radian').scalar) : unit_sinh(n)
49
+ Unit === n ? unit_sinh(n.convert_to('radian').scalar) : unit_sinh(n)
50
50
  end
51
51
  module_function :unit_sinh
52
52
  module_function :sinh
53
53
 
54
54
  alias :unit_cosh :cosh
55
55
  def cosh(n)
56
- Unit === n ? unit_cosh(n.to('radian').scalar) : unit_cosh(n)
56
+ Unit === n ? unit_cosh(n.convert_to('radian').scalar) : unit_cosh(n)
57
57
  end
58
58
  module_function :unit_cosh
59
59
  module_function :cosh
60
60
 
61
61
  alias :unit_tan :tan
62
62
  def tan(n)
63
- Unit === n ? unit_tan(n.to('radian').scalar) : unit_tan(n)
63
+ Unit === n ? unit_tan(n.convert_to('radian').scalar) : unit_tan(n)
64
64
  end
65
65
  module_function :tan
66
66
  module_function :unit_tan
67
67
 
68
68
  alias :unit_tanh :tanh
69
69
  def tanh(n)
70
- Unit === n ? unit_tanh(n.to('radian').scalar) : unit_tanh(n)
70
+ Unit === n ? unit_tanh(n.convert_to('radian').scalar) : unit_tanh(n)
71
71
  end
72
72
  module_function :unit_tanh
73
73
  module_function :tanh
@@ -2,7 +2,7 @@ require 'time'
2
2
  # make a string into a unit
3
3
  class String
4
4
  def to_unit(other = nil)
5
- other ? Unit.new(self).to(other) : Unit.new(self)
5
+ other ? Unit.new(self).convert_to(other) : Unit.new(self)
6
6
  end
7
7
  alias :unit :to_unit
8
8
  alias :u :to_unit
@@ -24,103 +24,111 @@ class String
24
24
  end
25
25
 
26
26
  #needed for compatibility with Rails, which defines a String.from method
27
- if self.public_instance_methods.include? 'from'
28
- alias :old_from :from
29
- end
27
+ # if self.public_instance_methods.include? 'from'
28
+ # alias :old_from :from
29
+ # end
30
30
 
31
- # "5 min".from("now")
32
- def from(time_point = ::Time.now)
33
- return old_from(time_point) if self.respond_to?(:old_from) && time_point.instance_of?(Integer)
34
- self.unit.from(time_point)
35
- end
36
-
37
- alias :after :from
38
-
39
- def from_now
40
- self.from('now')
41
- end
42
-
43
- # "5 min".ago
44
- def ago
45
- self.unit.ago
46
- end
47
-
48
- def before(time_point = ::Time.now)
49
- self.unit.before(time_point)
50
- end
51
-
52
- def before_now
53
- self.before('now')
54
- end
55
-
56
- def since(time_point = ::Time.now)
57
- self.unit.since(time_point)
58
- end
59
-
60
- def until(time_point = ::Time.now)
61
- self.unit.until(time_point)
62
- end
63
-
64
- def to(other)
65
- self.unit.to(other)
66
- end
67
-
68
- def time(options = {})
69
- self.to_time(options) rescue self.to_datetime(options)
70
- end
71
-
72
- def to_time(options = {})
73
- begin
74
- #raises exception when Chronic not defined or when it returns a nil (i.e., can't parse the input)
75
- r = Chronic.parse(self,options)
76
- raise(ArgumentError, 'Invalid Time String') unless r
77
- return r
78
- rescue
79
- case
80
- when self == "now"
81
- Time.now
82
- when Time.respond_to?(:parse)
83
- Time.parse(self)
84
- else
85
- Time.local(*ParseDate.parsedate(self))
86
- end
87
- end
88
- end
89
-
90
- def to_datetime(options = {})
91
- begin
92
- # raises an exception if Chronic.parse = nil or if Chronic not defined
93
- r = Chronic.parse(self,options).send(:to_datetime)
94
- rescue Exception => e
95
- r = case
96
- when self.to_s == "now"
97
- DateTime.now
98
- else
99
- DateTime.parse(self)
100
- end
101
- end
102
- raise RuntimeError, "Invalid Time String (#{self.to_s})" if r == DateTime.new
103
- return r
104
- end
105
-
106
- def to_date(options={})
107
- begin
108
- r = Chronic.parse(self,options).to_date
109
- rescue
110
- r = case
111
- when self == "today"
112
- Date.today
113
- when RUBY_VERSION < "1.9"
114
- Date.civil(*ParseDate.parsedate(self)[0..5].compact)
115
- else
116
- Date.parse(self)
117
- end
118
- end
119
- raise RuntimeError, 'Invalid Date String' if r == Date.new
120
- return r
121
- end
31
+ # # "5 min".from("now")
32
+ # def from(time_point = ::Time.now)
33
+ # return old_from(time_point) if self.respond_to?(:old_from) && time_point.instance_of?(Integer)
34
+ # self.unit.from(time_point)
35
+ # end
36
+ #
37
+ # alias :after :from
122
38
 
123
- def datetime(options = {})
124
- self.to_datetime(options) rescue self.to_time(options)
39
+ # def from_now
40
+ # self.from('now')
41
+ # end
42
+ #
43
+ # # "5 min".ago
44
+ # def ago
45
+ # self.unit.ago
46
+ # end
47
+ #
48
+ # def before(time_point = ::Time.now)
49
+ # self.unit.before(time_point)
50
+ # end
51
+ #
52
+ # def before_now
53
+ # self.before('now')
54
+ # end
55
+ #
56
+ # def since(time_point = ::Time.now)
57
+ # self.unit.since(time_point)
58
+ # end
59
+ #
60
+ # def until(time_point = ::Time.now)
61
+ # self.unit.until(time_point)
62
+ # end
63
+ #
64
+ def convert_to(other)
65
+ self.unit.convert_to(other)
125
66
  end
67
+ #
68
+ # unless String.instance_methods.include?(:to)
69
+ # def to(other)
70
+ # warn "string.to is deprecated, use string.convert_to if you must, but the best would be Unit('unit').convert_to('target unit')"
71
+ # convert_to(other)
72
+ # end
73
+ # end
74
+ #
75
+ # def time(options = {})
76
+ # self.to_time(options) rescue self.to_datetime(options)
77
+ # end
78
+ #
79
+ # def to_time(options = {})
80
+ # begin
81
+ # #raises exception when Chronic not defined or when it returns a nil (i.e., can't parse the input)
82
+ # r = Chronic.parse(self,options)
83
+ # raise(ArgumentError, 'Invalid Time String') unless r
84
+ # return r
85
+ # rescue Exception => e
86
+ # puts e.inspect
87
+ # case
88
+ # when self == "now"
89
+ # Time.now
90
+ # when Time.respond_to?(:parse)
91
+ # Time.parse(self)
92
+ # else
93
+ # Time.local(*ParseDate.parsedate(self))
94
+ # end
95
+ # end
96
+ # end
97
+ #
98
+ # def to_datetime(options = {})
99
+ # begin
100
+ # # raises an exception if Chronic.parse = nil or if Chronic not defined
101
+ # r = Chronic.parse(self,options).send(:to_datetime)
102
+ # rescue NameError => e
103
+ # r = case
104
+ # when self.to_s == "now"
105
+ # DateTime.now
106
+ # else
107
+ # DateTime.parse(self)
108
+ # end
109
+ # end
110
+ # raise RuntimeError, "Invalid Time String (#{self.to_s})" if r == DateTime.new
111
+ # return r
112
+ # end
113
+ #
114
+ # def to_date(options={})
115
+ # begin
116
+ # r = Chronic.parse(self,options).to_date
117
+ # rescue
118
+ # r = case
119
+ # when self == "today"
120
+ # Date.today
121
+ # when RUBY_VERSION < "1.9"
122
+ # Date.civil(*ParseDate.parsedate(self)[0..5].compact)
123
+ # else
124
+ # Date.parse(self)
125
+ # end
126
+ # end
127
+ # raise RuntimeError, 'Invalid Date String' if r == Date.new
128
+ # return r
129
+ # end
130
+ #
131
+ # def datetime(options = {})
132
+ # self.to_datetime(options) rescue self.to_time(options)
133
+ # end
126
134
  end
@@ -0,0 +1,132 @@
1
+ #
2
+ # String Extras
3
+ #
4
+ # These are some extra string syntax sugar methods. In some cases they conflict with methods defined in other
5
+ # gems (notably with Rails). I could make them more compatilble, but honestly they should not be here in the first place.
6
+ #
7
+ # in most cases it is better to do something like
8
+ #
9
+ # Unit("1 m").convert_to("ft")
10
+ #
11
+ # instead of
12
+ #
13
+ # "1 m".convert_to("ft")
14
+ #
15
+ # to use these methods:
16
+ # require 'ruby-units/string/extra'
17
+
18
+ require 'time'
19
+ class String
20
+
21
+ #needed for compatibility with Rails, which defines a String.from method
22
+ if self.public_instance_methods.include? 'from'
23
+ alias :old_from :from
24
+ end
25
+
26
+ # "5 min".from("now")
27
+ def from(time_point = ::Time.now)
28
+ return old_from(time_point) if self.respond_to?(:old_from) && time_point.instance_of?(Integer)
29
+ warn Kernel.caller.first + " called ruby-units/string#from, which is deprecated. Use Unit(string).from(string) instead"
30
+ self.unit.from(time_point)
31
+ end
32
+
33
+ alias :after :from
34
+
35
+ def from_now
36
+ warn Kernel.caller.first + " called ruby-units/string#from_now, which is deprecated. Use Unit(string).from(Time.now) instead"
37
+ self.from(Time.now)
38
+ end
39
+
40
+ # "5 min".ago
41
+ def ago
42
+ warn Kernel.caller.first + " called ruby-units/string#ago, which is deprecated. Use Unit(string).ago instead"
43
+ self.unit.ago
44
+ end
45
+
46
+ def before(time_point = ::Time.now)
47
+ warn Kernel.caller.first + " called ruby-units/string#before, which is deprecated. Use Unit(string).before(Time.now) instead"
48
+ self.unit.before(time_point)
49
+ end
50
+
51
+ def before_now
52
+ warn Kernel.caller.first + " called ruby-units/string#before_now, which is deprecated. Use Unit(string).before(Time.now) instead"
53
+ self.before(Time.now)
54
+ end
55
+
56
+ def since(time_point = ::Time.now)
57
+ warn Kernel.caller.first + " called ruby-units/string#since, which is deprecated. Use Unit(string).since(Time.now) instead"
58
+ self.unit.since(time_point)
59
+ end
60
+
61
+ def until(time_point = ::Time.now)
62
+ warn Kernel.caller.first + " called ruby-units/string#until, which is deprecated. Use Unit(string).until(Time.now) instead"
63
+ self.unit.until(time_point)
64
+ end
65
+
66
+
67
+ unless String.instance_methods.include?(:to)
68
+ def to(other)
69
+ warn "string.to is deprecated, use string.convert_to if you must, but the best would be Unit('unit').convert_to('target unit')"
70
+ convert_to(other)
71
+ end
72
+ end
73
+
74
+ def time(options = {})
75
+ self.to_time(options) rescue self.to_datetime(options)
76
+ end
77
+
78
+ def to_time(options = {})
79
+ begin
80
+ #raises exception when Chronic not defined or when it returns a nil (i.e., can't parse the input)
81
+ r = Chronic.parse(self,options)
82
+ raise(ArgumentError, 'Invalid Time String') unless r
83
+ return r
84
+ rescue
85
+ case
86
+ when self == "now"
87
+ Time.now
88
+ when Time.respond_to?(:parse)
89
+ Time.parse(self)
90
+ else
91
+ Time.local(*ParseDate.parsedate(self))
92
+ end
93
+ end
94
+ end
95
+
96
+ def to_datetime(options = {})
97
+ begin
98
+ # raises an exception if Chronic.parse = nil or if Chronic not defined
99
+ r = Chronic.parse(self,options).send(:to_datetime)
100
+ rescue NameError => e
101
+ r = case
102
+ when self.to_s == "now"
103
+ DateTime.now
104
+ else
105
+ DateTime.parse(self)
106
+ end
107
+ end
108
+ raise RuntimeError, "Invalid Time String (#{self.to_s})" if r == DateTime.new
109
+ return r
110
+ end
111
+
112
+ def to_date(options={})
113
+ begin
114
+ r = Chronic.parse(self,options).to_date
115
+ rescue
116
+ r = case
117
+ when self == "today"
118
+ Date.today
119
+ when RUBY_VERSION < "1.9"
120
+ Date.civil(*ParseDate.parsedate(self)[0..5].compact)
121
+ else
122
+ Date.parse(self)
123
+ end
124
+ end
125
+ raise RuntimeError, 'Invalid Date String' if r == Date.new
126
+ return r
127
+ end
128
+
129
+ def datetime(options = {})
130
+ self.to_datetime(options) rescue self.to_time(options)
131
+ end
132
+ end