Class: String
- Defined in:
- lib/ruby_units/string.rb,
lib/ruby_units/string/extra.rb
Overview
make a string into a unit
Instance Method Summary collapse
-
#%(*args) ⇒ Object
format unit output using formating codes ‘%0.2f’ % ‘1 mm’.unit => ‘1.00 mm’.
-
#ago ⇒ Object
“5 min”.ago.
- #before(time_point = ::Time.now) ⇒ Object
- #before_now ⇒ Object
-
#convert_to(other) ⇒ Object
def from_now self.from(‘now’) end.
- #datetime(options = {}) ⇒ Object
-
#from(time_point = ::Time.now) ⇒ Object
(also: #after)
“5 min”.from(“now”).
- #from_now ⇒ Object
- #old_from ⇒ Object
- #since(time_point = ::Time.now) ⇒ Object
- #time(options = {}) ⇒ Object
- #to(other) ⇒ Object
- #to_date(options = {}) ⇒ Object
- #to_datetime(options = {}) ⇒ Object
- #to_time(options = {}) ⇒ Object
- #to_unit(other = nil) ⇒ Object (also: #unit, #u)
- #unit_format ⇒ Object
- #until(time_point = ::Time.now) ⇒ Object
Instance Method Details
#%(*args) ⇒ Object
format unit output using formating codes ‘%0.2f’ % ‘1 mm’.unit => ‘1.00 mm’
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ruby_units/string.rb', line 12 def %(*args) return "" if self.empty? case when args.first.is_a?(Unit) args.first.to_s(self) when (!defined?(Uncertain).nil? && args.first.is_a?(Uncertain)) args.first.to_s(self) when args.first.is_a?(Complex) args.first.to_s else unit_format(*args) end end |
#ago ⇒ Object
“5 min”.ago
41 42 43 44 |
# File 'lib/ruby_units/string/extra.rb', line 41 def ago warn Kernel.caller.first + " called ruby-units/string#ago, which is deprecated. Use Unit(string).ago instead" self.unit.ago end |
#before(time_point = ::Time.now) ⇒ Object
46 47 48 49 |
# File 'lib/ruby_units/string/extra.rb', line 46 def before(time_point = ::Time.now) warn Kernel.caller.first + " called ruby-units/string#before, which is deprecated. Use Unit(string).before(Time.now) instead" self.unit.before(time_point) end |
#before_now ⇒ Object
51 52 53 54 |
# File 'lib/ruby_units/string/extra.rb', line 51 def before_now warn Kernel.caller.first + " called ruby-units/string#before_now, which is deprecated. Use Unit(string).before(Time.now) instead" self.before(Time.now) end |
#convert_to(other) ⇒ Object
def from_now
self.from('now')
end
# “5 min”.ago def ago
self.unit.ago
end
def before(time_point = ::Time.now)
self.unit.before(time_point)
end
def before_now
self.before('now')
end
def since(time_point = ::Time.now)
self.unit.since(time_point)
end
def until(time_point = ::Time.now)
self.unit.until(time_point)
end
64 65 66 |
# File 'lib/ruby_units/string.rb', line 64 def convert_to(other) self.unit.convert_to(other) end |
#datetime(options = {}) ⇒ Object
129 130 131 |
# File 'lib/ruby_units/string/extra.rb', line 129 def datetime( = {}) self.to_datetime() rescue self.to_time() end |
#from(time_point = ::Time.now) ⇒ Object Also known as: after
“5 min”.from(“now”)
27 28 29 30 31 |
# File 'lib/ruby_units/string/extra.rb', line 27 def from(time_point = ::Time.now) return old_from(time_point) if self.respond_to?(:old_from) && time_point.instance_of?(Integer) warn Kernel.caller.first + " called ruby-units/string#from, which is deprecated. Use Unit(string).from(string) instead" self.unit.from(time_point) end |
#from_now ⇒ Object
35 36 37 38 |
# File 'lib/ruby_units/string/extra.rb', line 35 def from_now warn Kernel.caller.first + " called ruby-units/string#from_now, which is deprecated. Use Unit(string).from(Time.now) instead" self.from(Time.now) end |
#old_from ⇒ Object
23 |
# File 'lib/ruby_units/string/extra.rb', line 23 alias :old_from :from |
#since(time_point = ::Time.now) ⇒ Object
56 57 58 59 |
# File 'lib/ruby_units/string/extra.rb', line 56 def since(time_point = ::Time.now) warn Kernel.caller.first + " called ruby-units/string#since, which is deprecated. Use Unit(string).since(Time.now) instead" self.unit.since(time_point) end |
#time(options = {}) ⇒ Object
74 75 76 |
# File 'lib/ruby_units/string/extra.rb', line 74 def time( = {}) self.to_time() rescue self.to_datetime() end |
#to(other) ⇒ Object
68 69 70 71 |
# File 'lib/ruby_units/string/extra.rb', line 68 def to(other) warn "string.to is deprecated, use string.convert_to if you must, but the best would be Unit('unit').convert_to('target unit')" convert_to(other) end |
#to_date(options = {}) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ruby_units/string/extra.rb', line 112 def to_date(={}) begin r = Chronic.parse(self,).to_date rescue r = case when self == "today" Date.today when RUBY_VERSION < "1.9" Date.civil(*ParseDate.parsedate(self)[0..5].compact) else Date.parse(self) end end raise RuntimeError, 'Invalid Date String' if r == Date.new return r end |
#to_datetime(options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ruby_units/string/extra.rb', line 96 def to_datetime( = {}) begin # raises an exception if Chronic.parse = nil or if Chronic not defined r = Chronic.parse(self,).send(:to_datetime) rescue NameError => e r = case when self.to_s == "now" DateTime.now else DateTime.parse(self) end end raise RuntimeError, "Invalid Time String (#{self.to_s})" if r == DateTime.new return r end |
#to_time(options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ruby_units/string/extra.rb', line 78 def to_time( = {}) begin #raises exception when Chronic not defined or when it returns a nil (i.e., can't parse the input) r = Chronic.parse(self,) raise(ArgumentError, 'Invalid Time String') unless r return r rescue case when self == "now" Time.now when Time.respond_to?(:parse) Time.parse(self) else Time.local(*ParseDate.parsedate(self)) end end end |
#to_unit(other = nil) ⇒ Object Also known as: unit, u
4 5 6 |
# File 'lib/ruby_units/string.rb', line 4 def to_unit(other = nil) other ? Unit.new(self).convert_to(other) : Unit.new(self) end |
#unit_format ⇒ Object
9 |
# File 'lib/ruby_units/string.rb', line 9 alias :unit_format :% |