simple_ext 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_ext/array.rb +1 -0
- data/lib/simple_ext/array/values.rb +75 -0
- data/lib/simple_ext/hash.rb +1 -0
- data/lib/simple_ext/hash/check.rb +21 -0
- data/lib/simple_ext/hash/except.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 941e17d10ecabc60c70b538a655fc9fca58ce360e677ccff5beb2f212faa2b44
|
4
|
+
data.tar.gz: 2cb3d6c6cf0492341a3a5d5d2e6526f68e5bd940b1b13f7ce7c0ba0dd430ca20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5c68a0c8455c85ecdc883d96275ee8e9d56ca816b5363dc88a8b98a973814b1c23a817d93de41a080b9b584040e28579a20e7eb7e75b843f2b21b5582380472
|
7
|
+
data.tar.gz: 320998a91180545b68366a973d7dd38b2e8dbc6ebc7bbd5563f2b4c80f7e6b823b819c4d66df8c2f177920f060848f03dd385eebde43535b4fe36e461e56e326
|
data/lib/simple_ext/array.rb
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Array
|
4
|
+
|
5
|
+
##
|
6
|
+
# Check if any element is nil in array
|
7
|
+
# [1, 2, ' ', 4].any_nil? => false
|
8
|
+
# [1, nil, 2, 4].any_nil? => true
|
9
|
+
def any_nil?
|
10
|
+
any?(&:nil?)
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# Check if any element is empty in array
|
15
|
+
# [1, 2, ' ', 4].any_empty? => false
|
16
|
+
# [1, nil, 2, 4].any_empty? => false
|
17
|
+
# [1, '', 3, 4].any_empty? => true
|
18
|
+
def any_empty?
|
19
|
+
any? { |e| e.respond_to?(:empty?) && e.empty? }
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# Check if any element is blank in array
|
24
|
+
# [1, 2, ' ', 4].any_blank? => true
|
25
|
+
# [1, nil, 2, 4].any_blank? => true
|
26
|
+
# [1, '', 3, 4].any_blank? => true
|
27
|
+
# [1, 2, 3, 4].any_blank? => false
|
28
|
+
def any_blank?
|
29
|
+
any?(&:blank?)
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# Check if array contains a hash with given arg
|
34
|
+
# arg: should be an hash
|
35
|
+
#
|
36
|
+
# arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
|
37
|
+
# arr.include_hash_with?({a: 4}) => true
|
38
|
+
# arr.include_hash_with?({"a" => 4, "b" => 5}) => true
|
39
|
+
# arr.include_hash_with?({a: 4, b: 6}) => false
|
40
|
+
def include_hash_with?(arg)
|
41
|
+
!find_hash_with(arg).blank?
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Check if array contains a string with given substring
|
46
|
+
#
|
47
|
+
# arr = ['abc', 'def', 'pqr', 'xyz']
|
48
|
+
# arr.include_string_with?('pq') => true
|
49
|
+
# arr.include_string_with?('df') => false
|
50
|
+
def include_string_with?(arg)
|
51
|
+
any? { |e| e.include?(arg) }
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Find a hash from array which matches given arg
|
56
|
+
# arg: should be hash
|
57
|
+
#
|
58
|
+
# arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
|
59
|
+
# arr.find_hash_with({a: 4}) => {a: 4, b: 5, c: 6}
|
60
|
+
# arr.find_hash_with({"b" => 2, "c" => 3}) => {a: 1, b: 2, c: 3}
|
61
|
+
# arr.find_hash_with({ a: 4, c: 8 }) => nil
|
62
|
+
def find_hash_with(arg)
|
63
|
+
find { |h| h.stringify_keys.sub_hash?(arg.stringify_keys) }
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Select hashes from array, which matches given arg
|
68
|
+
# arg: should be hash
|
69
|
+
#
|
70
|
+
# arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}, {a: 1, b: 8, c: 9}]
|
71
|
+
# arr.select_hash_with({ a: 1 }) => [{a: 1, b: 2, c: 3}, {a: 1, b: 8, c: 9}]
|
72
|
+
def select_hash_with(arg)
|
73
|
+
select { |h| h.stringify_keys.sub_hash?(arg.stringify_keys) }
|
74
|
+
end
|
75
|
+
end
|
data/lib/simple_ext/hash.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
class Hash
|
5
|
+
|
6
|
+
##
|
7
|
+
# Check if current hash is sub hash of given hash
|
8
|
+
# {a: 1, b: 2}.sub_hash_of?({a: 1, b: 2, c: 3, d: 4}) => true
|
9
|
+
# {a: 1, b: 2}.sub_hash_of?({a: 1, b: 5, c: 3, d: 4}) => false
|
10
|
+
def sub_hash_of?(hash)
|
11
|
+
hash.merge(self) == hash
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# Check if given hash is sub hash of current hash
|
16
|
+
# {a: 1, b: 2, c: 3, d: 4}.sub_hash?({a: 1, b: 2}) => true
|
17
|
+
# {a: 1, b: 2, c: 3, d: 4}.sub_hash?({c: 1, b: 2}) => false
|
18
|
+
def sub_hash?(hash)
|
19
|
+
merge(hash) == self
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tushar Hawaldar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02-
|
11
|
+
date: 2020-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Core ruby extentions extracted from Rails ActiveSupport
|
14
14
|
email:
|
@@ -20,11 +20,13 @@ files:
|
|
20
20
|
- lib/simple_ext.rb
|
21
21
|
- lib/simple_ext/array.rb
|
22
22
|
- lib/simple_ext/array/access.rb
|
23
|
+
- lib/simple_ext/array/values.rb
|
23
24
|
- lib/simple_ext/digest.rb
|
24
25
|
- lib/simple_ext/digest/uuid.rb
|
25
26
|
- lib/simple_ext/file.rb
|
26
27
|
- lib/simple_ext/file/atomic.rb
|
27
28
|
- lib/simple_ext/hash.rb
|
29
|
+
- lib/simple_ext/hash/check.rb
|
28
30
|
- lib/simple_ext/hash/except.rb
|
29
31
|
- lib/simple_ext/hash/keys.rb
|
30
32
|
- lib/simple_ext/hash/merge.rb
|