Class: QDM::Interval

Inherits:
Object
  • Object
show all
Defined in:
app/models/qdm/basetypes/interval.rb

Overview

Represents an Interval

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(low, high, lowClosed = true, highClosed = true) ⇒ Interval

Low and high are required (at minimum).



7
8
9
10
11
12
# File 'app/models/qdm/basetypes/interval.rb', line 7

def initialize(low, high, lowClosed = true, highClosed = true)
  @low = low
  @high = high.is_a?(DateTime) && (high.year > 9999) ? high.change(year: 9999) : high
  @lowClosed = lowClosed
  @highClosed = highClosed
end

Instance Attribute Details

#highObject

Returns the value of attribute high.



4
5
6
# File 'app/models/qdm/basetypes/interval.rb', line 4

def high
  @high
end

#highClosedObject

Returns the value of attribute highClosed.



4
5
6
# File 'app/models/qdm/basetypes/interval.rb', line 4

def highClosed
  @highClosed
end

#lowObject

Returns the value of attribute low.



4
5
6
# File 'app/models/qdm/basetypes/interval.rb', line 4

def low
  @low
end

#lowClosedObject

Returns the value of attribute lowClosed.



4
5
6
# File 'app/models/qdm/basetypes/interval.rb', line 4

def lowClosed
  @lowClosed
end

Class Method Details

.demongoize(object) ⇒ Object

Get the object as it was stored in the database, and instantiate this custom class from it.

The array elements in demongoize are the same 5 elements used in mongoize, i.e. [ low, high ].



63
64
65
66
67
68
69
# File 'app/models/qdm/basetypes/interval.rb', line 63

def demongoize(object)
  return nil unless object

  object = object.symbolize_keys
  fix_datetime(object)
  QDM::Interval.new(object[:low], object[:high], object[:lowClosed], object[:highClosed]) if object.is_a?(Hash)
end

.evolve(object) ⇒ Object

Converts the object that was supplied to a criteria and converts it into a database friendly form.



93
94
95
96
97
98
# File 'app/models/qdm/basetypes/interval.rb', line 93

def evolve(object)
  case object
  when QDM::Interval then object.mongoize
  else object
  end
end

.fix_datetime(object) ⇒ Object



85
86
87
88
89
# File 'app/models/qdm/basetypes/interval.rb', line 85

def fix_datetime(object)
  # Cast to DateTime if it is a string representing a DateTime
  object[:low] = DateTime.parse(object[:low]) if (object[:low].is_a? String) && DateTime.parse(object[:low])
  object[:high] = DateTime.parse(object[:high]) if (object[:high].is_a? String) && DateTime.parse(object[:high])
end

.mongoize(object) ⇒ Object

Takes any possible object and converts it to how it would be stored in the database.



73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/qdm/basetypes/interval.rb', line 73

def mongoize(object)
  case object
  when nil then nil
  when QDM::Interval then object.mongoize
  when Hash
    object = object.symbolize_keys
    fix_datetime(object)
    QDM::Interval.new(object[:low], object[:high], object[:lowClosed], object[:highClosed]).mongoize
  else object
  end
end

Instance Method Details

#mongoizeObject

Converts an object of this instance into a database friendly value.



15
16
17
# File 'app/models/qdm/basetypes/interval.rb', line 15

def mongoize
  { low: @low, high: @high, lowClosed: @lowClosed, highClosed: @highClosed, _type: 'QDM::Interval' }
end

#shift_dates(seconds) ⇒ Object

Shift dates by the given value. Given value should be in seconds. Positive values shift forward, negative values shift backwards.

NOTE: This will only shift if @high and @low are DateTimes.



24
25
26
27
28
29
30
31
# File 'app/models/qdm/basetypes/interval.rb', line 24

def shift_dates(seconds)
  @low = (@low.utc.to_time + seconds.seconds).to_datetime.new_offset(0) if (@low.is_a? DateTime) || (@low.is_a? Time)
  if (@high.is_a? DateTime) || (@high.is_a? Time)
    @high = (@high.utc.to_time + seconds.seconds).to_datetime.new_offset(0)
    @high = @high.year > 9999 ? @high.change(year: 9999) : @high
  end
  self
end

#shift_years(year_shift) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/qdm/basetypes/interval.rb', line 33

def shift_years(year_shift)
  if (@low.is_a? DateTime) || (@low.is_a? Time)
    raise RangeError, 'Year was shifted after 9999 or before 0001' if @low.year + year_shift < 1 || @low.year + year_shift > 9999

    low_shift = @low.year + year_shift
    @low = if @low.month == 2 && @low.day == 29 && !::Date.leap?(low_shift)
             @low.change(year: low_shift, day: 28)
           else
             @low.change(year: low_shift)
           end
  end
  if (@high.is_a? DateTime) || (@high.is_a? Time)
    raise RangeError, 'Year was shifted after 9999 or before 0001' if @high.year + year_shift < 1 || @high.year + year_shift > 9999

    high_shift = @high.year + year_shift
    @high = if @high.month == 2 && @high.day == 29 && !::Date.leap?(high_shift)
              @high.change(year: high_shift, day: 28)
            else
              @high.change(year: high_shift)
            end
  end
  self
end