Class: DatesResolver::AR

Inherits:
Object
  • Object
show all
Defined in:
lib/dates_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection_of_objects = []) ⇒ AR

Returns a new instance of AR.



7
8
9
10
11
12
13
14
# File 'lib/dates_resolver.rb', line 7

def initialize(collection_of_objects=[])
  @collection = collection_of_objects
  @start_dates = @collection.map(&:start_date).uniq
  @end_dates = @collection.map(&:end_date).uniq
  @dates = (@start_dates + @end_dates).uniq.sort!
  @new_collection =[]
  @logger = Logger.new(STDOUT)
end

Instance Method Details

#resolveObject



16
17
18
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
# File 'lib/dates_resolver.rb', line 16

def resolve
  @collection.each do |obj|
    new_obj = obj.clone
    @dates.each_with_index do |date, i|
      if (@end_dates.include?(date) && !(@start_dates.include?(date)))
        #if not back to back also
        (@logger.debug("not fit to be resolved: date is present only in end_dates") && next)
      end

      if !(date >= obj.start_date)
        (@logger.debug("not fit to be resolved: date is lesser than current obj date") && next)
      end

      #hack for 1day overlaps
      #TODO: patch it
      if (@end_dates.include?(date) && @start_dates.include?(date))
        start_date = date
        end_date = date
        next_date = date
        increment_next_date(next_date)
      else
        # start date is set to be the date value in current iteration
        start_date = date
        next_date = @dates[i+1]
        end_date = (if @start_dates.include?(next_date)
        (next_date -1)
      else
        #seems the next coming up date is an end_date
        increment_next_date(next_date)
        next_date
      end)
    end

    new_obj.start_date = start_date
    new_obj.end_date = end_date
    @new_collection << new_obj.clone
    #resolve only 1 overlapping promotional end or exception at once.
    #also halt the resolver if the max end date is achieved
    break if (end_date == obj.end_date || end_date == @dates.last)
  end
end
@new_collection.uniq
end