Module: Runt::DPrecision

Included in:
DateRange, PDate
Defined in:
lib/runt/dprecision.rb

Overview

:title:DPrecision

DPrecision

Module providing automatic precisioning of Date, DateTime, and PDate classes.

Inspired by a pattern[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.

Author

Matthew Lipper

Defined Under Namespace

Classes: Precision

Constant Summary collapse

YEAR =

Pseudo Singletons:

Precision.year
MONTH =
Precision.month
WEEK =
Precision.week
DAY =
Precision.day
HOUR =
Precision.hour
MIN =
Precision.min
SEC =
Precision.sec
MILLI =
Precision.millisec
DEFAULT =
MIN

Class Method Summary collapse

Class Method Details

.explode(date, prec) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/runt/dprecision.rb', line 35

def DPrecision.explode(date,prec)
  result = [date.year,date.month,date.day]
    if(date.respond_to?(:hour))
      result << date.hour << date.min << date.sec
    else
      result << 0 << 0 << 0
    end
  result
end

.to_p(date, prec = DEFAULT) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/runt/dprecision.rb', line 18

def DPrecision.to_p(date,prec=DEFAULT)
	  has_p = date.respond_to?(:date_precision)
	  #puts "DPrecision.to_p(#{date.class}<#{has_p ? date.date_precision : nil}>,#{prec})" 
	  return date if PDate == date.class && (prec == date.date_precision) 
  case prec
    when MIN then PDate.min(*DPrecision.explode(date,prec))
    when DAY then PDate.day(*DPrecision.explode(date,prec))
    when HOUR then PDate.hour(*DPrecision.explode(date,prec))
    when WEEK then PDate.week(*DPrecision.explode(date,prec))
    when MONTH then PDate.month(*DPrecision.explode(date,prec))
    when YEAR then PDate.year(*DPrecision.explode(date,prec))
    when SEC then PDate.sec(*DPrecision.explode(date,prec))
    when MILLI then date #raise "Not implemented."
    else PDate.default(*DPrecision.explode(date,prec))
  end
end