Class: DTime
Overview
class Numeric
Defined Under Namespace
Classes: TimeUnit
Constant Summary collapse
- @@time_units =
Time units
[]
Instance Attribute Summary collapse
-
#delta ⇒ Object
(also: #to_f, #to_second, #to_sec)
readonly
Returns the value of attribute delta.
Class Method Summary collapse
Instance Method Summary collapse
- #+@ ⇒ Object
- #-@ ⇒ Object
- #<=>(rhs) ⇒ Object
- #coerce(lhs) ⇒ Object
-
#each(&block) ⇒ Object
Warning: Values are spawned from the largest unit to the finest (e.g. days, hours, minutes…).
- #floor ⇒ Object
- #hash ⇒ Object
-
#initialize(delta) ⇒ DTime
constructor
A new instance of DTime.
- #inspect ⇒ Object
- #round ⇒ Object
- #second ⇒ Object (also: #sec)
-
#to_a ⇒ Object
Warning: This method does not give you the sign of the DTime.
-
#to_hash ⇒ Object
Warning: This method does not give you the sign of the DTime.
- #to_i ⇒ Object
- #to_s ⇒ Object
- #to_yaml_string ⇒ Object
Constructor Details
#initialize(delta) ⇒ DTime
Returns a new instance of DTime.
21 22 23 |
# File 'lib/d_time.rb', line 21 def initialize(delta) @delta = delta.to_f end |
Instance Attribute Details
#delta ⇒ Object (readonly) Also known as: to_f, to_second, to_sec
Returns the value of attribute delta.
25 26 27 |
# File 'lib/d_time.rb', line 25 def delta @delta end |
Class Method Details
.diff(*args, &block) ⇒ Object
190 191 192 193 194 |
# File 'lib/d_time.rb', line 190 def self.diff ( *args, &block ) start = Time.now block[*args] return Time.now - start end |
.time_unit(unit_name, value_in_seconds) ⇒ Object
class TimeUnit
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/d_time.rb', line 78 def self.time_unit ( unit_name, value_in_seconds ) value_in_seconds = value_in_seconds.to_f unit = TimeUnit.new unit_name, value_in_seconds @@time_units << unit current_size = @@time_units.size to_unit_name = "to_#{unit_name}" to_unit_plural = "to_#{unit.plural}" # Conversion method unless method_defined? to_unit_name define_method(to_unit_name) { delta / value_in_seconds } end unless method_defined? to_unit_plural alias_method to_unit_plural, to_unit_name end # Component extraction method unless method_defined? unit_name define_method(unit_name) do next_unit = @@time_units[current_size] modulo = (next_unit.nil?)? 1 : next_unit.value_in_seconds ((delta.abs % modulo) / value_in_seconds).to_i end end unless method_defined? unit.plural alias_method unit.plural, unit_name end # Numeric extension unless Numeric.method_defined? unit_name Numeric.send(:define_method, unit_name) { DTime.new(self * value_in_seconds) } end unless Numeric.method_defined? unit.plural Numeric.send(:alias_method, unit.plural, unit_name) end end |
Instance Method Details
#+@ ⇒ Object
47 48 49 |
# File 'lib/d_time.rb', line 47 def +@ self end |
#<=>(rhs) ⇒ Object
31 32 33 |
# File 'lib/d_time.rb', line 31 def <=> ( rhs ) @delta <=> rhs.to_f end |
#coerce(lhs) ⇒ Object
27 28 29 |
# File 'lib/d_time.rb', line 27 def coerce ( lhs ) [lhs.to_dtime, self] end |
#each(&block) ⇒ Object
Warning: Values are spawned from the largest unit to the finest (e.g. days, hours, minutes…). Warning: This method does not give you the sign of the DTime.
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/d_time.rb', line 142 def each ( &block ) accu = @delta.abs @@time_units.reverse_each do |unit| if unit.name == :second block[accu, unit] else x, accu = accu.divmod(unit.value_in_seconds) next if x.zero? and accu == @delta.abs block[x.to_i, unit] end end end |
#floor ⇒ Object
57 58 59 |
# File 'lib/d_time.rb', line 57 def floor @delta.floor end |
#hash ⇒ Object
65 66 67 |
# File 'lib/d_time.rb', line 65 def hash @delta.hash end |
#inspect ⇒ Object
175 176 177 |
# File 'lib/d_time.rb', line 175 def inspect to_s + ':DTime' end |
#round ⇒ Object
61 62 63 |
# File 'lib/d_time.rb', line 61 def round @delta.round end |
#second ⇒ Object Also known as: sec
115 116 117 |
# File 'lib/d_time.rb', line 115 def second delta.abs % 60 end |
#to_a ⇒ Object
Warning: This method does not give you the sign of the DTime.
156 157 158 159 160 |
# File 'lib/d_time.rb', line 156 def to_a result = [] each { |x, unit| result << x } result end |
#to_hash ⇒ Object
Warning: This method does not give you the sign of the DTime.
184 185 186 187 188 |
# File 'lib/d_time.rb', line 184 def to_hash result = {} each { |x, unit| result[unit.name] = x } result.freeze end |
#to_i ⇒ Object
53 54 55 |
# File 'lib/d_time.rb', line 53 def to_i @delta.to_i end |
#to_s ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/d_time.rb', line 162 def to_s result = [] each do |x, unit| case x when 0 when 1 then result << "1 #{unit.name}" else result << "#{x} #{unit.plural}" end end result << '0' if result.empty? (((@delta < 0)? '-' : '') + result.join(' ')).freeze end |
#to_yaml_string ⇒ Object
179 180 181 |
# File 'lib/d_time.rb', line 179 def to_yaml_string to_s end |