dates_resolver 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/dates_resolver.rb +79 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 949a2ae46104af1346560fa4546127f71be41a0c
4
+ data.tar.gz: 529526332d42f289299eb9fcd208e1c8a6ade012
5
+ SHA512:
6
+ metadata.gz: 142ec1a11365c5130d61c635956397c2b40af0fd339fb020d301219b33ebc074410c3e1b9ef2c59fab0e8a3db1debd822fe2139bc13ea6c68291e7851d9fbf34
7
+ data.tar.gz: 9ddaa8f323d56afdcf9aa3546ba4ab02d965fa3344c5db052de8423eb079ffa4f8d483aba876794e35da5efdd9618d2b9bc5524a2970aa569f8322437767972d
@@ -0,0 +1,79 @@
1
+ require 'logger'
2
+ # accepts Collection of ActiveRecord Objects with attributes start date and end date
3
+ # returns a collection of ActiveRecord Objects with resolved date ranges
4
+ class DatesResolver
5
+ def initialize(collection_of_objects=[])
6
+ @collection = collection_of_objects
7
+ @start_dates = @collection.map(&:start_date).uniq
8
+ @end_dates = @collection.map(&:end_date).uniq
9
+ @dates = (@start_dates + @end_dates).uniq.sort!
10
+ @new_collection =[]
11
+ @logger = Logger.new(STDOUT)
12
+ end
13
+
14
+ def resolve
15
+
16
+ @collection.each do |obj|
17
+ new_obj = obj.clone
18
+ @dates.each_with_index do |date, i|
19
+ if (@end_dates.include?(date) && !(@start_dates.include?(date)))
20
+ #if not back to back also
21
+ (@logger.debug("not fit to be resolved: date is present only in end_dates") && next)
22
+ end
23
+
24
+ if !(date >= obj.start_date)
25
+ (@logger.debug("not fit to be resolved: date is lesser than current obj date") && next)
26
+ end
27
+
28
+ #hack for 1day overlaps
29
+ #TODO: patch it
30
+ if (@end_dates.include?(date) && @start_dates.include?(date))
31
+ start_date = date
32
+ end_date = date
33
+ next_date = date
34
+ increment_next_date(next_date)
35
+ else
36
+ # start date is set to be the date value in current iteration
37
+ start_date = date
38
+ next_date = @dates[i+1]
39
+ end_date = (if @start_dates.include?(next_date)
40
+ (next_date -1)
41
+ else
42
+ #seems the next coming up date is an end_date
43
+ increment_next_date(next_date)
44
+ next_date
45
+ end)
46
+ end
47
+
48
+ new_obj.start_date = start_date
49
+ new_obj.end_date = end_date
50
+ @new_collection << new_obj.clone
51
+ #resolve only 1 overlapping promotional end or exception at once.
52
+ #also halt the resolver if the max end date is achieved
53
+ break if (end_date == obj.end_date || end_date == @dates.last)
54
+ end
55
+ end
56
+ @new_collection.uniq
57
+ end
58
+
59
+ def add_and_sort_dates_collection(new_date,dates)
60
+ dates << new_date
61
+ dates.sort!.uniq!
62
+ end
63
+
64
+ def increment_next_date(next_date)
65
+ begin
66
+ if next_date != @dates.last
67
+ add_and_sort_dates_collection(next_date + 1, @dates)
68
+ add_and_sort_dates_collection(next_date + 1, @start_dates)
69
+ end
70
+ rescue NoMethodError => err
71
+ custom_val = "NEXT_DATE seems to be null"
72
+ #TODO: use inspect instead of to_json
73
+ #to show more clarity in the logs
74
+ @logger.fatal(custom_val +"\n" + err+ "\n" + e.to_json)
75
+ @logger.fatal(err.backtrace)
76
+ return true
77
+ end
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dates_resolver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - SathishAchilles
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: It consumes two ActiveRecords Date ranges that are overlapping and splits
14
+ them based on the overlaps.. returns the splits as ActiveRecords again
15
+ email: sathshh@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/dates_resolver.rb
21
+ homepage: http://rubygems.org/gems/dates_resolver
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A simple gem to resolve the overlapping date ranges..
45
+ test_files: []