Class: Duration

Class | Duration
In: | lib/more/facets/duration.rb
Parent: | Object

### Methods

+ + - / <=> == [] after before days hours inspect minutes new reset_segments seconds segmented segments strftime to_a to_f to_h to_i to_s total weeks years

### Included Modules

Comparable

### Classes and Modules

Module Duration::Numeric
Module Duration::Time

### Constants

SECOND | = | 1
MINUTE | = | 60 * SECOND
HOUR | = | 60 * MINUTE
DAY | = | 24 * HOUR
WEEK | = | 7 * DAY
YEAR | = | 365 * DAY
SEGMENTS | = | %w{years weeks days hours minutes seconds}.collect{ |s| s.to_sym }

### Public Class methods

[](seconds, *segments)

[Source]

    # File lib/more/facets/duration.rb, line 14
      def self.[](seconds, *segments)
        new(seconds, *segments)
      end

new(seconds=0, *segments)

[Source]

    # File lib/more/facets/duration.rb, line 19
      def initialize(seconds=0, *segments)
        @seconds = seconds.to_i
        reset_segments(*segments)
      end

### Public Instance methods

+(other)

[Source]

    # File lib/more/facets/duration.rb, line 123
      def +(other)
        self.class.new(@seconds + other.to_i, segments)
      end

+(other)

[Source]

    # File lib/more/facets/duration.rb, line 131
      def +(other)
        self.class.new(@seconds * other.to_i, segments)
      end

-(other)

[Source]

    # File lib/more/facets/duration.rb, line 127
      def -(other)
        self.class.new(@seconds - other.to_i, segments)
      end

/(other)

[Source]

    # File lib/more/facets/duration.rb, line 135
      def /(other)
        self.class.new(@seconds / other.to_i, segments)
      end

<=>(other)

[Source]

    # File lib/more/facets/duration.rb, line 102
      def <=>(other)
        @seconds <=> other.to_i
      end

==(other)

Returns true if `other` is also a Duration instance with the same `value`, or if `other == value`.

[Source]

    # File lib/more/facets/duration.rb, line 94
      def ==(other)
        if Duration === other
          other.seconds == seconds
        else
          other == seconds
        end
      end

after(time)

[Source]

    # File lib/more/facets/duration.rb, line 211
      def after(time)
        @seconds.after(time)
      end

before(time)

[Source]

    # File lib/more/facets/duration.rb, line 206
      def before(time)
        @seconds.before(time)
      end

days()

[Source]

    # File lib/more/facets/duration.rb, line 116
      def days    ; to_h[:days]    ; end

hours()

[Source]

    # File lib/more/facets/duration.rb, line 117
      def hours   ; to_h[:hours]   ; end

inspect()

[Source]

    # File lib/more/facets/duration.rb, line 50
      def inspect
        h = to_h
        segments.reverse.collect do |l|
          "#{h[l.to_sym]} #{l}"
        end.join(' ')
      end

minutes()

[Source]

    # File lib/more/facets/duration.rb, line 118
      def minutes ; to_h[:minutes] ; end

reset_segments(*segments)

[Source]

    # File lib/more/facets/duration.rb, line 28
      def reset_segments(*segments)
        case segments.size
        when 0
          @segments = [:days, :hours, :minutes, :seconds]
        when 1
          case segments = segments[0]
          when Array
            @segments = segments.collect{ |p| (p.to_s.downcase.chomp('s') + 's').to_sym }
            raise ArgumentError unless @segments.all?{ |s| SEGMENTS.include?(s) }
          else
            f = SEGMENTS.index(segments)
            @segments = SEGMENTS[f..0]
          end
        when 2
          f = SEGMENTS.index(segments[0])
          t = SEGMENTS.index(segments[1])
          @segments = SEGMENTS[f..t]
        else
          raise ArgumentError
        end
      end

seconds()

[Source]

    # File lib/more/facets/duration.rb, line 119
      def seconds ; to_h[:seconds] ; end

segmented(*segments)

[Source]

    # File lib/more/facets/duration.rb, line 140
      def segmented(*segments)
        self.class.new(@seconds, segments)
        #segments = segments.collect{ |p| p.to_s.downcase.chomp('s') }
        #y,w,d,h,m,s = nil,nil,nil,nil,nil,nil
        #x = @seconds
        #y, x = *x.divmod(YEAR)   if segments.include?('year')
        #w, x = *x.divmod(WEEK)   if segments.include?('week')
        #d, x = *x.divmod(DAY)    if segments.include?('day')
        #h, x = *x.divmod(HOUR)   if segments.include?('hour')
        #m, x = *x.divmod(MINUTE) if segments.include?('minute')
        #s = x if segments.include?('second')
        #[y, w, d, h, m, s].compact
      end

segments()

[Source]

    # File lib/more/facets/duration.rb, line 25
      def segments; @segments; end

strftime(fmt)

Format duration.

Identifiers

        %w -- Number of weeks
        %d -- Number of days
        %h -- Number of hours
        %m -- Number of minutes
        %s -- Number of seconds
        %t -- Total number of seconds
        %x -- Duration#to_s
        %% -- Literal `%' character

Example

        d = Duration.new(:weeks => 10, :days => 7)
        => #<Duration: 11 weeks>
        d.strftime("It's been %w weeks!")
        => "It's been 11 weeks!"

[Source]

    # File lib/more/facets/duration.rb, line 174
      def strftime(fmt)
        h = to_h
        hx = {
         'y' => h[:years]  ,
         'w' => h[:weeks]  ,
         'd' => h[:days]   ,
         'h' => h[:hours]  ,
         'm' => h[:minutes],
         's' => h[:seconds],
         't' => total,
         'x' => to_s
        }
        fmt.gsub(/%?%(w|d|h|m|s|t|x)/) do |match|
          hx[match[1..1]]
        end.gsub('%%', '%')
      end

to_a()

[Source]

    # File lib/more/facets/duration.rb, line 62
      def to_a
        a, s = [], @seconds
        a[5], s = *s.divmod(YEAR)   if @segments.include?(:years)
        a[4], s = *s.divmod(WEEK)   if @segments.include?(:weeks)
        a[3], s = *s.divmod(DAY)    if @segments.include?(:days)
        a[2], s = *s.divmod(HOUR)   if @segments.include?(:hours)
        a[1], s = *s.divmod(MINUTE) if @segments.include?(:minutes)
        a[0], s = *s.divmod(SECOND) if @segments.include?(:seconds)
        a.compact.reverse
      end

to_f()

[Source]

    # File lib/more/facets/duration.rb, line 58
      def to_f ; @seconds.to_f ; end

to_h()

[Source]

    # File lib/more/facets/duration.rb, line 74
      def to_h
        h, s = {}, @seconds
        h[:years],   s = *s.divmod(YEAR)   if @segments.include?(:years)
        h[:weeks],   s = *s.divmod(WEEK)   if @segments.include?(:weeks)
        h[:days],    s = *s.divmod(DAY)    if @segments.include?(:days)
        h[:hours],   s = *s.divmod(HOUR)   if @segments.include?(:hours)
        h[:minutes], s = *s.divmod(MINUTE) if @segments.include?(:minutes)
        h[:seconds], s = *s.divmod(SECOND) if @segments.include?(:seconds)
        h
      end

to_i()

[Source]

    # File lib/more/facets/duration.rb, line 57
      def to_i ; @seconds.to_i ; end

to_s()

[Source]

    # File lib/more/facets/duration.rb, line 85
      def to_s
        h = to_h
        segments.reverse.collect do |l|
          "#{h[l.to_sym]} #{l}"
        end.join(' ')
      end

total()

[Source]

    # File lib/more/facets/duration.rb, line 121
      def total ; seconds ; end

weeks()

[Source]

    # File lib/more/facets/duration.rb, line 115
      def weeks   ; to_h[:weeks]   ; end

years()

def self.===(other) #:nodoc:

      other.is_a?(Duration) rescue super

end

[Source]

    # File lib/more/facets/duration.rb, line 114
      def years   ; to_h[:years]   ; end

[Validate]