| Module | Runt::DPrecision |
| In: |
lib/runt/dprecision.rb
|
Module providing automatic precisioning of Date, DateTime, and PDate classes.
Inspired by a pattern by Martin Fowler.
| Author: | Matthew Lipper |
| YEAR | = | Precision.year | Pseudo Singletons: | |
| MONTH | = | Precision.month | ||
| WEEK | = | Precision.week | ||
| DAY | = | Precision.day | ||
| HOUR | = | Precision.hour | ||
| MIN | = | Precision.min | ||
| SEC | = | Precision.sec | ||
| MILLI | = | Precision.millisec | ||
| DEFAULT | = | MIN |
# File lib/runt/dprecision.rb, line 33 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
# File lib/runt/dprecision.rb, line 18 def DPrecision.to_p(date,prec=DEFAULT) 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