Class: Runt::DateRange

Inherits:
Range
  • Object
show all
Includes:
DPrecision
Defined in:
lib/runt/daterange.rb

Overview

:title:DateRange

DateRange

Based the range[http://martinfowler.com/ap2/range.html] pattern by Martin Fowler.

Author

Matthew Lipper

Constant Summary collapse

EMPTY =
DateRange.new(PDate.day(2004,2,2),PDate.day(2004,2,1))

Constants included from DPrecision

Runt::DPrecision::DAY, Runt::DPrecision::DEFAULT, Runt::DPrecision::HOUR, Runt::DPrecision::MILLI, Runt::DPrecision::MIN, Runt::DPrecision::MONTH, Runt::DPrecision::SEC, Runt::DPrecision::WEEK, Runt::DPrecision::YEAR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DPrecision

explode, to_p

Constructor Details

#initialize(start_expr, end_expr, exclusive = false) ⇒ DateRange

Returns a new instance of DateRange.



23
24
25
26
# File 'lib/runt/daterange.rb', line 23

def initialize(start_expr, end_expr,exclusive=false)
  super(start_expr, end_expr,exclusive)
  @start_expr, @end_expr = start_expr, end_expr
end

Instance Attribute Details

#end_exprObject (readonly)

Returns the value of attribute end_expr.



21
22
23
# File 'lib/runt/daterange.rb', line 21

def end_expr
  @end_expr
end

#start_exprObject (readonly)

Returns the value of attribute start_expr.



21
22
23
# File 'lib/runt/daterange.rb', line 21

def start_expr
  @start_expr
end

Instance Method Details

#<=>(other) ⇒ Object



61
62
63
64
# File 'lib/runt/daterange.rb', line 61

def <=>(other)
  return @start_expr <=> other.start_expr if(@start_expr != other.start_expr)
  return @end_expr <=> other.end_expr
end

#empty?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/runt/daterange.rb', line 39

def empty?
  return @start_expr >= @end_expr
end

#gap(obj) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/runt/daterange.rb', line 43

def gap(obj)

  return EMPTY if self.overlap? obj

  lower=nil
  higher=nil

  if((self<=>obj)<0)
    lower=self
    higher=obj
  else
    lower=obj
    higher=self
  end

  return DateRange.new((lower.end_expr+1),(higher.start_expr-1))
end

#include?(obj) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/runt/daterange.rb', line 28

def include?(obj)
  return super(obj.min) && super(obj.max) if obj.kind_of? Range
  return super(obj)
end

#maxObject



67
# File 'lib/runt/daterange.rb', line 67

def max; @end_expr  end

#minObject



66
# File 'lib/runt/daterange.rb', line 66

def min; @start_expr  end

#overlap?(obj) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/runt/daterange.rb', line 33

def overlap?(obj)
  return true if( member?(obj) || include?(obj.min) || include?(obj.max) )
  return true if( obj.kind_of?(Range) && obj.include?(self) )
  false
end

#to_sObject



68
# File 'lib/runt/daterange.rb', line 68

def to_s; @start_expr.to_s + " " + @end_expr.to_s end