aduki 0.2.4 → 0.2.6
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/aduki.gemspec +1 -1
- data/lib/aduki.rb +9 -3
- data/lib/aduki/version.rb +1 -1
- data/lib/core_ext/array.rb +32 -0
- data/lib/core_ext/hash.rb +43 -0
- data/spec/hash_recursively_replace_keys_spec.rb +56 -0
- data/spec/hash_remove_by_value_spec.rb +58 -0
- data/spec/hash_spec.rb +61 -0
- metadata +12 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b85c60d97a71915e789b12de6530f1bbabb45eb7
|
4
|
+
data.tar.gz: 77743c74d0d3fc6182941742c22b98ea06a6d906
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55e9eff182433b8a42591f97ef97f108ddef3f824a5d7b969b26cd2db7ee83288e833aa35b0acc23c9473d841883125c8cb01f6d2b17eae49077d58dca0aacbc
|
7
|
+
data.tar.gz: f7a1bab6b2cae7502d5e649fce9810d4f8ccff24d8e5cf77c0d9bfebf4c245a209b13b6cfd21a64d6555ba39ca14fb9132ac0a8194e53eb6ff324418cf4e3a3a
|
data/aduki.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.homepage = "https://github.com/conanite/aduki"
|
17
17
|
|
18
18
|
|
19
|
-
gem.add_development_dependency 'rspec', '~>
|
19
|
+
gem.add_development_dependency 'rspec', '~> 3.1'
|
20
20
|
gem.add_development_dependency 'rspec_numbering_formatter'
|
21
21
|
|
22
22
|
gem.files = `git ls-files`.split($/)
|
data/lib/aduki.rb
CHANGED
@@ -5,6 +5,11 @@ require "aduki/recursive_hash"
|
|
5
5
|
require "aduki/attr_finder"
|
6
6
|
|
7
7
|
module Aduki
|
8
|
+
def self.install_monkey_patches
|
9
|
+
require 'core_ext/array'
|
10
|
+
require 'core_ext/hash'
|
11
|
+
end
|
12
|
+
|
8
13
|
def self.to_aduki obj, collector={ }, key="", join=""
|
9
14
|
case obj
|
10
15
|
when Hash
|
@@ -40,7 +45,7 @@ module Aduki
|
|
40
45
|
return value.map { |v| to_value klass, setter, v} if value.is_a? Array
|
41
46
|
|
42
47
|
type = klass.aduki_type_for_attribute_name setter
|
43
|
-
if type && (value.class <= type)
|
48
|
+
if type && type.is_a?(Class) && (value.class <= type)
|
44
49
|
value
|
45
50
|
elsif type.is_a? Hash
|
46
51
|
to_typed_hash type.values.first, value
|
@@ -82,7 +87,7 @@ module Aduki
|
|
82
87
|
setters[first] ||= { }
|
83
88
|
setters[first][rest] = value
|
84
89
|
else
|
85
|
-
setters[setter] = value
|
90
|
+
setters[setter.to_s] = value
|
86
91
|
end
|
87
92
|
end
|
88
93
|
setters
|
@@ -182,10 +187,11 @@ module Aduki
|
|
182
187
|
module Initializer
|
183
188
|
def initialize attrs={ }
|
184
189
|
self.class.get_aduki_initializers.each { |initializer| send initializer }
|
185
|
-
|
190
|
+
aduki_apply_attributes attrs
|
186
191
|
aduki_after_initialize
|
187
192
|
end
|
188
193
|
|
194
|
+
def aduki_apply_attributes attrs ; Aduki.apply_attributes self, attrs ; end
|
189
195
|
def aduki_after_initialize ; end
|
190
196
|
|
191
197
|
def self.included(base)
|
data/lib/aduki/version.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
class Array
|
2
|
+
def recursively_delete_keys! *keys
|
3
|
+
self.each { |v| v.recursively_delete_keys!(*keys) if v.is_a?(Hash) || v.is_a?(Array) } ; self
|
4
|
+
end
|
5
|
+
|
6
|
+
def recursively_replace_keys! &block
|
7
|
+
self.each { |v| v.recursively_replace_keys!(&block) if v.is_a?(Hash) || v.is_a?(Array) } ; self
|
8
|
+
end
|
9
|
+
|
10
|
+
def recursively_replace_values &block
|
11
|
+
result = []
|
12
|
+
each_with_index { |v, i|
|
13
|
+
result << ((v.is_a?(Hash) || v.is_a?(Array)) ? v.recursively_replace_values(&block) : yield(i, v))
|
14
|
+
}
|
15
|
+
result
|
16
|
+
end
|
17
|
+
|
18
|
+
def recursively_remove_by_value! &block
|
19
|
+
self.delete_if { |v|
|
20
|
+
if v.is_a? Hash
|
21
|
+
v.recursively_remove_by_value! &block
|
22
|
+
v.empty?
|
23
|
+
elsif v.is_a? Array
|
24
|
+
v.recursively_remove_by_value! &block
|
25
|
+
v.empty?
|
26
|
+
else
|
27
|
+
yield v
|
28
|
+
end
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Hash
|
2
|
+
def recursively_delete_keys! *keys_to_delete
|
3
|
+
keys_to_delete.each { |k| delete k }
|
4
|
+
values.each { |v| v.recursively_delete_keys!(*keys_to_delete) if v.is_a?(Hash) || v.is_a?(Array) }
|
5
|
+
self
|
6
|
+
end
|
7
|
+
|
8
|
+
def recursively_replace_keys! &block
|
9
|
+
keys.each do |k|
|
10
|
+
v = self[k]
|
11
|
+
mk = yield k
|
12
|
+
if k != mk
|
13
|
+
self[mk] = delete k
|
14
|
+
end
|
15
|
+
v.recursively_replace_keys!(&block) if v.is_a?(Hash) || v.is_a?(Array)
|
16
|
+
end
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def recursively_stringify_keys!
|
21
|
+
recursively_replace_keys! { |k| k.to_s }
|
22
|
+
end
|
23
|
+
|
24
|
+
def recursively_remove_by_value! &block
|
25
|
+
self.delete_if { |k, v|
|
26
|
+
if v.is_a? Hash
|
27
|
+
v.recursively_remove_by_value! &block
|
28
|
+
v.empty?
|
29
|
+
elsif v.is_a? Array
|
30
|
+
v.recursively_remove_by_value! &block
|
31
|
+
v.empty?
|
32
|
+
else
|
33
|
+
yield v
|
34
|
+
end
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def recursively_replace_values &block
|
39
|
+
each_with_object({ }) { |(k, v), result|
|
40
|
+
result[k] = (v.is_a?(Hash) || v.is_a?(Array)) ? v.recursively_replace_values!(&block) : yield(k, v)
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
before { Aduki.install_monkey_patches }
|
5
|
+
|
6
|
+
describe 'recursively_replace_keys!' do
|
7
|
+
it "destructively replaces keys with upcase symbol equivalents" do
|
8
|
+
h0 = { "x/y/z" => [1,2,3], "p-q-r" => 123 }
|
9
|
+
a = [h0, h0, 10, 11, 12]
|
10
|
+
hsh = {
|
11
|
+
"Foo_BAR" => 1,
|
12
|
+
HEYHO: 2,
|
13
|
+
"Tweedlé Dum" => 3,
|
14
|
+
"4" => 4,
|
15
|
+
" 20 something?" => true,
|
16
|
+
"** more fun 2 !! **" => true,
|
17
|
+
"ça và" => true,
|
18
|
+
"((a b c))" => true,
|
19
|
+
:nana => {
|
20
|
+
"a b c" => 1,
|
21
|
+
"surprise! :)" => a
|
22
|
+
}
|
23
|
+
}
|
24
|
+
expected = {
|
25
|
+
:FOO_BAR => 1,
|
26
|
+
:HEYHO => 2,
|
27
|
+
:"TWEEDLé DUM" => 3,
|
28
|
+
:"4" => 4,
|
29
|
+
:"20 SOMETHING?" => true,
|
30
|
+
:"** MORE FUN 2 !! **" => true,
|
31
|
+
:"çA Và" => true,
|
32
|
+
:"((A B C))" => true,
|
33
|
+
:NANA => {
|
34
|
+
:"A B C" => 1,
|
35
|
+
:"SURPRISE! :)" =>
|
36
|
+
[
|
37
|
+
{
|
38
|
+
:"X/Y/Z" => [1,2,3],
|
39
|
+
:"P-Q-R" => 123
|
40
|
+
},
|
41
|
+
{
|
42
|
+
:"X/Y/Z" => [1,2,3],
|
43
|
+
:"P-Q-R" => 123
|
44
|
+
},
|
45
|
+
10,
|
46
|
+
11,
|
47
|
+
12
|
48
|
+
]
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
hsh.recursively_replace_keys! { |k| k.to_s.strip.upcase.to_sym }
|
53
|
+
expect(hsh).to eq expected
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
before { Aduki.install_monkey_patches }
|
5
|
+
|
6
|
+
describe 'recursively_remove_by_value!' do
|
7
|
+
class Unwanted < Struct.new(:name)
|
8
|
+
def to_s; name; end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:h) {
|
12
|
+
{
|
13
|
+
:foo => "bar",
|
14
|
+
willy: Unwanted.new("wonka"),
|
15
|
+
"titi" => [1,2,3, String, "yellow", Unwanted.new(999), 3.1415, {
|
16
|
+
a: :b,
|
17
|
+
c: :d,
|
18
|
+
2 => Unwanted.new(Hash),
|
19
|
+
3 => Unwanted.new(Array),
|
20
|
+
4 => Numeric,
|
21
|
+
6 => {
|
22
|
+
7 => "me",
|
23
|
+
8 => "you",
|
24
|
+
:foo => Unwanted.new("enough!")}} ],
|
25
|
+
thats: :it
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
it "removes unwanted items" do
|
30
|
+
h.recursively_remove_by_value! do |v|
|
31
|
+
v.is_a? Unwanted
|
32
|
+
end
|
33
|
+
|
34
|
+
expected = {
|
35
|
+
foo: "bar",
|
36
|
+
thats: :it,
|
37
|
+
"titi" => [1, 2, 3, String, "yellow", 3.1415, {:a=>:b, :c=>:d, 4=>Numeric, 6=>{7=>"me", 8=>"you"}}]
|
38
|
+
}
|
39
|
+
|
40
|
+
expect(h).to eq(expected)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "removes more unwanted items" do
|
44
|
+
h.recursively_remove_by_value! do |v|
|
45
|
+
k = v.class
|
46
|
+
true unless k <= String || k <= Symbol || k <= Hash || k <= Array || k <= Numeric
|
47
|
+
end
|
48
|
+
|
49
|
+
expected = {
|
50
|
+
foo: "bar",
|
51
|
+
thats: :it,
|
52
|
+
"titi" => [1, 2, 3, "yellow", 3.1415, {:a=>:b, :c=>:d, 6=>{7=>"me", 8=>"you"}}]
|
53
|
+
}
|
54
|
+
|
55
|
+
expect(h).to eq(expected)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
before { Aduki.install_monkey_patches }
|
5
|
+
|
6
|
+
describe 'recursively_delete_keys!' do
|
7
|
+
it "should delete the specified keys from itself and all nested hashes, even sub-nested in arrays" do
|
8
|
+
h = {
|
9
|
+
site_id: "foo",
|
10
|
+
"site_id" => :bar,
|
11
|
+
things: {
|
12
|
+
site_id: "toto",
|
13
|
+
"site_id" => :titi,
|
14
|
+
2 => [ { a: 12, b: 53, site_id: 99 },
|
15
|
+
{ a: 13, b: 54, site_id: 98 },
|
16
|
+
{ a: 14, b: 55, site_id: 97 },
|
17
|
+
{ a: 14, b: 56, site_id: 96 } ],
|
18
|
+
toto: :titi
|
19
|
+
},
|
20
|
+
others: [ { name: "Walter", site_id: 95 } ]
|
21
|
+
}
|
22
|
+
|
23
|
+
h.recursively_delete_keys! :site_id, "site_id"
|
24
|
+
|
25
|
+
h.should == {
|
26
|
+
things: {
|
27
|
+
2 => [ { a: 12, b: 53 },
|
28
|
+
{ a: 13, b: 54 },
|
29
|
+
{ a: 14, b: 55 },
|
30
|
+
{ a: 14, b: 56 } ],
|
31
|
+
toto: :titi
|
32
|
+
},
|
33
|
+
others: [ { name: "Walter" } ]
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'recursively_stringify_keys!' do
|
39
|
+
it "should change all keys to strings" do
|
40
|
+
h = {
|
41
|
+
foo: "bar",
|
42
|
+
12 => 13,
|
43
|
+
Date.parse("2013-04-05") => "yes",
|
44
|
+
nil => "gotcha",
|
45
|
+
[:foo] => "arrrrr"
|
46
|
+
}
|
47
|
+
|
48
|
+
expected = {
|
49
|
+
"foo" => "bar",
|
50
|
+
"12" => 13,
|
51
|
+
"2013-04-05" => "yes",
|
52
|
+
"" => "gotcha",
|
53
|
+
"[:foo]" => "arrrrr"
|
54
|
+
}
|
55
|
+
|
56
|
+
h.recursively_stringify_keys!
|
57
|
+
|
58
|
+
expect(h).to eq expected
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aduki
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Conan Dalton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '3.1'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '3.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec_numbering_formatter
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,10 +56,15 @@ files:
|
|
56
56
|
- lib/aduki/attr_finder.rb
|
57
57
|
- lib/aduki/recursive_hash.rb
|
58
58
|
- lib/aduki/version.rb
|
59
|
+
- lib/core_ext/array.rb
|
60
|
+
- lib/core_ext/hash.rb
|
59
61
|
- spec/array_attribute_spec.rb
|
60
62
|
- spec/attr_finder_spec.rb
|
61
63
|
- spec/custom_builder_spec.rb
|
62
64
|
- spec/finder_spec.rb
|
65
|
+
- spec/hash_recursively_replace_keys_spec.rb
|
66
|
+
- spec/hash_remove_by_value_spec.rb
|
67
|
+
- spec/hash_spec.rb
|
63
68
|
- spec/initialize_with_object_spec.rb
|
64
69
|
- spec/initializer_spec.rb
|
65
70
|
- spec/merge_attributes_spec.rb
|
@@ -98,6 +103,9 @@ test_files:
|
|
98
103
|
- spec/attr_finder_spec.rb
|
99
104
|
- spec/custom_builder_spec.rb
|
100
105
|
- spec/finder_spec.rb
|
106
|
+
- spec/hash_recursively_replace_keys_spec.rb
|
107
|
+
- spec/hash_remove_by_value_spec.rb
|
108
|
+
- spec/hash_spec.rb
|
101
109
|
- spec/initialize_with_object_spec.rb
|
102
110
|
- spec/initializer_spec.rb
|
103
111
|
- spec/merge_attributes_spec.rb
|