Class: DateCalculator

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/date_calculator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, label = nil, calculate = true) ⇒ DateCalculator

Returns a new instance of DateCalculator.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'backend/app/model/date_calculator.rb', line 5

def initialize(obj, label = nil, calculate = true)
  @root_object = obj

  @resource = obj.respond_to?(:root_record_id) ? obj.class.root_model[obj.root_record_id] : @root_object
  @label = label

  @min_begin = nil
  @min_begin_date = nil
  @max_end = nil
  @max_end_date = nil

  calculate! if calculate
end

Instance Attribute Details

#max_endObject (readonly)

Returns the value of attribute max_end



3
4
5
# File 'backend/app/model/date_calculator.rb', line 3

def max_end
  @max_end
end

#max_end_dateObject (readonly)

Returns the value of attribute max_end_date



3
4
5
# File 'backend/app/model/date_calculator.rb', line 3

def max_end_date
  @max_end_date
end

#min_beginObject (readonly)

Returns the value of attribute min_begin



3
4
5
# File 'backend/app/model/date_calculator.rb', line 3

def min_begin
  @min_begin
end

#min_begin_dateObject (readonly)

Returns the value of attribute min_begin_date



3
4
5
# File 'backend/app/model/date_calculator.rb', line 3

def min_begin_date
  @min_begin_date
end

Instance Method Details

#calculate!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'backend/app/model/date_calculator.rb', line 19

def calculate!
    DB.open do |db|
      date_query = db[:date]
                     .filter(Sequel.|(Sequel.~(:begin => nil), Sequel.~(:end => nil)))
                     .select(:begin, :end)


      if @root_object.is_a?(Resource)
        ao_ids = db[:archival_object]
                  .filter(:root_record_id => @root_object.id)
                  .select(:id)

        date_query = date_query
                      .filter(Sequel.|({:resource_id => @resource.id},
                                       {:archival_object_id => ao_ids}))
      else
        ao_ids = [@root_object.id]
        parent_ids = [@root_object.id]

        while (true) do
          ids = db[:archival_object]
                 .filter(:parent_id => parent_ids)
                 .select(:id)
                 .map {|row| row[:id]}

          if ids.empty?
            break
          else
            ao_ids.concat(ids)
            parent_ids = ids
          end
        end

        date_query = date_query
                       .filter(:archival_object_id => ao_ids)
      end

      if @label
        label_id = db[:enumeration_value]
                     .filter(:enumeration_id => db[:enumeration].filter(:name => 'date_label').select(:id))
                     .filter(:value => @label)
                     .select(:id)

        date_query = date_query.filter(:label_id => label_id)
      end

      date_query.map {|row|
        begin_raw = row[:begin]
        end_raw = row[:end] || begin_raw

        begin_date = coerce_begin_date(begin_raw)
        end_date = coerce_end_date(end_raw)

        @min_begin_date = [@min_begin_date, begin_date].compact.min
        @min_begin = begin_raw if @min_begin_date == begin_date

        @max_end_date = [@max_end_date, end_date].compact.max
        @max_end = end_raw if @max_end_date == end_date
      }
    end
  end

  def to_hash
    {
      :object => {:uri => @root_object.uri,
                  :jsonmodel_type => @root_object.class.my_jsonmodel.record_type,
                  :title => @root_object.title || @root_object.display_string,
                  :id => @root_object.id},
      :resource => @resource ? {:uri => @resource.uri, :title => @resource.title} : nil,

      :label => @label,

      :min_begin_date => @min_begin_date,
      :min_begin => @min_begin,
      :max_end_date => @max_end_date,
      :max_end => @max_end
    }
  end

  private

  def coerce_begin_date(raw_date)
    return if raw_date.nil?

    if raw_date =~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?$/
      Date.strptime(raw_date, '%Y-%m-%d')
    elsif raw_date =~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]?$/
      Date.strptime("#{raw_date}-01", '%Y-%m-%d')
    elsif raw_date =~ /^[0-9][0-9][0-9][0-9]$/
      Date.strptime("#{raw_date}-01-01", '%Y-%m-%d')
    else
      raise "Not a date: #{raw_date}"
    end
  end

  def coerce_end_date(raw_date)
    return if raw_date.nil?

    if raw_date =~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?$/
      Date.strptime(raw_date, '%Y-%m-%d')
    elsif raw_date =~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]?$/
      year, month = raw_date.match(/([0-9][0-9][0-9][0-9])-([0-9][0-9]?)/).captures
      Date.civil(year.to_i, month.to_i, -1)
    elsif raw_date =~ /^[0-9][0-9][0-9][0-9]$/
      Date.civil(raw_date.to_i, -1, -1)
    else
      raise "Not a date: #{raw_date}"
    end
  end

end

#coerce_begin_date(raw_date) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'backend/app/model/date_calculator.rb', line 100

def coerce_begin_date(raw_date)
  return if raw_date.nil?

  if raw_date =~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?$/
    Date.strptime(raw_date, '%Y-%m-%d')
  elsif raw_date =~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]?$/
    Date.strptime("#{raw_date}-01", '%Y-%m-%d')
  elsif raw_date =~ /^[0-9][0-9][0-9][0-9]$/
    Date.strptime("#{raw_date}-01-01", '%Y-%m-%d')
  else
    raise "Not a date: #{raw_date}"
  end
end

#coerce_end_date(raw_date) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'backend/app/model/date_calculator.rb', line 114

def coerce_end_date(raw_date)
  return if raw_date.nil?

  if raw_date =~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?$/
    Date.strptime(raw_date, '%Y-%m-%d')
  elsif raw_date =~ /^[0-9][0-9][0-9][0-9]-[0-9][0-9]?$/
    year, month = raw_date.match(/([0-9][0-9][0-9][0-9])-([0-9][0-9]?)/).captures
    Date.civil(year.to_i, month.to_i, -1)
  elsif raw_date =~ /^[0-9][0-9][0-9][0-9]$/
    Date.civil(raw_date.to_i, -1, -1)
  else
    raise "Not a date: #{raw_date}"
  end
end

#to_hashObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'backend/app/model/date_calculator.rb', line 81

def to_hash
  {
    :object => {:uri => @root_object.uri,
                :jsonmodel_type => @root_object.class.my_jsonmodel.record_type,
                :title => @root_object.title || @root_object.display_string,
                :id => @root_object.id},
    :resource => @resource ? {:uri => @resource.uri, :title => @resource.title} : nil,

    :label => @label,

    :min_begin_date => @min_begin_date,
    :min_begin => @min_begin,
    :max_end_date => @max_end_date,
    :max_end => @max_end
  }
end