trials 0.0.7 → 0.1.0
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 +4 -4
- data/lib/trials/extensions/array.rb +5 -0
- data/lib/trials/extensions/hash.rb +48 -0
- data/lib/trials/utils/h_array.rb +52 -0
- data/lib/trials.rb +3 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 574015ebe59522f5c8269f49baa0f94c8ffd4510a992f75b03b3db0d02e4aede
|
4
|
+
data.tar.gz: 2b2d695cb64e9a2da338726f61d206e46b3b9bf693f57a7395a1185a8a58cc6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59eb34e02c11c2f3b26f00597eae0b3c55ba0c7299203928a69da64f64ee287a83278ab8f5ac9fe50ef73e39635ed7d6500b6d4b72eb3f98c86b8b35383ad624
|
7
|
+
data.tar.gz: 413eea94a00660664f744f3baca2e891ce5db54f4548437d110da0df99405f3e51f439b15a4dc5663037bf5ad5eadf936b26fc110335f1979a579c658c84d82e
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class Hash
|
2
|
+
def to_os(n = nil)
|
3
|
+
n ||= self
|
4
|
+
|
5
|
+
case n
|
6
|
+
when Array
|
7
|
+
n.map { |x| to_os(x) }
|
8
|
+
when NilClass, Hash
|
9
|
+
n
|
10
|
+
.map { |k, v| [k, to_os(v)] }
|
11
|
+
.to_h
|
12
|
+
.then { |x| OpenStruct.new(x) }
|
13
|
+
else
|
14
|
+
n
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def sanitize_value(key, type, date_format: nil, datetime_format: nil)
|
19
|
+
self.merge(
|
20
|
+
key => or_nil do
|
21
|
+
case type
|
22
|
+
when :date
|
23
|
+
Date.strptime(*[self.dig(key), date_format].compact)
|
24
|
+
when :datetime
|
25
|
+
DateTime.parse(*[self.dig(key), datetime_format].compact)
|
26
|
+
when :integer, :int
|
27
|
+
self.dig(key).to_i
|
28
|
+
when :float
|
29
|
+
self.dig(key).to_f
|
30
|
+
when :string
|
31
|
+
self.dig(key).to_s
|
32
|
+
when :present?
|
33
|
+
self.dig(key).present?
|
34
|
+
when :blank?
|
35
|
+
self.dig(key).blank?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
def sanitize(schema, date_format: '%Y-%m-%d')
|
42
|
+
cp = self.dup
|
43
|
+
schema.each do |key, type|
|
44
|
+
cp = cp.sanitize_value(key, type, date_format: date_format)
|
45
|
+
end
|
46
|
+
cp
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class HArray < Array
|
2
|
+
def hashes
|
3
|
+
self
|
4
|
+
end
|
5
|
+
|
6
|
+
def count_by
|
7
|
+
hashes
|
8
|
+
.group_by { |x| yield(x) }
|
9
|
+
.map { |k, v| [k, v.length] }
|
10
|
+
.to_h
|
11
|
+
end
|
12
|
+
|
13
|
+
def rename_keys(**renames)
|
14
|
+
renames.each do |orig_key, new_key|
|
15
|
+
hashes.map do |h|
|
16
|
+
new_h = h.dup
|
17
|
+
new_h[new_key] = new_h.delete(orig_key)
|
18
|
+
new_h
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def uniq_keys
|
24
|
+
hashes.flat_map(&:keys).uniq.compact
|
25
|
+
end
|
26
|
+
|
27
|
+
def merge(*groups, key:, join: :inner)
|
28
|
+
groups = [self, *groups].map do |group|
|
29
|
+
group
|
30
|
+
.map { |h| [h.dig(key), h] }
|
31
|
+
.to_h
|
32
|
+
end
|
33
|
+
|
34
|
+
keys = begin
|
35
|
+
case join
|
36
|
+
when :inner
|
37
|
+
groups.map(&:keys).reduce(&:&)
|
38
|
+
when :all
|
39
|
+
groups.flat_map(&:keys).uniq
|
40
|
+
when :first
|
41
|
+
groups.first.keys
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
keys.map do |key|
|
46
|
+
groups
|
47
|
+
.map { |g| g.dig(key) }
|
48
|
+
.compact
|
49
|
+
.reduce(&:merge)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/trials.rb
CHANGED
@@ -32,5 +32,8 @@ require_relative 'trials/utils/benchmarking'
|
|
32
32
|
require_relative 'trials/utils/rollbar'
|
33
33
|
require_relative 'trials/utils/aws'
|
34
34
|
require_relative 'trials/utils/google_drive'
|
35
|
+
require_relative 'trials/utils/h_array'
|
35
36
|
require_relative 'trials/data_handling/addresses'
|
36
37
|
require_relative 'trials/data_handling/names'
|
38
|
+
require_relative 'trials/extensions/hash'
|
39
|
+
require_relative 'trials/extensions/array'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- grahamotte
|
@@ -287,11 +287,14 @@ files:
|
|
287
287
|
- lib/trials.rb
|
288
288
|
- lib/trials/data_handling/addresses.rb
|
289
289
|
- lib/trials/data_handling/names.rb
|
290
|
+
- lib/trials/extensions/array.rb
|
291
|
+
- lib/trials/extensions/hash.rb
|
290
292
|
- lib/trials/utils/aws.rb
|
291
293
|
- lib/trials/utils/benchmarking.rb
|
292
294
|
- lib/trials/utils/csvs.rb
|
293
295
|
- lib/trials/utils/files.rb
|
294
296
|
- lib/trials/utils/google_drive.rb
|
297
|
+
- lib/trials/utils/h_array.rb
|
295
298
|
- lib/trials/utils/hashes.rb
|
296
299
|
- lib/trials/utils/jsons.rb
|
297
300
|
- lib/trials/utils/logging.rb
|