Class: String
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
- #before(time_point = ::Time.now) ⇒ Object (also: #before_now)
- #datetime(options = {}) ⇒ Object
- #from(time_point = ::Time.now) ⇒ Object (also: #after, #from_now)
- #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 Unit === args[0] args[0].to_s(self) when (!defined?(Uncertain).nil? && (Uncertain === args[0])) args[0].to_s(self) when Complex === args[0] args[0].to_s else unit_format(*args) end end |
#ago ⇒ Object
39 40 41 |
# File 'lib/ruby_units/string.rb', line 39 def ago self.unit.ago end |
#before(time_point = ::Time.now) ⇒ Object Also known as: before_now
43 44 45 |
# File 'lib/ruby_units/string.rb', line 43 def before(time_point = ::Time.now) self.unit.before(time_point) end |
#datetime(options = {}) ⇒ Object
115 116 117 |
# File 'lib/ruby_units/string.rb', line 115 def datetime( = {}) self.to_datetime() rescue self.to_time() end |
#from(time_point = ::Time.now) ⇒ Object Also known as: after, from_now
31 32 33 34 |
# File 'lib/ruby_units/string.rb', line 31 def from(time_point = ::Time.now) return old_from(time_point) if Integer === time_point self.unit.from(time_point) end |
#old_from ⇒ Object
28 |
# File 'lib/ruby_units/string.rb', line 28 alias :old_from :from |
#since(time_point = ::Time.now) ⇒ Object
48 49 50 |
# File 'lib/ruby_units/string.rb', line 48 def since(time_point = ::Time.now) self.unit.since(time_point) end |
#time(options = {}) ⇒ Object
60 61 62 |
# File 'lib/ruby_units/string.rb', line 60 def time( = {}) self.to_time() rescue self.to_datetime() end |
#to(other) ⇒ Object
56 57 58 |
# File 'lib/ruby_units/string.rb', line 56 def to(other) self.unit.to(other) end |
#to_date(options = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ruby_units/string.rb', line 98 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ruby_units/string.rb', line 82 def to_datetime( = {}) begin # raises an exception if Chronic.parse = nil or if Chronic not defined r = Chronic.parse(self,).send(:to_datetime) rescue Exception => 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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ruby_units/string.rb', line 64 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).to(other) : Unit.new(self) end |
#unit_format ⇒ Object
9 |
# File 'lib/ruby_units/string.rb', line 9 alias :unit_format :% |