Class: Gitlab::ImportExport::Project::Sample::DateCalculator

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/import_export/project/sample/date_calculator.rb

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(dates) ⇒ DateCalculator

Returns a new instance of DateCalculator.


10
11
12
13
14
15
# File 'lib/gitlab/import_export/project/sample/date_calculator.rb', line 10

def initialize(dates)
  @dates = dates.dup
  @dates.compact!
  @dates.sort!
  @dates.map! { |date| date.to_time.to_f }
end

Instance Method Details

#calculate_by_closest_date_to_average(date) ⇒ Object


27
28
29
30
31
# File 'lib/gitlab/import_export/project/sample/date_calculator.rb', line 27

def calculate_by_closest_date_to_average(date)
  return date unless closest_date_to_average && closest_date_to_average < Time.current

  date + (Time.current - closest_date_to_average).seconds
end

#closest_date_to_averageObject


17
18
19
20
21
22
23
24
25
# File 'lib/gitlab/import_export/project/sample/date_calculator.rb', line 17

def closest_date_to_average
  strong_memoize(:closest_date_to_average) do
    next if @dates.empty?

    average_date = (@dates.first + @dates.last) / 2.0
    closest_date = @dates.min_by { |date| (date - average_date).abs }
    Time.zone.at(closest_date)
  end
end