bidu-core_ext 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +49 -0
- data/LICENSE +22 -0
- data/README.md +143 -0
- data/Rakefile +7 -0
- data/core_ext.gemspec +25 -0
- data/lib/array.rb +15 -0
- data/lib/array/hash_builder.rb +20 -0
- data/lib/bidu.rb +4 -0
- data/lib/bidu/core_ext.rb +12 -0
- data/lib/bidu/core_ext/version.rb +5 -0
- data/lib/enumerable.rb +30 -0
- data/lib/hash.rb +161 -0
- data/lib/hash/deep_hash_constructor.rb +83 -0
- data/lib/hash/key_changer.rb +68 -0
- data/lib/hash/value_changer.rb +52 -0
- data/lib/numeric.rb +6 -0
- data/lib/symbol.rb +5 -0
- data/spec/lib/array_spec.rb +93 -0
- data/spec/lib/enumerable_spec.rb +31 -0
- data/spec/lib/hash/deep_hash_constructor_spec.rb +167 -0
- data/spec/lib/hash_spec.rb +287 -0
- data/spec/lib/numeric_spec.rb +61 -0
- data/spec/lib/symbol_spec.rb +25 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/support/shared_examples/chain_fetch.rb +88 -0
- data/spec/support/shared_examples/clean.rb +143 -0
- data/spec/support/shared_examples/hash_keys_changer.rb +92 -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/remap.rb +89 -0
- data/spec/support/shared_examples/value_changer.rb +63 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5a917ec3ef22ae183e9cbb569e438a80456800ad
|
4
|
+
data.tar.gz: dc600d8c5f69a6776e77e8aacddbef83d446cd52
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 97f018e78cf3b0eeff67540fc48c4c2e0b19a99db46edde05fe4e22708e4856b8e0cf1314f7b2a97f538d55e38ed527c0b12dad122f46208775f91e8a71a6560
|
7
|
+
data.tar.gz: f3825dd7fd3d241a4daa9312d8748bfa580d8e1b37160b8be2bc183050239bd90f423790b1e1880b5fa09bf78d9380ee3317708a0bf5fb3bd98df066f1dbef35
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
coverage
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
bidu-core_ext (1.0.0)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (4.2.1)
|
11
|
+
i18n (~> 0.7)
|
12
|
+
json (~> 1.7, >= 1.7.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
diff-lcs (1.2.5)
|
17
|
+
docile (1.1.5)
|
18
|
+
i18n (0.7.0)
|
19
|
+
json (1.8.2)
|
20
|
+
minitest (5.6.1)
|
21
|
+
multi_json (1.10.1)
|
22
|
+
rake (10.3.2)
|
23
|
+
rspec (2.99.0)
|
24
|
+
rspec-core (~> 2.99.0)
|
25
|
+
rspec-expectations (~> 2.99.0)
|
26
|
+
rspec-mocks (~> 2.99.0)
|
27
|
+
rspec-core (2.99.2)
|
28
|
+
rspec-expectations (2.99.2)
|
29
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
30
|
+
rspec-mocks (2.99.2)
|
31
|
+
simplecov (0.9.1)
|
32
|
+
docile (~> 1.1.0)
|
33
|
+
multi_json (~> 1.0)
|
34
|
+
simplecov-html (~> 0.8.0)
|
35
|
+
simplecov-html (0.8.0)
|
36
|
+
thread_safe (0.3.5)
|
37
|
+
tzinfo (1.2.2)
|
38
|
+
thread_safe (~> 0.1)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
bidu-core_ext!
|
45
|
+
bundler (~> 1.6)
|
46
|
+
rake
|
47
|
+
rspec (~> 2.14)
|
48
|
+
rspec-mocks
|
49
|
+
simplecov
|
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,143 @@
|
|
1
|
+
Core_Ext
|
2
|
+
========
|
3
|
+
|
4
|
+
This project adds some new methods to the core ruby classes
|
5
|
+
|
6
|
+
## Array
|
7
|
+
### chain_map
|
8
|
+
applies map in a chain
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
array = [ :a, :long_name, :sym ]
|
12
|
+
array.chain_map(:to_s, :size, :to_s)
|
13
|
+
[ '1', '9', '3' ]
|
14
|
+
```
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
array = [ :a, :long_name, :sym ]
|
18
|
+
array.chain_map(:to_s, :size) { |v| "final: #{v}" }
|
19
|
+
[ 'final: 1', 'final: 9', 'final: 3' ]
|
20
|
+
```
|
21
|
+
|
22
|
+
### as_hash
|
23
|
+
Creates a hash from the array using the argumen array as keys
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
[1, 2, 3].as_hash %w(a b c)
|
27
|
+
```
|
28
|
+
returns
|
29
|
+
```ruby
|
30
|
+
{ 'a' => 1, 'b' => 2, 'c' => 3 } }
|
31
|
+
```
|
32
|
+
|
33
|
+
## Hash
|
34
|
+
### chain_fetch
|
35
|
+
Applies fetch in a chain
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
{ a: { b: { c: { d: 10 } } } }.chain_fetch(:a, :b, :c, :d)
|
39
|
+
10
|
40
|
+
```
|
41
|
+
```ruby
|
42
|
+
h = { a: { b: { c: { d: 10 } } } }
|
43
|
+
h.chain_fetch(:a, :x, :y, :z) { |key, missed_keys| "returned #{key}" }
|
44
|
+
'returned x'
|
45
|
+
```
|
46
|
+
|
47
|
+
###squash
|
48
|
+
Squash a deep hash into a simple level hash
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
{ a: { b:1 } }.squash
|
52
|
+
```
|
53
|
+
returns
|
54
|
+
```ruby
|
55
|
+
{ 'a.b' => 1 }
|
56
|
+
```
|
57
|
+
|
58
|
+
### to_deep_hash
|
59
|
+
Changes a hash spliting keys into inner hashs
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
{ 'a.b' => 1 }.to_deep_hash
|
63
|
+
```
|
64
|
+
returns
|
65
|
+
```ruby
|
66
|
+
{ 'a' => { 'b' => 1 } }
|
67
|
+
```
|
68
|
+
|
69
|
+
### camelize_keys
|
70
|
+
Change the keys camelizing them
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
{ ca_b: 1 }.camelize_keys
|
74
|
+
```
|
75
|
+
returns
|
76
|
+
```ruby
|
77
|
+
{ CaB: 1 }
|
78
|
+
```
|
79
|
+
|
80
|
+
### change_keys
|
81
|
+
Change the array keys using a block
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
{ ca_b: 1 }.change_keys { |k| k.to_s.upcase }
|
85
|
+
```
|
86
|
+
returns
|
87
|
+
```ruby
|
88
|
+
{ 'CA_B' => 1 }
|
89
|
+
```
|
90
|
+
|
91
|
+
### change_values
|
92
|
+
Change the values of the array
|
93
|
+
```ruby
|
94
|
+
{ a: 1 }.change_keys { |v| (v+1).to_s }
|
95
|
+
```
|
96
|
+
returns
|
97
|
+
```ruby
|
98
|
+
{ a: '2' }
|
99
|
+
```
|
100
|
+
|
101
|
+
### prepend_to_keys
|
102
|
+
Change each keys prepending an string
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
{ key: 1 }.prepend_to_keys 'scope:'
|
106
|
+
```
|
107
|
+
returns
|
108
|
+
```ruby
|
109
|
+
{ :'scope:key' => 1 }
|
110
|
+
```
|
111
|
+
### append_to_keys
|
112
|
+
Change each keys appending an string
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
{ key: 1 }.append_to_keys 's'
|
116
|
+
```
|
117
|
+
returns
|
118
|
+
```ruby
|
119
|
+
{ keys: 1 }
|
120
|
+
```
|
121
|
+
|
122
|
+
### sort_keys
|
123
|
+
Sort the hash usig the keys
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
{ b:1, a:2 }.sort_keys
|
127
|
+
```
|
128
|
+
returns
|
129
|
+
```ruby
|
130
|
+
{ a:2, b:1 }
|
131
|
+
```
|
132
|
+
|
133
|
+
## Enumerable
|
134
|
+
|
135
|
+
## clean!
|
136
|
+
CLeans empty values from a hash
|
137
|
+
```ruby
|
138
|
+
{ a: 1, b: [], c: nil, d: {}, e: '', f: { b: [], c: nil, d: {}, e: '' } }.clean!
|
139
|
+
```
|
140
|
+
returns
|
141
|
+
```ruby
|
142
|
+
{}
|
143
|
+
```
|
data/Rakefile
ADDED
data/core_ext.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bidu/core_ext/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'bidu-core_ext'
|
8
|
+
spec.version = Bidu::CoreExt::VERSION
|
9
|
+
spec.authors = ['Bidu Developers']
|
10
|
+
spec.email = ['dev@bidu.com.br']
|
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'
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
23
|
+
spec.add_development_dependency 'rspec-mocks'
|
24
|
+
spec.add_development_dependency 'simplecov'
|
25
|
+
end
|
data/lib/array.rb
ADDED
@@ -0,0 +1,15 @@
|
|
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
|
+
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/bidu.rb
ADDED
data/lib/enumerable.rb
ADDED
@@ -0,0 +1,30 @@
|
|
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
|
+
private
|
25
|
+
|
26
|
+
def empty_value?(v)
|
27
|
+
v.nil? || v.try(:empty?) ||
|
28
|
+
((v.is_a?(Hash) || v.is_a?(Array)) && v.clean!.empty?)
|
29
|
+
end
|
30
|
+
end
|
data/lib/hash.rb
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
require 'hash/value_changer'
|
2
|
+
require 'hash/deep_hash_constructor'
|
3
|
+
require 'hash/key_changer'
|
4
|
+
|
5
|
+
class Hash
|
6
|
+
def chain_fetch(*keys)
|
7
|
+
value = self
|
8
|
+
|
9
|
+
if block_given?
|
10
|
+
value = value.fetch(keys.shift) do |*args|
|
11
|
+
missed_keys = keys
|
12
|
+
keys = []
|
13
|
+
yield(*(args + [missed_keys]))
|
14
|
+
end until keys.empty?
|
15
|
+
else
|
16
|
+
value = value.fetch(keys.shift) until keys.empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
value
|
20
|
+
end
|
21
|
+
|
22
|
+
def squash
|
23
|
+
{}.tap do |hash|
|
24
|
+
each do |key, value|
|
25
|
+
if value.is_a? Hash
|
26
|
+
value.squash.each do |k, v|
|
27
|
+
new_key = [key, k].join('.')
|
28
|
+
hash[new_key] = v
|
29
|
+
end
|
30
|
+
else
|
31
|
+
hash[key] = value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def remap_keys(remap)
|
38
|
+
dup.remap_keys!(remap)
|
39
|
+
end
|
40
|
+
|
41
|
+
def remap_keys!(remap)
|
42
|
+
new_hash = {}
|
43
|
+
remap.each do |o, n|
|
44
|
+
new_hash[n] = delete o
|
45
|
+
end
|
46
|
+
merge! new_hash
|
47
|
+
end
|
48
|
+
|
49
|
+
def lower_camelize_keys(options = {})
|
50
|
+
dup.lower_camelize_keys!(options)
|
51
|
+
end
|
52
|
+
|
53
|
+
def lower_camelize_keys!(options = {})
|
54
|
+
options = options.merge({ uppercase_first_letter: false })
|
55
|
+
|
56
|
+
camelize_keys!(options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def camelize_keys(options = {})
|
60
|
+
dup.camelize_keys!(options)
|
61
|
+
end
|
62
|
+
|
63
|
+
def camelize_keys!(options = {})
|
64
|
+
Hash::KeyChanger.new(self).camelize_keys(options)
|
65
|
+
end
|
66
|
+
|
67
|
+
def exclusive_merge(hash)
|
68
|
+
dup.exclusive_merge!(hash)
|
69
|
+
end
|
70
|
+
|
71
|
+
def exclusive_merge!(hash)
|
72
|
+
merge!(hash.slice(*keys))
|
73
|
+
end
|
74
|
+
|
75
|
+
# change all keys returning the new map
|
76
|
+
# options: { recursive: true }
|
77
|
+
# ex: { "a":1 }.change_keys{ |key| key.upcase } == { "A":1 }
|
78
|
+
def change_keys(options = {}, &block)
|
79
|
+
deep_dup.change_keys!(options, &block)
|
80
|
+
end
|
81
|
+
|
82
|
+
# change all keys returning the new map
|
83
|
+
# options: { recursive: true }
|
84
|
+
# ex: { "a":1 }.change_keys{ |key| key.upcase } == { "A":1 }
|
85
|
+
def change_keys!(options = {}, &block)
|
86
|
+
Hash::KeyChanger.new(self).change_keys(options, &block)
|
87
|
+
end
|
88
|
+
|
89
|
+
# prepend a string to all keys
|
90
|
+
# options {
|
91
|
+
# recursive: true,
|
92
|
+
# type: :keep [keep, string, symbol] (key type to be returned)
|
93
|
+
# }
|
94
|
+
# ex: { :a => 1, "b"=> 2 }.prepend_to_keys("foo_") == { :foo_a => 1, "foo_b"=> 2 }
|
95
|
+
def prepend_to_keys(str, options = {})
|
96
|
+
change_key_text(options) do |key|
|
97
|
+
"#{str}#{key}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# append a string to all keys
|
102
|
+
# options {
|
103
|
+
# recursive: true,
|
104
|
+
# type: :keep [keep, string, symbol] (key type to be returned)
|
105
|
+
# }
|
106
|
+
# ex: { :a => 1, "b"=> 2 }.append_to_keys("_bar") == { :a_bar => 1, "b_bar"=> 2 }
|
107
|
+
def append_to_keys(str, options = {})
|
108
|
+
change_key_text(options) do |key|
|
109
|
+
"#{key}#{str}"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# sorts keys for hash
|
114
|
+
# options: { recursive: true }
|
115
|
+
# ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }
|
116
|
+
def sort_keys(options = {})
|
117
|
+
options = {
|
118
|
+
recursive: true
|
119
|
+
}.merge(options)
|
120
|
+
|
121
|
+
{}.tap do |hash|
|
122
|
+
keys.sort.each do |key|
|
123
|
+
value = self[key]
|
124
|
+
hash[key] = value unless value.is_a?(Hash) && options[:recursive]
|
125
|
+
hash[key] = value.sort_keys(options) if value.is_a?(Hash) && options[:recursive]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# creates a new hash with changes in its values
|
131
|
+
# options: {
|
132
|
+
# recursive: true,
|
133
|
+
# skip_hash:true
|
134
|
+
# }
|
135
|
+
# ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 }
|
136
|
+
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s } == { a:"1", b:"{ c=>1 }
|
137
|
+
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s } == { a:"1", b:{ c=>"1" } }
|
138
|
+
def change_values(options = {}, &block)
|
139
|
+
deep_dup.change_values!(options, &block)
|
140
|
+
end
|
141
|
+
|
142
|
+
def change_values!(options = {}, &block)
|
143
|
+
Hash::ValueChanger.new(options, &block).change(self)
|
144
|
+
end
|
145
|
+
|
146
|
+
def to_deep_hash(separator = '.')
|
147
|
+
Hash::DeepHashConstructor.new(separator).deep_hash(self)
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
# changes the text of the keys
|
153
|
+
# options {
|
154
|
+
# recursive: true,
|
155
|
+
# type: :keep [keep, string, symbol] (key type to be returned)
|
156
|
+
# }
|
157
|
+
# ex: { :a => 1, "b"=> 2 }.change_key_text{ |key| key.upcase } == { :A => 1, "B"=> 2 }
|
158
|
+
def change_key_text(options = {}, &block)
|
159
|
+
Hash::KeyChanger.new(self).change_text(options, &block)
|
160
|
+
end
|
161
|
+
end
|