cotcube-helpers 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5e22624f57015916268dc4ca26ec98e2d759150ab9dc88dc3b5ceda93248148
4
- data.tar.gz: 73628320f993e73c83423dded2880f44412174c8d1cd4dd201ace3f2c6c6e41e
3
+ metadata.gz: 22fa00c18900f6c955d12685727b76560b6a426007d644f489f585d5f1488649
4
+ data.tar.gz: ffa3ca8962565929835410d50a60fb878ee782db074bcb7392139acf5bd946b9
5
5
  SHA512:
6
- metadata.gz: 4b7b9051f22418e8a5a17245b1a410e284bf279787f1f9d279f58083f93a588c916926badc2b2834361241d874126a3af11c36805d20afe5b34049934c16cb6b
7
- data.tar.gz: fd0dbfb22b5b7631c5ee220de6551e1adfe9a38311afe85918598fdf86d9dd50e1028663b633c8a675853238767455504bdfa9163a466ef3e362950aa44cf064
6
+ metadata.gz: 48f5a7982b6ab4bc14cc123dbe49b12ad7e2aa5a664696244f808bb1293b31721315a9ceb098a6b8c57e0f8da6d7940d3becc6296c4184adf53f6ee3faf44d8c
7
+ data.tar.gz: 3d640f4fac8b7e2a84985bf00f09a3a34862b1bebf78fac2f03326a72133ca32e4d1f710ead8995ebbb1e50a0f8a24ad3f70b1fc5dc593b9d2e30ab864ad6ebc
@@ -1,3 +1,8 @@
1
+ ## 0.1.5 (January 02, 2021)
2
+ - applied new datetime helper to Range#to_time_intervals
3
+ - added new DateTime extension, containing 'to_seconds_since_sunday_morning'
4
+ - added #select_within to array_ext
5
+
1
6
  ## 0.1.4 (December 27, 2020)
2
7
  - applied cops
3
8
  - added README for reduce; minor changes
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -12,6 +12,7 @@ require_relative 'cotcube-helpers/enum_ext'
12
12
  require_relative 'cotcube-helpers/hash_ext'
13
13
  require_relative 'cotcube-helpers/range_ext'
14
14
  require_relative 'cotcube-helpers/string_ext'
15
+ require_relative 'cotcube-helpers/datetime_ext'
15
16
  require_relative 'cotcube-helpers/subpattern'
16
17
  require_relative 'cotcube-helpers/parallelize'
17
18
  require_relative 'cotcube-helpers/simple_output'
@@ -53,4 +53,25 @@ class Array
53
53
  block.call(self[i - 2], self[i - 1], self[i])
54
54
  end.compact
55
55
  end
56
+
57
+ # selects all elements from array that fit in given ranges.
58
+ # if :attr is given, selects all elements, where elem[:attr] fit
59
+ # raises if elem.first[attr].nil?
60
+ def select_within(ranges:, attr: nil, &block)
61
+ unless attr.nil? || first[attr]
62
+ raise ArgumentError,
63
+ "At least first element of Array '#{first}' does not contain attr '#{attr}'!"
64
+ end
65
+ raise ArgumentError, 'Ranges should be an Array or, more precisely, respond_to :map' unless ranges.respond_to? :map
66
+ raise ArgumentError, 'Each range in :ranges should respond to .include!' unless ranges.map do |x|
67
+ x.respond_to? :include?
68
+ end.reduce(:&)
69
+
70
+ select do |el|
71
+ value = attr.nil? ? el : el[attr]
72
+ ranges.map do |range|
73
+ range.include?(block.nil? ? value : block.call(value))
74
+ end.reduce(:|)
75
+ end
76
+ end
56
77
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Monkey patching the Ruby class DateTime
4
+ class DateTime
5
+ # based on the fact that sunday is 'wday 0' plus that trading week starts
6
+ # sunday 0:00 (as trading starts sunday 5pm CT to fit tokyo monday morning)
7
+ def to_seconds_since_sunday_morning
8
+ wday * 86_400 + hour * 3600 + min * 60 + sec
9
+ end
10
+
11
+ alias to_sssm to_seconds_since_sunday_morning
12
+ end
@@ -43,6 +43,7 @@ class Range
43
43
  end
44
44
 
45
45
  # sub-day is checked for DST and filtered along provided ranges
46
+ # noinspection RubyNilAnalysis
46
47
  starting_with_dst = result.first.dst?
47
48
 
48
49
  # The following lambda is completely misplaces here.
@@ -63,7 +64,6 @@ class Range
63
64
  end
64
65
  convert_to_sec_since.call('9:00a.m - 5:00p.m.')
65
66
 
66
- seconds_since_sunday_morning = ->(x) { x.wday * 86_400 + x.hour * 3600 + x.min * 60 + x.sec }
67
67
  ranges ||= [
68
68
  61_200...144_000, # Sun 5pm .. Mon 4pm
69
69
  147_600...230_400, # Mon 5pm .. Tue 4pm
@@ -82,8 +82,7 @@ class Range
82
82
  time
83
83
  end
84
84
  end
85
- return result if ranges.empty?
86
85
 
87
- result.select { |x| ranges.map { |r| r.include? seconds_since_sunday_morning.call(x) }.reduce(:|) }
86
+ result.select_within(ranges: ranges) { |x| x.to_datetime.to_seconds_since_monday_morning }
88
87
  end
89
88
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cotcube-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin L. Tischendorf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-27 00:00:00.000000000 Z
11
+ date: 2021-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -82,6 +82,7 @@ files:
82
82
  - cotcube-helpers.gemspec
83
83
  - lib/cotcube-helpers.rb
84
84
  - lib/cotcube-helpers/array_ext.rb
85
+ - lib/cotcube-helpers/datetime_ext.rb
85
86
  - lib/cotcube-helpers/enum_ext.rb
86
87
  - lib/cotcube-helpers/hash_ext.rb
87
88
  - lib/cotcube-helpers/input.rb