Class DTime
In: lib/d_time.rb
Parent: Object

Methods

+@   -@   <=>   coerce   diff   each   floor   hash   inspect   new   round   sec   second   time_unit   to_a   to_hash   to_i   to_s   to_yaml_string  

Included Modules

Comparable

Classes and Modules

Class DTime::TimeUnit

External Aliases

delta -> to_f
delta -> to_second
delta -> to_sec
minutes -> min
to_minutes -> to_min

Attributes

delta  [R] 

Public Class methods

[Source]

# File lib/d_time.rb, line 190
  def self.diff ( *args, &block )
    start = Time.now
    block[*args]
    return Time.now - start
  end

[Source]

# File lib/d_time.rb, line 21
  def initialize(delta)
    @delta = delta.to_f
  end

[Source]

# 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

Public Instance methods

[Source]

# File lib/d_time.rb, line 47
  def +@
    self
  end

[Source]

# File lib/d_time.rb, line 43
  def -@
    DTime.new(-@delta)
  end

[Source]

# File lib/d_time.rb, line 31
  def <=> ( rhs )
    @delta <=> rhs.to_f
  end

[Source]

# File lib/d_time.rb, line 27
  def coerce ( lhs )
    [lhs.to_dtime, self]
  end

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.

[Source]

# 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

[Source]

# File lib/d_time.rb, line 57
  def floor
    @delta.floor
  end

[Source]

# File lib/d_time.rb, line 65
  def hash
    @delta.hash
  end

[Source]

# File lib/d_time.rb, line 175
  def inspect
    to_s + ':DTime'
  end

[Source]

# File lib/d_time.rb, line 61
  def round
    @delta.round
  end
sec()

Alias for second

[Source]

# File lib/d_time.rb, line 115
  def second
    delta.abs % 60
  end

Warning: This method does not give you the sign of the DTime.

[Source]

# File lib/d_time.rb, line 156
  def to_a
    result = []
    each { |x, unit| result << x }
    result
  end

Warning: This method does not give you the sign of the DTime.

[Source]

# File lib/d_time.rb, line 184
  def to_hash
    result = {}
    each { |x, unit| result[unit.name] = x }
    result.freeze
  end

[Source]

# File lib/d_time.rb, line 53
  def to_i
    @delta.to_i
  end

[Source]

# 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

[Source]

# File lib/d_time.rb, line 179
  def to_yaml_string
    to_s
  end

[Validate]