Class: BigDuration

Inherits:
Duration show all
Defined in:
lib/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

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.



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/duration.rb', line 345

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.



336
337
338
# File 'lib/duration.rb', line 336

def months
  @months
end

#yearsObject

Returns the value of attribute years.



336
337
338
# File 'lib/duration.rb', line 336

def years
  @years
end

Instance Method Details

#eachObject

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



392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/duration.rb', line 392

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.



407
408
409
410
411
412
413
414
# File 'lib/duration.rb', line 407

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



385
386
387
388
# File 'lib/duration.rb', line 385

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