darthjee-core_ext 1.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 +7 -0
- data/.gitignore +2 -0
- data/.rspec +1 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +62 -0
- data/LICENSE +22 -0
- data/README.md +188 -0
- data/Rakefile +7 -0
- data/core_ext.gemspec +27 -0
- data/lib/array.rb +23 -0
- data/lib/array/hash_builder.rb +20 -0
- data/lib/darthjee.rb +4 -0
- data/lib/darthjee/core_ext.rb +12 -0
- data/lib/darthjee/core_ext/version.rb +5 -0
- data/lib/enumerable.rb +45 -0
- data/lib/hash.rb +195 -0
- data/lib/hash/deep_hash_constructor.rb +83 -0
- data/lib/hash/key_changer.rb +76 -0
- data/lib/hash/value_changer.rb +61 -0
- data/lib/numeric.rb +6 -0
- data/lib/symbol.rb +9 -0
- data/spec/lib/array_spec.rb +229 -0
- data/spec/lib/enumerable_spec.rb +31 -0
- data/spec/lib/hash/deep_hash_constructor_spec.rb +167 -0
- data/spec/lib/hash/key_changer_spec.rb +55 -0
- data/spec/lib/hash_spec.rb +347 -0
- data/spec/lib/numeric_spec.rb +61 -0
- data/spec/lib/symbol_spec.rb +35 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/support/models/hash/value_changer/dummy.rb +15 -0
- data/spec/support/models/hash/value_changer/dummy_iteractor.rb +12 -0
- data/spec/support/shared_examples/array_random.rb +16 -0
- data/spec/support/shared_examples/chain_fetch.rb +88 -0
- data/spec/support/shared_examples/chain_hash_keys_changer.rb +88 -0
- data/spec/support/shared_examples/clean.rb +143 -0
- data/spec/support/shared_examples/expected.rb +6 -0
- data/spec/support/shared_examples/hash_keys_changer.rb +85 -0
- data/spec/support/shared_examples/keys_appender.rb +43 -0
- data/spec/support/shared_examples/keys_camelizer.rb +285 -0
- data/spec/support/shared_examples/keys_underscorer.rb +82 -0
- data/spec/support/shared_examples/remap.rb +89 -0
- data/spec/support/shared_examples/value_changer.rb +92 -0
- metadata +217 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d36ee33e60a4897dde6c91aa95e63d8644253493
|
4
|
+
data.tar.gz: e5f8f6d6ff67007a00486e40fa6dc72b472241f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d0dea263e737fb1838163f640681dcba2f7cee1b9ddedcc6518d0aa68cf95c8b6a0e6f227508174b997a58f79b79ecd095debf8f16e6496d731bc7a648743de
|
7
|
+
data.tar.gz: '03768926ec5377779a98c52326f93cf7e0bacee374b7be6228e01fdaf56cf342ac80bb1aad6a624ec2394cfbff38af90c225068672a46be81dcf3d625098baf5'
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
darthjee-core_ext (1.2.6)
|
5
|
+
activesupport (~> 5.1.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.1.1)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
coderay (1.1.1)
|
16
|
+
concurrent-ruby (1.0.5)
|
17
|
+
diff-lcs (1.2.5)
|
18
|
+
docile (1.1.5)
|
19
|
+
i18n (0.8.4)
|
20
|
+
json (2.1.0)
|
21
|
+
method_source (0.8.2)
|
22
|
+
minitest (5.10.2)
|
23
|
+
pry (0.10.4)
|
24
|
+
coderay (~> 1.1.0)
|
25
|
+
method_source (~> 0.8.1)
|
26
|
+
slop (~> 3.4)
|
27
|
+
pry-nav (0.2.4)
|
28
|
+
pry (>= 0.9.10, < 0.11.0)
|
29
|
+
rake (11.3.0)
|
30
|
+
rspec (2.99.0)
|
31
|
+
rspec-core (~> 2.99.0)
|
32
|
+
rspec-expectations (~> 2.99.0)
|
33
|
+
rspec-mocks (~> 2.99.0)
|
34
|
+
rspec-core (2.99.2)
|
35
|
+
rspec-expectations (2.99.2)
|
36
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
37
|
+
rspec-mocks (2.99.4)
|
38
|
+
simplecov (0.14.1)
|
39
|
+
docile (~> 1.1.0)
|
40
|
+
json (>= 1.8, < 3)
|
41
|
+
simplecov-html (~> 0.10.0)
|
42
|
+
simplecov-html (0.10.1)
|
43
|
+
slop (3.6.0)
|
44
|
+
thread_safe (0.3.6)
|
45
|
+
tzinfo (1.2.3)
|
46
|
+
thread_safe (~> 0.1)
|
47
|
+
|
48
|
+
PLATFORMS
|
49
|
+
ruby
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
bundler (~> 1.6)
|
53
|
+
darthjee-core_ext!
|
54
|
+
pry (~> 0.10.4)
|
55
|
+
pry-nav (~> 0.2.4)
|
56
|
+
rake (~> 11.3.0)
|
57
|
+
rspec (~> 2.14)
|
58
|
+
rspec-mocks (~> 2.99.4)
|
59
|
+
simplecov (~> 0.14.1)
|
60
|
+
|
61
|
+
BUNDLED WITH
|
62
|
+
1.15.1
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Bidu.com.br
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
Core_Ext
|
2
|
+
========
|
3
|
+
|
4
|
+
This project adds some new methods to the core ruby classes
|
5
|
+
|
6
|
+
To use core-ext either intall directly
|
7
|
+
|
8
|
+
```console
|
9
|
+
gem install darthjee-core_ext
|
10
|
+
```
|
11
|
+
|
12
|
+
```
|
13
|
+
gem 'danica'
|
14
|
+
```
|
15
|
+
|
16
|
+
```console
|
17
|
+
bundle install danica
|
18
|
+
```
|
19
|
+
|
20
|
+
|
21
|
+
## Array
|
22
|
+
### map_to_hash
|
23
|
+
map returning a hash with the original array for keys
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
array = %w(a ab)
|
27
|
+
arrays.map_to_hash { |val| val.length }
|
28
|
+
{ 'a' => 1, 'b' => 2 }
|
29
|
+
```
|
30
|
+
|
31
|
+
### chain_map
|
32
|
+
applies map in a chain
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
array = [ :a, :long_name, :sym ]
|
36
|
+
array.chain_map(:to_s, :size, :to_s)
|
37
|
+
[ '1', '9', '3' ]
|
38
|
+
```
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
array = [ :a, :long_name, :sym ]
|
42
|
+
array.chain_map(:to_s, :size) { |v| "final: #{v}" }
|
43
|
+
[ 'final: 1', 'final: 9', 'final: 3' ]
|
44
|
+
```
|
45
|
+
|
46
|
+
### as_hash
|
47
|
+
Creates a hash from the array using the argumen array as keys
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
[1, 2, 3].as_hash %w(a b c)
|
51
|
+
```
|
52
|
+
returns
|
53
|
+
```ruby
|
54
|
+
{ 'a' => 1, 'b' => 2, 'c' => 3 } }
|
55
|
+
```
|
56
|
+
|
57
|
+
## Hash
|
58
|
+
### map_to_hash
|
59
|
+
map returning a hash with the original keys
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
hash = { a: 1, b: 2 }
|
63
|
+
hash.map_to_hash { |k, v| "#{k}_#{v}" }
|
64
|
+
{ a: "a_1", b: "b_2" }
|
65
|
+
```
|
66
|
+
|
67
|
+
### chain_fetch
|
68
|
+
Applies fetch in a chain
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
{ a: { b: { c: { d: 10 } } } }.chain_fetch(:a, :b, :c, :d)
|
72
|
+
10
|
73
|
+
```
|
74
|
+
```ruby
|
75
|
+
h = { a: { b: { c: { d: 10 } } } }
|
76
|
+
h.chain_fetch(:a, :x, :y, :z) { |key, missed_keys| "returned #{key}" }
|
77
|
+
'returned x'
|
78
|
+
```
|
79
|
+
|
80
|
+
### squash
|
81
|
+
Squash a deep hash into a simple level hash
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
{ a: { b:1 } }.squash
|
85
|
+
```
|
86
|
+
returns
|
87
|
+
```ruby
|
88
|
+
{ 'a.b' => 1 }
|
89
|
+
```
|
90
|
+
|
91
|
+
### to_deep_hash
|
92
|
+
Changes a hash spliting keys into inner hashs
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
{ 'a.b' => 1 }.to_deep_hash
|
96
|
+
```
|
97
|
+
returns
|
98
|
+
```ruby
|
99
|
+
{ 'a' => { 'b' => 1 } }
|
100
|
+
```
|
101
|
+
|
102
|
+
### camelize_keys
|
103
|
+
Change the keys camelizing them
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
{ ca_b: 1 }.camelize_keys
|
107
|
+
```
|
108
|
+
returns
|
109
|
+
```ruby
|
110
|
+
{ CaB: 1 }
|
111
|
+
```
|
112
|
+
|
113
|
+
### change_keys
|
114
|
+
Change the array keys using a block
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
{ ca_b: 1 }.change_keys { |k| k.to_s.upcase }
|
118
|
+
```
|
119
|
+
returns
|
120
|
+
```ruby
|
121
|
+
{ 'CA_B' => 1 }
|
122
|
+
```
|
123
|
+
|
124
|
+
### chain_change_keys
|
125
|
+
Change the hash keys usin a chained method call
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
{ ca_b: 1 }.chain_change_keys(:to_s, :upcase, :to_sym)
|
129
|
+
```
|
130
|
+
returns
|
131
|
+
```ruby
|
132
|
+
{ CA_B: 1 }
|
133
|
+
```
|
134
|
+
|
135
|
+
### change_values
|
136
|
+
Change the values of the array
|
137
|
+
```ruby
|
138
|
+
{ a: 1 }.change_keys { |v| (v+1).to_s }
|
139
|
+
```
|
140
|
+
returns
|
141
|
+
```ruby
|
142
|
+
{ a: '2' }
|
143
|
+
```
|
144
|
+
|
145
|
+
### prepend_to_keys
|
146
|
+
Change each keys prepending an string
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
{ key: 1 }.prepend_to_keys 'scope:'
|
150
|
+
```
|
151
|
+
returns
|
152
|
+
```ruby
|
153
|
+
{ :'scope:key' => 1 }
|
154
|
+
```
|
155
|
+
### append_to_keys
|
156
|
+
Change each keys appending an string
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
{ key: 1 }.append_to_keys 's'
|
160
|
+
```
|
161
|
+
returns
|
162
|
+
```ruby
|
163
|
+
{ keys: 1 }
|
164
|
+
```
|
165
|
+
|
166
|
+
### sort_keys
|
167
|
+
Sort the hash usig the keys
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
{ b:1, a:2 }.sort_keys
|
171
|
+
```
|
172
|
+
returns
|
173
|
+
```ruby
|
174
|
+
{ a:2, b:1 }
|
175
|
+
```
|
176
|
+
|
177
|
+
## Enumerable
|
178
|
+
|
179
|
+
## clean!
|
180
|
+
CLeans empty values from a hash
|
181
|
+
```ruby
|
182
|
+
{ a: 1, b: [], c: nil, d: {}, e: '', f: { b: [], c: nil, d: {}, e: '' } }.clean!
|
183
|
+
```
|
184
|
+
returns
|
185
|
+
```ruby
|
186
|
+
{}
|
187
|
+
```
|
188
|
+
|
data/Rakefile
ADDED
data/core_ext.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'darthjee/core_ext/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'darthjee-core_ext'
|
8
|
+
spec.version = Darthjee::CoreExt::VERSION
|
9
|
+
spec.authors = ['Darthjee']
|
10
|
+
spec.email = ['darthjee@gmail.com']
|
11
|
+
spec.summary = 'Core Extensions'
|
12
|
+
|
13
|
+
spec.files = `git ls-files -z`.split("\x0")
|
14
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.add_runtime_dependency 'activesupport', '~> 5.1.1'
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
21
|
+
spec.add_development_dependency 'rake', '~> 11.3.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
23
|
+
spec.add_development_dependency 'rspec-mocks', '~> 2.99.4'
|
24
|
+
spec.add_development_dependency 'pry', '~> 0.10.4'
|
25
|
+
spec.add_development_dependency 'pry-nav', '~> 0.2.4'
|
26
|
+
spec.add_development_dependency 'simplecov', '~> 0.14.1'
|
27
|
+
end
|
data/lib/array.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'array/hash_builder'
|
2
|
+
|
3
|
+
class Array
|
4
|
+
def chain_map(*methods)
|
5
|
+
result = self
|
6
|
+
result = result.map(&(methods.shift)) until methods.empty?
|
7
|
+
|
8
|
+
return result unless block_given?
|
9
|
+
result.map { |*args| yield(*args) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def as_hash(keys)
|
13
|
+
Array::HashBuilder.new(self, keys).build
|
14
|
+
end
|
15
|
+
|
16
|
+
def random
|
17
|
+
self[rand(size)]
|
18
|
+
end
|
19
|
+
|
20
|
+
def random!
|
21
|
+
self.slice!(rand(size))
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Array::HashBuilder
|
2
|
+
attr_accessor :values, :keys
|
3
|
+
|
4
|
+
def initialize(values, keys)
|
5
|
+
@values = values.dup
|
6
|
+
@keys = keys.dup
|
7
|
+
end
|
8
|
+
|
9
|
+
def build
|
10
|
+
fixes_sizes
|
11
|
+
|
12
|
+
Hash[[keys, values].transpose]
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def fixes_sizes
|
18
|
+
values.concat Array.new(keys.size - values.size) if keys.size > values.size
|
19
|
+
end
|
20
|
+
end
|
data/lib/darthjee.rb
ADDED
data/lib/enumerable.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Enumerable
|
2
|
+
def clean
|
3
|
+
deep_dup.clean!
|
4
|
+
end
|
5
|
+
|
6
|
+
# delete hash or array values if value is nil
|
7
|
+
# ex: { a: nil, b: 2 }.clean! => { b: 2 }
|
8
|
+
def clean!
|
9
|
+
if is_a?(Hash)
|
10
|
+
delete_if { |_k, v| empty_value?(v) }
|
11
|
+
else
|
12
|
+
delete_if { |v| empty_value?(v) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def map_and_find
|
17
|
+
mapped = nil
|
18
|
+
find do |*args|
|
19
|
+
mapped = yield(*args)
|
20
|
+
end
|
21
|
+
mapped || nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def map_and_select
|
25
|
+
mapped = map do |*args|
|
26
|
+
yield(*args)
|
27
|
+
end
|
28
|
+
mapped.select { |e| e }
|
29
|
+
end
|
30
|
+
|
31
|
+
def map_to_hash
|
32
|
+
{}.tap do |hash|
|
33
|
+
each do |element|
|
34
|
+
hash[element] = yield(element)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def empty_value?(v)
|
42
|
+
v.nil? || v.try(:empty?) ||
|
43
|
+
((v.is_a?(Hash) || v.is_a?(Array)) && v.clean!.empty?)
|
44
|
+
end
|
45
|
+
end
|