ndr_support 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +14 -0
  3. data/.rubocop.yml +27 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +22 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +4 -0
  8. data/Guardfile +16 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +91 -0
  11. data/Rakefile +12 -0
  12. data/code_safety.yml +258 -0
  13. data/gemfiles/Gemfile.rails32 +6 -0
  14. data/gemfiles/Gemfile.rails32.lock +108 -0
  15. data/gemfiles/Gemfile.rails41 +6 -0
  16. data/gemfiles/Gemfile.rails41.lock +111 -0
  17. data/gemfiles/Gemfile.rails42 +6 -0
  18. data/gemfiles/Gemfile.rails42.lock +111 -0
  19. data/lib/ndr_support.rb +21 -0
  20. data/lib/ndr_support/array.rb +52 -0
  21. data/lib/ndr_support/concerns/working_days.rb +94 -0
  22. data/lib/ndr_support/date_and_time_extensions.rb +103 -0
  23. data/lib/ndr_support/daterange.rb +196 -0
  24. data/lib/ndr_support/fixnum/calculations.rb +15 -0
  25. data/lib/ndr_support/fixnum/julian_date_conversions.rb +14 -0
  26. data/lib/ndr_support/hash.rb +52 -0
  27. data/lib/ndr_support/integer.rb +12 -0
  28. data/lib/ndr_support/nil.rb +38 -0
  29. data/lib/ndr_support/ourdate.rb +97 -0
  30. data/lib/ndr_support/ourtime.rb +51 -0
  31. data/lib/ndr_support/regexp_range.rb +65 -0
  32. data/lib/ndr_support/safe_file.rb +185 -0
  33. data/lib/ndr_support/safe_path.rb +268 -0
  34. data/lib/ndr_support/string/cleaning.rb +136 -0
  35. data/lib/ndr_support/string/conversions.rb +137 -0
  36. data/lib/ndr_support/tasks.rb +1 -0
  37. data/lib/ndr_support/time/conversions.rb +13 -0
  38. data/lib/ndr_support/utf8_encoding.rb +72 -0
  39. data/lib/ndr_support/utf8_encoding/control_characters.rb +53 -0
  40. data/lib/ndr_support/utf8_encoding/force_binary.rb +44 -0
  41. data/lib/ndr_support/utf8_encoding/object_support.rb +31 -0
  42. data/lib/ndr_support/version.rb +5 -0
  43. data/lib/ndr_support/yaml/serialization_migration.rb +65 -0
  44. data/lib/tasks/audit_code.rake +423 -0
  45. data/ndr_support.gemspec +39 -0
  46. data/test/array_test.rb +20 -0
  47. data/test/concerns/working_days_test.rb +122 -0
  48. data/test/daterange_test.rb +194 -0
  49. data/test/fixnum/calculations_test.rb +28 -0
  50. data/test/hash_test.rb +84 -0
  51. data/test/integer_test.rb +14 -0
  52. data/test/nil_test.rb +40 -0
  53. data/test/ourdate_test.rb +27 -0
  54. data/test/ourtime_test.rb +27 -0
  55. data/test/regexp_range_test.rb +135 -0
  56. data/test/resources/filesystem_paths.yml +37 -0
  57. data/test/safe_file_test.rb +597 -0
  58. data/test/safe_path_test.rb +168 -0
  59. data/test/string/cleaning_test.rb +176 -0
  60. data/test/string/conversions_test.rb +353 -0
  61. data/test/test_helper.rb +41 -0
  62. data/test/time/conversions_test.rb +15 -0
  63. data/test/utf8_encoding/control_characters_test.rb +84 -0
  64. data/test/utf8_encoding/force_binary_test.rb +64 -0
  65. data/test/utf8_encoding_test.rb +170 -0
  66. data/test/yaml/serialization_test.rb +145 -0
  67. metadata +295 -0
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '..'
4
+
5
+ gem 'activerecord', '~> 3.2.18'
6
+ gem 'activesupport', '~> 3.2.18'
@@ -0,0 +1,108 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ ndr_support (3.0.0)
5
+ activerecord (>= 3.2.18, < 5.0.0)
6
+ activesupport (>= 3.2.18, < 5.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (3.2.22)
12
+ activesupport (= 3.2.22)
13
+ builder (~> 3.0.0)
14
+ activerecord (3.2.22)
15
+ activemodel (= 3.2.22)
16
+ activesupport (= 3.2.22)
17
+ arel (~> 3.0.2)
18
+ tzinfo (~> 0.3.29)
19
+ activesupport (3.2.22)
20
+ i18n (~> 0.6, >= 0.6.4)
21
+ multi_json (~> 1.0)
22
+ arel (3.0.3)
23
+ ast (2.0.0)
24
+ astrolabe (1.3.1)
25
+ parser (~> 2.2)
26
+ builder (3.0.4)
27
+ coderay (1.1.0)
28
+ docile (1.1.5)
29
+ ffi (1.9.10)
30
+ formatador (0.2.5)
31
+ guard (2.13.0)
32
+ formatador (>= 0.2.4)
33
+ listen (>= 2.7, <= 4.0)
34
+ lumberjack (~> 1.0)
35
+ nenv (~> 0.1)
36
+ notiffany (~> 0.0)
37
+ pry (>= 0.9.12)
38
+ shellany (~> 0.0)
39
+ thor (>= 0.18.1)
40
+ guard-compat (1.2.1)
41
+ guard-minitest (2.4.4)
42
+ guard-compat (~> 1.2)
43
+ minitest (>= 3.0)
44
+ guard-rubocop (1.2.0)
45
+ guard (~> 2.0)
46
+ rubocop (~> 0.20)
47
+ i18n (0.7.0)
48
+ json (1.8.3)
49
+ listen (3.0.3)
50
+ rb-fsevent (>= 0.9.3)
51
+ rb-inotify (>= 0.9)
52
+ lumberjack (1.0.9)
53
+ metaclass (0.0.4)
54
+ method_source (0.8.2)
55
+ minitest (5.7.0)
56
+ mocha (1.1.0)
57
+ metaclass (~> 0.0.1)
58
+ multi_json (1.11.2)
59
+ nenv (0.2.0)
60
+ notiffany (0.0.7)
61
+ nenv (~> 0.1)
62
+ shellany (~> 0.0)
63
+ parser (2.2.2.6)
64
+ ast (>= 1.1, < 3.0)
65
+ powerpack (0.1.1)
66
+ pry (0.10.1)
67
+ coderay (~> 1.1.0)
68
+ method_source (~> 0.8.1)
69
+ slop (~> 3.4)
70
+ rainbow (2.0.0)
71
+ rake (10.4.2)
72
+ rb-fsevent (0.9.5)
73
+ rb-inotify (0.9.5)
74
+ ffi (>= 0.5.0)
75
+ rubocop (0.32.1)
76
+ astrolabe (~> 1.3)
77
+ parser (>= 2.2.2.5, < 3.0)
78
+ powerpack (~> 0.1)
79
+ rainbow (>= 1.99.1, < 3.0)
80
+ ruby-progressbar (~> 1.4)
81
+ ruby-progressbar (1.7.5)
82
+ shellany (0.0.1)
83
+ simplecov (0.10.0)
84
+ docile (~> 1.1.0)
85
+ json (~> 1.8)
86
+ simplecov-html (~> 0.10.0)
87
+ simplecov-html (0.10.0)
88
+ slop (3.6.0)
89
+ terminal-notifier-guard (1.6.4)
90
+ thor (0.19.1)
91
+ tzinfo (0.3.44)
92
+
93
+ PLATFORMS
94
+ ruby
95
+
96
+ DEPENDENCIES
97
+ activerecord (~> 3.2.18)
98
+ activesupport (~> 3.2.18)
99
+ bundler (~> 1.7)
100
+ guard
101
+ guard-minitest
102
+ guard-rubocop
103
+ minitest (>= 5.0.0)
104
+ mocha (~> 1.1)
105
+ ndr_support!
106
+ rake (~> 10.0)
107
+ simplecov
108
+ terminal-notifier-guard
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '..'
4
+
5
+ gem 'activerecord', '~> 4.1.0'
6
+ gem 'activesupport', '~> 4.1.0'
@@ -0,0 +1,111 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ ndr_support (3.0.0)
5
+ activerecord (>= 3.2.18, < 5.0.0)
6
+ activesupport (>= 3.2.18, < 5.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.1.12)
12
+ activesupport (= 4.1.12)
13
+ builder (~> 3.1)
14
+ activerecord (4.1.12)
15
+ activemodel (= 4.1.12)
16
+ activesupport (= 4.1.12)
17
+ arel (~> 5.0.0)
18
+ activesupport (4.1.12)
19
+ i18n (~> 0.6, >= 0.6.9)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.1)
23
+ tzinfo (~> 1.1)
24
+ arel (5.0.1.20140414130214)
25
+ ast (2.0.0)
26
+ astrolabe (1.3.1)
27
+ parser (~> 2.2)
28
+ builder (3.2.2)
29
+ coderay (1.1.0)
30
+ docile (1.1.5)
31
+ ffi (1.9.10)
32
+ formatador (0.2.5)
33
+ guard (2.13.0)
34
+ formatador (>= 0.2.4)
35
+ listen (>= 2.7, <= 4.0)
36
+ lumberjack (~> 1.0)
37
+ nenv (~> 0.1)
38
+ notiffany (~> 0.0)
39
+ pry (>= 0.9.12)
40
+ shellany (~> 0.0)
41
+ thor (>= 0.18.1)
42
+ guard-compat (1.2.1)
43
+ guard-minitest (2.4.4)
44
+ guard-compat (~> 1.2)
45
+ minitest (>= 3.0)
46
+ guard-rubocop (1.2.0)
47
+ guard (~> 2.0)
48
+ rubocop (~> 0.20)
49
+ i18n (0.7.0)
50
+ json (1.8.3)
51
+ listen (3.0.3)
52
+ rb-fsevent (>= 0.9.3)
53
+ rb-inotify (>= 0.9)
54
+ lumberjack (1.0.9)
55
+ metaclass (0.0.4)
56
+ method_source (0.8.2)
57
+ minitest (5.7.0)
58
+ mocha (1.1.0)
59
+ metaclass (~> 0.0.1)
60
+ nenv (0.2.0)
61
+ notiffany (0.0.7)
62
+ nenv (~> 0.1)
63
+ shellany (~> 0.0)
64
+ parser (2.2.2.6)
65
+ ast (>= 1.1, < 3.0)
66
+ powerpack (0.1.1)
67
+ pry (0.10.1)
68
+ coderay (~> 1.1.0)
69
+ method_source (~> 0.8.1)
70
+ slop (~> 3.4)
71
+ rainbow (2.0.0)
72
+ rake (10.4.2)
73
+ rb-fsevent (0.9.5)
74
+ rb-inotify (0.9.5)
75
+ ffi (>= 0.5.0)
76
+ rubocop (0.32.1)
77
+ astrolabe (~> 1.3)
78
+ parser (>= 2.2.2.5, < 3.0)
79
+ powerpack (~> 0.1)
80
+ rainbow (>= 1.99.1, < 3.0)
81
+ ruby-progressbar (~> 1.4)
82
+ ruby-progressbar (1.7.5)
83
+ shellany (0.0.1)
84
+ simplecov (0.10.0)
85
+ docile (~> 1.1.0)
86
+ json (~> 1.8)
87
+ simplecov-html (~> 0.10.0)
88
+ simplecov-html (0.10.0)
89
+ slop (3.6.0)
90
+ terminal-notifier-guard (1.6.4)
91
+ thor (0.19.1)
92
+ thread_safe (0.3.5)
93
+ tzinfo (1.2.2)
94
+ thread_safe (~> 0.1)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ activerecord (~> 4.1.0)
101
+ activesupport (~> 4.1.0)
102
+ bundler (~> 1.7)
103
+ guard
104
+ guard-minitest
105
+ guard-rubocop
106
+ minitest (>= 5.0.0)
107
+ mocha (~> 1.1)
108
+ ndr_support!
109
+ rake (~> 10.0)
110
+ simplecov
111
+ terminal-notifier-guard
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '..'
4
+
5
+ gem 'activerecord', '~> 4.2.0'
6
+ gem 'activesupport', '~> 4.2.0'
@@ -0,0 +1,111 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ ndr_support (3.0.0)
5
+ activerecord (>= 3.2.18, < 5.0.0)
6
+ activesupport (>= 3.2.18, < 5.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.2.3)
12
+ activesupport (= 4.2.3)
13
+ builder (~> 3.1)
14
+ activerecord (4.2.3)
15
+ activemodel (= 4.2.3)
16
+ activesupport (= 4.2.3)
17
+ arel (~> 6.0)
18
+ activesupport (4.2.3)
19
+ i18n (~> 0.7)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.3, >= 0.3.4)
23
+ tzinfo (~> 1.1)
24
+ arel (6.0.2)
25
+ ast (2.0.0)
26
+ astrolabe (1.3.1)
27
+ parser (~> 2.2)
28
+ builder (3.2.2)
29
+ coderay (1.1.0)
30
+ docile (1.1.5)
31
+ ffi (1.9.10)
32
+ formatador (0.2.5)
33
+ guard (2.13.0)
34
+ formatador (>= 0.2.4)
35
+ listen (>= 2.7, <= 4.0)
36
+ lumberjack (~> 1.0)
37
+ nenv (~> 0.1)
38
+ notiffany (~> 0.0)
39
+ pry (>= 0.9.12)
40
+ shellany (~> 0.0)
41
+ thor (>= 0.18.1)
42
+ guard-compat (1.2.1)
43
+ guard-minitest (2.4.4)
44
+ guard-compat (~> 1.2)
45
+ minitest (>= 3.0)
46
+ guard-rubocop (1.2.0)
47
+ guard (~> 2.0)
48
+ rubocop (~> 0.20)
49
+ i18n (0.7.0)
50
+ json (1.8.3)
51
+ listen (3.0.3)
52
+ rb-fsevent (>= 0.9.3)
53
+ rb-inotify (>= 0.9)
54
+ lumberjack (1.0.9)
55
+ metaclass (0.0.4)
56
+ method_source (0.8.2)
57
+ minitest (5.7.0)
58
+ mocha (1.1.0)
59
+ metaclass (~> 0.0.1)
60
+ nenv (0.2.0)
61
+ notiffany (0.0.7)
62
+ nenv (~> 0.1)
63
+ shellany (~> 0.0)
64
+ parser (2.2.2.6)
65
+ ast (>= 1.1, < 3.0)
66
+ powerpack (0.1.1)
67
+ pry (0.10.1)
68
+ coderay (~> 1.1.0)
69
+ method_source (~> 0.8.1)
70
+ slop (~> 3.4)
71
+ rainbow (2.0.0)
72
+ rake (10.4.2)
73
+ rb-fsevent (0.9.5)
74
+ rb-inotify (0.9.5)
75
+ ffi (>= 0.5.0)
76
+ rubocop (0.32.1)
77
+ astrolabe (~> 1.3)
78
+ parser (>= 2.2.2.5, < 3.0)
79
+ powerpack (~> 0.1)
80
+ rainbow (>= 1.99.1, < 3.0)
81
+ ruby-progressbar (~> 1.4)
82
+ ruby-progressbar (1.7.5)
83
+ shellany (0.0.1)
84
+ simplecov (0.10.0)
85
+ docile (~> 1.1.0)
86
+ json (~> 1.8)
87
+ simplecov-html (~> 0.10.0)
88
+ simplecov-html (0.10.0)
89
+ slop (3.6.0)
90
+ terminal-notifier-guard (1.6.4)
91
+ thor (0.19.1)
92
+ thread_safe (0.3.5)
93
+ tzinfo (1.2.2)
94
+ thread_safe (~> 0.1)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ activerecord (~> 4.2.0)
101
+ activesupport (~> 4.2.0)
102
+ bundler (~> 1.7)
103
+ guard
104
+ guard-minitest
105
+ guard-rubocop
106
+ minitest (>= 5.0.0)
107
+ mocha (~> 1.1)
108
+ ndr_support!
109
+ rake (~> 10.0)
110
+ simplecov
111
+ terminal-notifier-guard
@@ -0,0 +1,21 @@
1
+ require 'ndr_support/array'
2
+ require 'ndr_support/daterange'
3
+ require 'ndr_support/fixnum/calculations'
4
+ require 'ndr_support/hash'
5
+ require 'ndr_support/integer'
6
+ require 'ndr_support/nil'
7
+ require 'ndr_support/ourdate'
8
+ require 'ndr_support/ourtime'
9
+ require 'ndr_support/regexp_range'
10
+ require 'ndr_support/safe_file'
11
+ require 'ndr_support/safe_path'
12
+ require 'ndr_support/string/cleaning'
13
+ require 'ndr_support/string/conversions'
14
+ require 'ndr_support/time/conversions'
15
+ require 'ndr_support/utf8_encoding'
16
+ require 'ndr_support/version'
17
+ require 'ndr_support/yaml/serialization_migration'
18
+
19
+ module NdrSupport
20
+ # Your code goes here...
21
+ end
@@ -0,0 +1,52 @@
1
+ class Array
2
+ # A utility method to return those elements in an array where the item in a
3
+ # corresponding array in true (matching by index)
4
+ def values_matching(a)
5
+ return [] unless a.respond_to?(:[])
6
+ result = []
7
+ each_with_index { |x, i| result << x if a[i] }
8
+ result
9
+ end
10
+
11
+ # Returns the smallest, i.e. least, non-nil element (can be used for any
12
+ # type of object that supports the <=> operator, including dates)
13
+ def smallest
14
+ # self.compact.sort.first
15
+ self.compact.min
16
+ end
17
+
18
+ # Returns the biggest, i.e. greatest, non-nil element
19
+ def biggest
20
+ # self.compact.sort.last
21
+ self.compact.max
22
+ end
23
+
24
+ # Flattens range objects within an array to allow include? to work within
25
+ # the array and within the ranges within the array.
26
+ def ranges_include?(value)
27
+ any? { |range| Array(range).include?(value) }
28
+ end
29
+
30
+ # Finds all the permutations of the array:
31
+ #
32
+ # [1,2,3].permutations #=> [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]
33
+ # [3,3].permutations #=> [3,3], [3,3]
34
+ #
35
+ def permutations
36
+ return [self] if length == 1
37
+
38
+ orders = []
39
+ positions = (0...length).to_a
40
+
41
+ # Finding the permutations with a basic array of digits
42
+ positions.each do |position|
43
+ (positions - [position]).permutations.each do |permutation|
44
+ orders << permutation.unshift(position)
45
+ end
46
+ end
47
+
48
+ # We subsitute in our original elements. This prevents duplicate
49
+ # elements from causing problems, and allows the [3,3] example to work.
50
+ orders.map { |order| order.map { |index| self[index] } }
51
+ end
52
+ end
@@ -0,0 +1,94 @@
1
+ require 'active_support/time'
2
+
3
+ # This module contains logic for #working_days_until, #weekday?, and #public_holiday?.
4
+ module WorkingDays
5
+ WEEK_DAYS = 1..5
6
+ HOLIDAYS = [ # Sourced from https://www.gov.uk/bank-holidays
7
+ # 2012
8
+ '2012-01-02', # Monday - New Year's Day (substitute day)
9
+ '2012-04-06', # Friday - Good Friday
10
+ '2012-04-09', # Monday - Easter Monday
11
+ '2012-05-07', # Monday - Early May bank holiday
12
+ '2012-06-04', # Monday - Spring bank holiday (substitute day)
13
+ '2012-06-05', # Tuesday - Queen's Diamond Jubilee (extra bank holiday)
14
+ '2012-08-27', # Monday - Summer bank holiday
15
+ '2012-12-25', # Tuesday - Christmas Day
16
+ '2012-12-26', # Wednesday - Boxing Day
17
+ # 2013
18
+ '2013-01-01', # Tuesday - New Year's Day
19
+ '2013-03-29', # Friday - Good Friday
20
+ '2013-04-01', # Monday - Easter Monday
21
+ '2013-05-06', # Monday - Early May bank holiday
22
+ '2013-05-27', # Monday - Spring bank holiday
23
+ '2013-08-26', # Monday - Summer bank holiday
24
+ '2013-12-25', # Wednesday - Christmas Day
25
+ '2013-12-26', # Thursday - Boxing Day
26
+ # 2014
27
+ '2014-01-01', # Wednesday - New Year's Day
28
+ '2014-04-18', # Friday - Good Friday
29
+ '2014-04-21', # Monday - Easter Monday
30
+ '2014-05-05', # Monday - Early May bank holiday
31
+ '2014-05-26', # Monday - Spring bank holiday
32
+ '2014-08-25', # Monday - Summer bank holiday
33
+ '2014-12-25', # Thursday - Christmas Day
34
+ '2014-12-26', # Friday - Boxing Day
35
+ # 2015
36
+ '2015-01-01', # Thursday - New Year's Day
37
+ '2015-04-03', # Friday - Good Friday
38
+ '2015-04-06', # Monday - Easter Monday
39
+ '2015-05-04', # Monday - Early May bank holiday
40
+ '2015-05-25', # Monday - Spring bank holiday
41
+ '2015-08-31', # Monday - Summer bank holiday
42
+ '2015-12-25', # Friday - Christmas Day
43
+ '2015-12-28', # Monday - Boxing Day (substitute day)
44
+ # 2016
45
+ '2016-01-01', # Friday - New Year's Day
46
+ '2016-03-25', # Friday - Good Friday
47
+ '2016-03-28', # Monday - Easter Monday
48
+ '2016-05-02', # Monday - Early May bank holiday
49
+ '2016-05-30', # Monday - Spring bank holiday
50
+ '2016-08-29', # Monday - Summer bank holiday
51
+ '2016-12-26', # Monday - Boxing Day
52
+ '2016-12-27', # Tuesday - Christmas Day (substitute day)
53
+ ].map { |str| Date.parse(str) }
54
+
55
+ # How many complete working days there are until the given
56
+ # `other`. Returns negative number if `other` is earlier.
57
+ def working_days_until(other)
58
+ return -other.working_days_until(self) if other < self
59
+
60
+ whole_days_to(other).count do |day|
61
+ day.weekday? && !day.public_holiday?
62
+ end
63
+ end
64
+
65
+ # How many complete weekdays there are until the given
66
+ # `other`. Returns negative number if `other` is earlier.
67
+ def weekdays_until(other)
68
+ return -other.weekdays_until(self) if other < self
69
+ whole_days_to(other).count(&:weekday?)
70
+ end
71
+
72
+ # Is this a weekday?
73
+ def weekday?
74
+ WEEK_DAYS.include? wday
75
+ end
76
+
77
+ # Is this a public holiday (in England / Wales)?
78
+ def public_holiday?
79
+ HOLIDAYS.include? to_date
80
+ end
81
+
82
+ private
83
+
84
+ def whole_days_to(other)
85
+ [self].tap do |days|
86
+ loop do
87
+ next_day = days.last + 1.day
88
+ next_day <= other ? days.push(next_day) : break
89
+ end
90
+
91
+ days.shift # Drop `self` off the front
92
+ end
93
+ end
94
+ end