Class: BigDuration

Inherits:
Duration show all
Defined in:
lib/more/facets/duration.rb

Overview

BigDuration is a variant of Duration that supports years and months. Support for months is not accurate, as a month is assumed to be 30 days so use at your own risk.

Constant Summary collapse

YEAR =
60 * 60 * 24 * 30 * 12
MONTH =
60 * 60 * 24 * 30

Constants inherited from Duration

Duration::DAY, Duration::HOUR, Duration::MINUTE, Duration::SECOND, Duration::WEEK

Instance Attribute Summary collapse

Attributes inherited from Duration

#days, #hours, #minutes, #total, #weeks

Instance Method Summary collapse

Methods inherited from Duration

#*, #+, #-, #/, #<=>, #inspect, #seconds=, #to_s

Methods included from Enumerable

#**, #accumulate, cart, cartesian_product, #cartesian_product, #cluster_by, #collect_if, #collect_with_index, combinations, #combos, #commonality, #compact_collect, #count, #divide, #each_by, #each_combination, #each_combo, #each_pair, #each_permutation, #eachn, #elementwise, #entropy, #every, #every!, #filter_collect, #frequency, #group_by, #ideal_entropy, #inject!, #injecting, #map_send, #mash, #mode, #modulate, #none?, #nonuniq, #occur, #one?, #permutation, #permutation_number, #probability, #split, #sum, #threaded_map, #threaded_map_send, #to_elem, #to_h, #to_hash, #uniq_by

Methods included from Comparable

#at_least, #at_most, #cap, #clip, #cmp

Constructor Details

#initialize(seconds_or_attr = 0) ⇒ BigDuration

Similar to Duration.new except that BigDuration.new supports ‘:years’ and ‘:months’ and will also handle years and months correctly when breaking down the seconds.



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/more/facets/duration.rb', line 360

def initialize(seconds_or_attr = 0)
    if seconds_or_attr.kind_of? Hash
        # Part->time map table.
        h =\
        {:years    =>  YEAR  ,
         :months   =>  MONTH ,
         :weeks    =>  WEEK  ,
         :days     =>  DAY   ,
         :hours    =>  HOUR  ,
         :minutes  =>  MINUTE,
         :seconds  =>  SECOND}

        # Loop through each valid part, ignore all others.
        seconds = seconds_or_attr.inject(0) do |sec, args|
            # Grab the part of the duration (week, day, whatever) and the number of seconds for it.
            part, time = args

            # Map each part to their number of seconds and the given value.
            # {:weeks => 2} maps to h[:weeks] -- so... weeks = WEEK * 2
            if h.key?(prt = part.to_s.to_sym) then sec + time * h[prt] else 0 end
        end
    else
        seconds = seconds_or_attr
    end

    @total, array = seconds.to_f.round, []
    @seconds = [YEAR, MONTH, WEEK, DAY, HOUR, MINUTE].inject(@total) do |left, part|
        array << left / part; left % part
    end

    @years, @months, @weeks, @days, @hours, @minutes = array
end

Instance Attribute Details

#monthsObject

Returns the value of attribute months.



351
352
353
# File 'lib/more/facets/duration.rb', line 351

def months
  @months
end

#yearsObject

Returns the value of attribute years.



351
352
353
# File 'lib/more/facets/duration.rb', line 351

def years
  @years
end

Instance Method Details

#eachObject

Similar to Duration#each except includes years and months in the interation.



407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/more/facets/duration.rb', line 407

def each
    [['years'   ,  @years  ],
     ['months'  ,  @months ],
     ['weeks'   ,  @weeks  ],
     ['days'    ,  @days   ],
     ['hours'   ,  @hours  ],
     ['minutes' ,  @minutes],
     ['seconds' ,  @seconds]].each do |part, time|
         # Yield to block
        yield part, time
    end
end

#seconds(part = nil) ⇒ Object

Derived from Duration#seconds, but supports ‘:years’ and ‘:months’ as well.



422
423
424
425
426
427
428
429
# File 'lib/more/facets/duration.rb', line 422

def seconds(part = nil)
    h = {:years => YEAR, :months => MONTH}
    if [:years, :months].include? part
        __send__(part) * h[part]
    else
        super(part)
    end
end

#strftime(fmt) ⇒ Object

BigDuration variant of Duration#strftime.

*Identifiers: BigDuration*

%y -- Number of years
%m -- Number of months


400
401
402
403
# File 'lib/more/facets/duration.rb', line 400

def strftime(fmt)
    h = {'y' => @years, 'M' => @months}
    super(fmt.gsub(/%?%(y|M)/) { |match| match.size == 3 ? match : h[match[1..1]] })
end