theusual 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/theusual/hash.rb +28 -5
- data/lib/theusual/string.rb +29 -0
- data/lib/theusual.rb +22 -6
- data/test/test_hash.rb +61 -0
- data/test/test_string.rb +91 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5626e015b912c430bbf29ef349b9c0278486e6d3
|
4
|
+
data.tar.gz: a8fd13ede49258fb7e14220a45112029d2d512fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5ec204b80a4eca317936e64ba6f0da7e9a16e643896c3bf046bb73ba0f4bf824249c46bf3d54a41ad56af0f19efae3e0d818bbf5085050dfd80575a4ba49740
|
7
|
+
data.tar.gz: f4b09eac9098a009b50ce9672a413db5752ea5bdca8e84b58222fc52f7f49f5be243c8ce8d4b61b22a4a42fb9a9b026839e3febb28e1fa4fa12aceaa95d9380b
|
data/lib/theusual/hash.rb
CHANGED
@@ -14,6 +14,30 @@ class Hash
|
|
14
14
|
end # class << self
|
15
15
|
|
16
16
|
|
17
|
+
# expand update() to accept multiple arguments
|
18
|
+
# eg. {}.update({a: 1}, {b: 2})
|
19
|
+
def update(*hashes)
|
20
|
+
clone.update! *hashes
|
21
|
+
end
|
22
|
+
|
23
|
+
def update!(*hashes)
|
24
|
+
hashes.each do |h|
|
25
|
+
h.each {|k,v| self[k] = v}
|
26
|
+
end
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def except(*keys)
|
32
|
+
clone.except! *keys
|
33
|
+
end
|
34
|
+
|
35
|
+
def except!(*keys)
|
36
|
+
keys.each { |key| delete(key) }
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
|
17
41
|
def select_keys(*keys)
|
18
42
|
if keys.length == 1 and keys.first.class < Enumerable
|
19
43
|
keys = keys.first
|
@@ -62,7 +86,6 @@ class Hash
|
|
62
86
|
Hash[map do |k, v|
|
63
87
|
[ block.arity == 1 ? block.call(k) : block.call(k, v), v ]
|
64
88
|
end]
|
65
|
-
|
66
89
|
end
|
67
90
|
|
68
91
|
def kmap!(&block)
|
@@ -73,13 +96,13 @@ class Hash
|
|
73
96
|
# map values, but preserve associated keys
|
74
97
|
# ie. http://apidock.com/rails/v4.2.7/Hash/transform_values
|
75
98
|
def vmap(&block)
|
76
|
-
|
77
|
-
[ k, block.arity == 1 ? block.call(v) : block.call(k, v) ]
|
78
|
-
end]
|
99
|
+
clone.vmap! &block
|
79
100
|
end
|
80
101
|
|
81
102
|
def vmap!(&block)
|
82
|
-
|
103
|
+
each do |k, v|
|
104
|
+
self[k] = block.arity == 1 ? block.call(v) : block.call(k, v)
|
105
|
+
end
|
83
106
|
end
|
84
107
|
|
85
108
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
class String
|
3
|
+
|
4
|
+
def is_f?
|
5
|
+
self == self.to_f.to_s rescue false
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
def is_i?
|
10
|
+
self == self.to_i.to_s and !self.include? '.' rescue false
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def numeric?
|
15
|
+
is_f?
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def to_bool
|
20
|
+
if self.downcase == 'false'
|
21
|
+
false
|
22
|
+
elsif self.downcase == 'true'
|
23
|
+
true
|
24
|
+
else
|
25
|
+
raise ArgumentError.new "expected 'true' or 'false', got: #{self}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/theusual.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module TheUsual
|
2
|
-
VERSION = '0.0.
|
2
|
+
VERSION = '0.0.4'
|
3
3
|
|
4
4
|
MODULES = [
|
5
5
|
'array',
|
@@ -7,6 +7,7 @@ module TheUsual
|
|
7
7
|
'ipaddr',
|
8
8
|
'mongoid',
|
9
9
|
'net/ssh',
|
10
|
+
'string',
|
10
11
|
'time',
|
11
12
|
]
|
12
13
|
|
@@ -25,10 +26,19 @@ module TheUsual
|
|
25
26
|
|
26
27
|
raise ArgumentError 'did you mean load(:all) ?' if modules.empty?
|
27
28
|
|
28
|
-
modules
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
modules.map! &:to_s
|
30
|
+
to_load = if [:all, 'all', '*'].include? modules.first
|
31
|
+
MODULES
|
32
|
+
else
|
33
|
+
modules
|
34
|
+
end
|
35
|
+
|
36
|
+
to_load.flatten.map(&:to_s).map do |_module|
|
37
|
+
unless MODULES.include? _module
|
38
|
+
raise ArgumentError.new(
|
39
|
+
"can not load utils for module: #{_module}"
|
40
|
+
)
|
41
|
+
end
|
32
42
|
|
33
43
|
begin
|
34
44
|
# load standard lib
|
@@ -40,7 +50,13 @@ module TheUsual
|
|
40
50
|
|
41
51
|
_module
|
42
52
|
rescue LoadError
|
43
|
-
#
|
53
|
+
# underlying gem not installed
|
54
|
+
# for :all, just skip monkey patch
|
55
|
+
if modules.include? _module
|
56
|
+
raise ArgumentError.new(
|
57
|
+
"missing library gem: gem install #{_module}"
|
58
|
+
)
|
59
|
+
end
|
44
60
|
end
|
45
61
|
end.compact
|
46
62
|
end
|
data/test/test_hash.rb
CHANGED
@@ -34,6 +34,67 @@ class HashTest < Minitest::Test
|
|
34
34
|
end
|
35
35
|
|
36
36
|
|
37
|
+
def test_update
|
38
|
+
data = {
|
39
|
+
a: 1,
|
40
|
+
b: 2,
|
41
|
+
c: 3,
|
42
|
+
}
|
43
|
+
|
44
|
+
assert_equal(
|
45
|
+
{a: 1, b: 2, c: 3, d: 4},
|
46
|
+
data.update({d: 4})
|
47
|
+
)
|
48
|
+
|
49
|
+
assert_equal(
|
50
|
+
{a: 1, b: 2, c: 3, d: 4, e: 5},
|
51
|
+
data.update({d: 4, e: 5})
|
52
|
+
)
|
53
|
+
|
54
|
+
assert_equal(
|
55
|
+
{a: 1, b: 2, c: 3, d: 4, e: 5},
|
56
|
+
data.update({d: 4}, {e: 5})
|
57
|
+
)
|
58
|
+
|
59
|
+
assert_equal(
|
60
|
+
{a: 1, b: 2, c: 3, d: 4, e: 5},
|
61
|
+
data.update!({d: 4}, {e: 5})
|
62
|
+
)
|
63
|
+
assert_equal(
|
64
|
+
{a: 1, b: 2, c: 3, d: 4, e: 5},
|
65
|
+
data
|
66
|
+
)
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
def test_except
|
71
|
+
data = {
|
72
|
+
a: 1,
|
73
|
+
b: 2,
|
74
|
+
c: 3,
|
75
|
+
}
|
76
|
+
|
77
|
+
assert_equal(
|
78
|
+
{a: 1, b: 2},
|
79
|
+
data.except(:c)
|
80
|
+
)
|
81
|
+
|
82
|
+
assert_equal(
|
83
|
+
{a: 1},
|
84
|
+
data.except(:b, :c)
|
85
|
+
)
|
86
|
+
|
87
|
+
assert_equal(
|
88
|
+
{a: 1},
|
89
|
+
data.except!(:b, :c)
|
90
|
+
)
|
91
|
+
assert_equal(
|
92
|
+
{a: 1},
|
93
|
+
data
|
94
|
+
)
|
95
|
+
end
|
96
|
+
|
97
|
+
|
37
98
|
def test_select_keys
|
38
99
|
data = {
|
39
100
|
c: 2,
|
data/test/test_string.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'theusual'
|
3
|
+
TheUsual::load :string
|
4
|
+
|
5
|
+
|
6
|
+
class HashTest < Minitest::Test
|
7
|
+
|
8
|
+
def test_is_i
|
9
|
+
assert_equal(
|
10
|
+
true,
|
11
|
+
'1'.is_i?
|
12
|
+
)
|
13
|
+
|
14
|
+
assert_equal(
|
15
|
+
true,
|
16
|
+
'10'.is_i?
|
17
|
+
)
|
18
|
+
|
19
|
+
assert_equal(
|
20
|
+
false,
|
21
|
+
'1.0'.is_i?
|
22
|
+
)
|
23
|
+
|
24
|
+
assert_equal(
|
25
|
+
true,
|
26
|
+
'0'.is_i?
|
27
|
+
)
|
28
|
+
|
29
|
+
assert_equal(
|
30
|
+
false,
|
31
|
+
'abc'.is_i?
|
32
|
+
)
|
33
|
+
|
34
|
+
assert_equal(
|
35
|
+
false,
|
36
|
+
'123abc'.is_i?
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def test_is_f
|
42
|
+
assert_equal(
|
43
|
+
false,
|
44
|
+
'1'.is_f?
|
45
|
+
)
|
46
|
+
|
47
|
+
assert_equal(
|
48
|
+
false,
|
49
|
+
'10'.is_f?
|
50
|
+
)
|
51
|
+
|
52
|
+
assert_equal(
|
53
|
+
true,
|
54
|
+
'1.0'.is_f?
|
55
|
+
)
|
56
|
+
|
57
|
+
assert_equal(
|
58
|
+
false,
|
59
|
+
'0'.is_f?
|
60
|
+
)
|
61
|
+
|
62
|
+
assert_equal(
|
63
|
+
false,
|
64
|
+
'abc'.is_f?
|
65
|
+
)
|
66
|
+
|
67
|
+
assert_equal(
|
68
|
+
false,
|
69
|
+
'123.4 abc'.is_f?
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def test_to_bool
|
75
|
+
assert_equal(
|
76
|
+
true,
|
77
|
+
'true'.to_bool
|
78
|
+
)
|
79
|
+
|
80
|
+
assert_equal(
|
81
|
+
true,
|
82
|
+
'True'.to_bool
|
83
|
+
)
|
84
|
+
|
85
|
+
assert_equal(
|
86
|
+
false,
|
87
|
+
'false'.to_bool
|
88
|
+
)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: theusual
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Pepper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -36,10 +36,12 @@ files:
|
|
36
36
|
- lib/theusual/ipaddr.rb
|
37
37
|
- lib/theusual/mongoid.rb
|
38
38
|
- lib/theusual/ssh.rb
|
39
|
+
- lib/theusual/string.rb
|
39
40
|
- lib/theusual/time.rb
|
40
41
|
- test/test_array.rb
|
41
42
|
- test/test_hash.rb
|
42
43
|
- test/test_ip_addr.rb
|
44
|
+
- test/test_string.rb
|
43
45
|
- test/test_time.rb
|
44
46
|
homepage: https://github.com/d1hotpep/theusualrb
|
45
47
|
licenses:
|
@@ -69,4 +71,5 @@ test_files:
|
|
69
71
|
- test/test_array.rb
|
70
72
|
- test/test_hash.rb
|
71
73
|
- test/test_ip_addr.rb
|
74
|
+
- test/test_string.rb
|
72
75
|
- test/test_time.rb
|