obfusk-util 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -4
- data/lib/obfusk/util/data.rb +72 -1
- data/lib/obfusk/util/sh.rb +12 -12
- data/lib/obfusk/util/version.rb +2 -2
- data/spec/obfusk/util/data_spec.rb +77 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 965ed408e2dc3fda80936c94b7ae17c89c4a5bfe
|
4
|
+
data.tar.gz: 794f55bedb6c351327c5e163cd22af9f4653eeb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d9b1bec69b6340241e94a92b11c62ed3ef24d35c2b6497f858bd28e68cfbe7ff9b5c16f4e5fd8e73c06db187bcc07cf5464ab70eb6bee0be6a8645605c89bb3
|
7
|
+
data.tar.gz: c8b1c572997fb721fd67d17c5cb891d4c386bde6314fd77f0ad260a12805faf9acfa85e9b9b50b798e8f78736c933b07895a227179c260bfeb5c2a1c66cf8bfa
|
data/README.md
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
File : README.md
|
4
4
|
Maintainer : Felix C. Stegerman <flx@obfusk.net>
|
5
|
-
Date : 2014-
|
5
|
+
Date : 2014-03-03
|
6
6
|
|
7
7
|
Copyright : Copyright (C) 2014 Felix C. Stegerman
|
8
|
-
Version : v0.5.
|
8
|
+
Version : v0.5.1
|
9
9
|
|
10
10
|
[]: }}}1
|
11
11
|
|
@@ -54,8 +54,22 @@ require 'obfusk/util/data'
|
|
54
54
|
|
55
55
|
x = { x: { y: 0 }, z: [1,2,3] }
|
56
56
|
Obfusk::Util.assoc(x, [:x,:y] => 1, [:z,1] => 99)
|
57
|
-
x[:x][:y]
|
58
|
-
x[:z]
|
57
|
+
x[:x][:y] # => 1
|
58
|
+
x[:z] # => [1,99,3]
|
59
|
+
|
60
|
+
Obfusk::Util.get_in({ x: { y: 1 } }, :x, :y)
|
61
|
+
# => 1
|
62
|
+
|
63
|
+
Obfusk::Util.symbolize_keys({ 'x' => 1, 'y' => 99 })
|
64
|
+
# => { x: 1, y: 99 }
|
65
|
+
|
66
|
+
Obfusk::Util.deep_stringify_keys({ x: { 2 => 99 } })
|
67
|
+
# => { 'x' => { '2' => 99 } }
|
68
|
+
|
69
|
+
h1 = { x: { y: 42 }, z: 11 }
|
70
|
+
h2 = { x: { y: 1, q: 1 }, z: 1 }
|
71
|
+
Obfusk::Util.deep_merge(h1, h2) { |k,o,n| o + n }
|
72
|
+
# => { x: { y: 43, q: 1 }, z: 12 }
|
59
73
|
|
60
74
|
y = Obfusk::Util.deepdup x
|
61
75
|
Obfusk::Util.empty_as_nil(ENV['FOO']) || default
|
data/lib/obfusk/util/data.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
#
|
3
3
|
# File : obfusk/util/data.rb
|
4
4
|
# Maintainer : Felix C. Stegerman <flx@obfusk.net>
|
5
|
-
# Date : 2014-
|
5
|
+
# Date : 2014-03-03
|
6
6
|
#
|
7
7
|
# Copyright : Copyright (C) 2014 Felix C. Stegerman
|
8
8
|
# Licence : LGPLv3+
|
@@ -33,6 +33,77 @@ module Obfusk; module Util
|
|
33
33
|
x
|
34
34
|
end # }}}1
|
35
35
|
|
36
|
+
# get the value in a nested associative structure; returns the value
|
37
|
+
# (if found), the result of the block (if passed), or nil.
|
38
|
+
#
|
39
|
+
# ```
|
40
|
+
# get_in({ x: { y: 1 } }, :x, :y)
|
41
|
+
# # => 1
|
42
|
+
# ```
|
43
|
+
def self.get_in(x, *ks, &b) # {{{1
|
44
|
+
if ks.length == 0
|
45
|
+
x
|
46
|
+
else
|
47
|
+
v = x.fetch(ks.first) { return b && b[] }
|
48
|
+
get_in v, *ks.drop(1), &b
|
49
|
+
end
|
50
|
+
end # }}}1
|
51
|
+
|
52
|
+
# --
|
53
|
+
|
54
|
+
# convert hash keys to strings
|
55
|
+
def self.stringify_keys(h)
|
56
|
+
transform_keys(h, &:to_s)
|
57
|
+
end
|
58
|
+
|
59
|
+
# convert hash keys to symbols
|
60
|
+
# @param always [Boolean] whether to always convert or ignore errors
|
61
|
+
def self.symbolize_keys(h, always = true)
|
62
|
+
transform_keys(h, &(always ? :to_sym : -> k { k.to_sym rescue k }))
|
63
|
+
end
|
64
|
+
|
65
|
+
# convert hash keys using block
|
66
|
+
def self.transform_keys(h, &b)
|
67
|
+
Hash[h.map { |k,v| [b[k],v] }]
|
68
|
+
end
|
69
|
+
|
70
|
+
# --
|
71
|
+
|
72
|
+
# convert nested hash keys to strings
|
73
|
+
def self.deep_stringify_keys(h)
|
74
|
+
deep_transform_keys(h, &:to_s)
|
75
|
+
end
|
76
|
+
|
77
|
+
# convert nested hash keys to symbols
|
78
|
+
# @param always [Boolean] whether to always convert or ignore errors
|
79
|
+
def self.deep_symbolize_keys(h, always = true)
|
80
|
+
deep_transform_keys(h, &(always ? :to_sym : -> k { k.to_sym rescue k }))
|
81
|
+
end
|
82
|
+
|
83
|
+
# convert nested hash keys using block
|
84
|
+
def self.deep_transform_keys(h, &b)
|
85
|
+
Hash[h.map { |k,v| [b[k], v.is_a?(Hash) ? deep_transform_keys(v,&b) : v] }]
|
86
|
+
end
|
87
|
+
|
88
|
+
# --
|
89
|
+
|
90
|
+
# merge hashes recursively
|
91
|
+
def self.deep_merge(h1, h2, &b) # {{{1
|
92
|
+
h1.merge(Hash[h2.map do |k,v2|
|
93
|
+
if h1.has_key?(k)
|
94
|
+
if (v1 = h1[k]).is_a?(Hash) && v2.is_a?(Hash)
|
95
|
+
[k, deep_merge(v1, v2, &b)]
|
96
|
+
else
|
97
|
+
[k, b ? b[k,h1[k],v2] : v2]
|
98
|
+
end
|
99
|
+
else
|
100
|
+
[k, v2]
|
101
|
+
end
|
102
|
+
end])
|
103
|
+
end # }}}1
|
104
|
+
|
105
|
+
# --
|
106
|
+
|
36
107
|
# deep copy using Marshal
|
37
108
|
def self.deepdup(obj)
|
38
109
|
Marshal.load Marshal.dump obj
|
data/lib/obfusk/util/sh.rb
CHANGED
@@ -110,36 +110,36 @@ module Obfusk; module Util
|
|
110
110
|
|
111
111
|
# --
|
112
112
|
|
113
|
-
#
|
113
|
+
# onow + sh; requires `obfusk/util/message`
|
114
114
|
def self.osh(*args)
|
115
|
-
::Obfusk::Util.
|
115
|
+
::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); sh(*args)
|
116
116
|
end
|
117
117
|
|
118
|
-
#
|
118
|
+
# onow + sh?; requires `obfusk/util/message`
|
119
119
|
def self.osh?(*args)
|
120
|
-
::Obfusk::Util.
|
120
|
+
::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); sh?(*args)
|
121
121
|
end
|
122
122
|
|
123
|
-
#
|
123
|
+
# onow + sh!; requires `obfusk/util/message`
|
124
124
|
def self.osh!(*args)
|
125
|
-
::Obfusk::Util.
|
125
|
+
::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); sh!(*args)
|
126
126
|
end
|
127
127
|
|
128
128
|
# --
|
129
129
|
|
130
|
-
#
|
130
|
+
# onow + shc; requires `obfusk/util/message`
|
131
131
|
def self.oshc(*args)
|
132
|
-
::Obfusk::Util.
|
132
|
+
::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); shc(*args)
|
133
133
|
end
|
134
134
|
|
135
|
-
#
|
135
|
+
# onow + shc?; requires `obfusk/util/message`
|
136
136
|
def self.oshc?(*args)
|
137
|
-
::Obfusk::Util.
|
137
|
+
::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); shc?(*args)
|
138
138
|
end
|
139
139
|
|
140
|
-
#
|
140
|
+
# onow + shc!; requires `obfusk/util/message`
|
141
141
|
def self.oshc!(*args)
|
142
|
-
::Obfusk::Util.
|
142
|
+
::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); shc!(*args)
|
143
143
|
end
|
144
144
|
|
145
145
|
# --
|
data/lib/obfusk/util/version.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
#
|
3
3
|
# File : obfusk/util/data_spec.rb
|
4
4
|
# Maintainer : Felix C. Stegerman <flx@obfusk.net>
|
5
|
-
# Date : 2014-
|
5
|
+
# Date : 2014-03-03
|
6
6
|
#
|
7
7
|
# Copyright : Copyright (C) 2014 Felix C. Stegerman
|
8
8
|
# Licence : LGPLv3+
|
@@ -29,6 +29,82 @@ describe 'obfusk/util/data' do
|
|
29
29
|
end
|
30
30
|
end # }}}1
|
31
31
|
|
32
|
+
context 'get_in' do # {{{1
|
33
|
+
it 'returns nested key' do
|
34
|
+
x = { x: { y: 0 }, z: [1,2,3] }
|
35
|
+
expect(ou.get_in(x, :x, :y)).to eq(0)
|
36
|
+
expect(ou.get_in(x, :z, 1)).to eq(2)
|
37
|
+
end
|
38
|
+
it 'returns nil when not found' do
|
39
|
+
expect(ou.get_in({}, :x, :y)).to eq(nil)
|
40
|
+
end
|
41
|
+
it 'returns block result when not found and block given' do
|
42
|
+
expect(ou.get_in({}, :x, :y) { 42 }).to eq(42)
|
43
|
+
end
|
44
|
+
end # }}}1
|
45
|
+
|
46
|
+
context 'stringify_keys' do # {{{1
|
47
|
+
it 'transforms keys to strings' do
|
48
|
+
expect(ou.stringify_keys({ x: 1, 2 => 99 })).to \
|
49
|
+
eq({ 'x' => 1, '2' => 99 })
|
50
|
+
end
|
51
|
+
end # }}}1
|
52
|
+
|
53
|
+
context 'symbolize_keys' do # {{{1
|
54
|
+
it 'transforms keys to symbols' do
|
55
|
+
expect(ou.symbolize_keys({ 'x' => 1, 'y' => 99 })).to \
|
56
|
+
eq({ x: 1, y: 99 })
|
57
|
+
end
|
58
|
+
it 'ignores errors w/ always = false' do
|
59
|
+
expect(ou.symbolize_keys({ 'x' => 1, 2 => 99 }, false)).to \
|
60
|
+
eq({ x: 1, 2 => 99 })
|
61
|
+
end
|
62
|
+
end # }}}1
|
63
|
+
|
64
|
+
context 'transform_keys' do # {{{1
|
65
|
+
it 'transforms keys' do
|
66
|
+
h = { 'x' => 1, 2 => 99 }
|
67
|
+
expect(ou.transform_keys(h) { |k| k.to_sym rescue k }).to \
|
68
|
+
eq({ x: 1, 2 => 99 })
|
69
|
+
end
|
70
|
+
end # }}}1
|
71
|
+
|
72
|
+
context 'deep_stringify_keys' do # {{{1
|
73
|
+
it 'transforms nested keys to strings' do
|
74
|
+
expect(ou.deep_stringify_keys({ x: { 2 => 99 } })).to \
|
75
|
+
eq({ 'x' => { '2' => 99 } })
|
76
|
+
end
|
77
|
+
end # }}}1
|
78
|
+
|
79
|
+
context 'deep_symbolize_keys' do # {{{1
|
80
|
+
it 'transforms nested keys to symbols' do
|
81
|
+
expect(ou.deep_symbolize_keys({ 'x' => { 'y' => 99 } })).to \
|
82
|
+
eq({ x: { y: 99 } })
|
83
|
+
end
|
84
|
+
end # }}}1
|
85
|
+
|
86
|
+
context 'deep_transform_keys' do # {{{1
|
87
|
+
it 'transforms nested keys' do
|
88
|
+
h = { 'x' => { 'y' => 42 }, 2 => 99 }
|
89
|
+
expect(ou.deep_transform_keys(h) { |k| k.to_sym rescue k }).to \
|
90
|
+
eq({ x: { y: 42 }, 2 => 99 })
|
91
|
+
end
|
92
|
+
end # }}}1
|
93
|
+
|
94
|
+
context 'deep_merge' do # {{{1
|
95
|
+
it 'merges hashes recursively' do
|
96
|
+
h1 = { x: { y: 42 }, z: 11 }
|
97
|
+
h2 = { x: { y: 1 , q: 1 }, z: 1 }
|
98
|
+
h3 = { x: { y: 1 , q: 1 }, z: 1 }
|
99
|
+
h4 = { x: { y: 42, q: 1 }, z: 11 }
|
100
|
+
h5 = { x: { y: 43, q: 1 }, z: 12 }
|
101
|
+
f = -> k,o,n { o + n }
|
102
|
+
expect(ou.deep_merge(h1, h2 )).to eq(h3)
|
103
|
+
expect(ou.deep_merge(h2, h1 )).to eq(h4)
|
104
|
+
expect(ou.deep_merge(h1, h2, &f)).to eq(h5)
|
105
|
+
end
|
106
|
+
end # }}}1
|
107
|
+
|
32
108
|
context 'deepdup' do # {{{1
|
33
109
|
it 'equal' do
|
34
110
|
x = { x: { y: 0 }, z: [1,2,3], s: ds::S.new(1,2) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: obfusk-util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felix C. Stegerman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|