rcore-ext 0.1.1 → 0.2.0
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.
- data/.gitignore +5 -6
- data/.rspec +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +7 -1
- data/LICENSE.txt +22 -0
- data/README.md +14 -6
- data/Rakefile +6 -0
- data/lib/rcore_ext.rb +3 -2
- data/lib/rcore_ext/array.rb +1 -0
- data/lib/rcore_ext/array/compact.rb +33 -0
- data/lib/rcore_ext/hash.rb +1 -0
- data/lib/rcore_ext/hash/compact.rb +126 -0
- data/lib/rcore_ext/string.rb +2 -0
- data/lib/rcore_ext/string/format.rb +8 -7
- data/lib/rcore_ext/version.rb +1 -1
- data/rcore-ext.gemspec +23 -19
- data/spec/rcore_ext/array/compact_spec.rb +49 -0
- data/spec/rcore_ext/hash/compact_spec.rb +177 -0
- metadata +55 -7
data/.gitignore
CHANGED
@@ -2,8 +2,12 @@
|
|
2
2
|
*.rbc
|
3
3
|
.bundle
|
4
4
|
.config
|
5
|
-
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
6
7
|
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
7
11
|
lib/bundler/man
|
8
12
|
pkg
|
9
13
|
rdoc
|
@@ -11,8 +15,3 @@ spec/reports
|
|
11
15
|
test/tmp
|
12
16
|
test/version_tmp
|
13
17
|
tmp
|
14
|
-
|
15
|
-
# YARD artifacts
|
16
|
-
.yardoc
|
17
|
-
_yardoc
|
18
|
-
doc/
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,14 +1,20 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rcore-ext (0.1.
|
4
|
+
rcore-ext (0.1.1)
|
5
|
+
activesupport
|
5
6
|
base32
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
9
10
|
specs:
|
11
|
+
activesupport (3.2.13)
|
12
|
+
i18n (= 0.6.1)
|
13
|
+
multi_json (~> 1.0)
|
10
14
|
base32 (0.2.0)
|
11
15
|
diff-lcs (1.2.1)
|
16
|
+
i18n (0.6.1)
|
17
|
+
multi_json (1.7.2)
|
12
18
|
rake (10.0.3)
|
13
19
|
rspec (2.13.0)
|
14
20
|
rspec-core (~> 2.13.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Abd ar-Rahman Hamidi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -5,15 +5,17 @@ The `rcore_ext` gem extends several core classes
|
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
add the following to your `Gemfile`:
|
8
|
+
Add this line to your application's Gemfile:
|
11
9
|
|
12
10
|
gem 'rcore-ext'
|
13
11
|
|
14
|
-
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
15
|
|
16
|
-
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install rcore-ext
|
17
19
|
|
18
20
|
## Usage
|
19
21
|
|
@@ -72,4 +74,10 @@ Examples:
|
|
72
74
|
```
|
73
75
|
|
74
76
|
|
75
|
-
##
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rcore_ext.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rcore_ext/array/compact'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
|
3
|
+
class Array
|
4
|
+
# Returns a copy of self with all nil,
|
5
|
+
# empty and blank nested elements removed.
|
6
|
+
#
|
7
|
+
# [1, 2, nil, [3, 4, nil, [5, nil], []], {}].deep_compact #=> [1, 2, [3, 4, [5]]]
|
8
|
+
# [1, 2].deep_compact #=> [1, 2]
|
9
|
+
# [1, 2, ['']].deep_compact #=> [1, 2]
|
10
|
+
# [1, 2, [' ']].deep_compact #=> [1, 2]
|
11
|
+
def deep_compact
|
12
|
+
result = dup.deep_compact!
|
13
|
+
result.nil? ? self : result
|
14
|
+
end
|
15
|
+
|
16
|
+
# Removes nil, empty and blank nested elements from the array.
|
17
|
+
# Returns nil if no changes were made, otherwise returns array.
|
18
|
+
#
|
19
|
+
# [1, 2, nil, [3, 4, nil, [5, nil], []], {}].deep_compact! #=> [1, 2, [3, 4, [5]]]
|
20
|
+
# [1, 2].deep_compact! #=> nil
|
21
|
+
# [1, 2, ['']].deep_compact! #=> [1, 2]
|
22
|
+
# [1, 2, [' ']].deep_compact! #=> [1, 2]
|
23
|
+
def deep_compact!
|
24
|
+
index = 0; changed = false
|
25
|
+
while size > index
|
26
|
+
item = at(index)
|
27
|
+
(changed = true unless item.deep_compact!.nil?) if item.is_a?(Array)
|
28
|
+
(self.delete_at(index); changed = true; next) if item.blank?
|
29
|
+
index += 1
|
30
|
+
end
|
31
|
+
changed ? self : nil
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rcore_ext/hash/compact'
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
# Returns a copy of self with all nil,
|
5
|
+
# empty and blank elements removed.
|
6
|
+
#
|
7
|
+
# You can also pass option to use
|
8
|
+
# * <tt>:keys</tt> - Only remove blank keys from a hash
|
9
|
+
# * <tt>:values</tt> - Only remove blank values from a hash
|
10
|
+
# * <tt>:either</tt> - Remove blank values or keys from a hash
|
11
|
+
# * <tt>:both</tt> - Remove blank values and keys from a hash if both are blank
|
12
|
+
#
|
13
|
+
# {1 => 2}.compact #=> {1 => 2}
|
14
|
+
# {1 => 2, nil => ''}.compact #=> {1 => 2}
|
15
|
+
# {1 => 2, nil => ' '}.compact #=> {1 => 2}
|
16
|
+
#
|
17
|
+
# {1 => 2, nil => 1, 2 => nil}.compact(:keys) #=> {1 => 2, 2 => nil}
|
18
|
+
# {1 => 2, nil => 1, 2 => nil}.compact(:values) #=> {1 => 2, nil => 1}
|
19
|
+
# {1 => 2, nil => 1, 2 => nil}.compact(:either) #=> {1 => 2}
|
20
|
+
# {1 => 2, nil => 1, 2 => nil}.compact(:both) #=> {1 => 2, nil => 1, 2 => nil}
|
21
|
+
def compact(option = nil)
|
22
|
+
@option = option
|
23
|
+
result = {}
|
24
|
+
each_pair do |key, value|
|
25
|
+
result[key] = value unless check_for_blank?(key, value)
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
# Removes nil, empty and blank elements from the hash.
|
31
|
+
# Returns nil if no changes were made, otherwise returns hash.
|
32
|
+
#
|
33
|
+
# You can also pass option to use
|
34
|
+
# * <tt>:keys</tt> - Only remove blank keys from a hash
|
35
|
+
# * <tt>:values</tt> - Only remove blank values from a hash
|
36
|
+
# * <tt>:either</tt> - Remove blank values or keys from a hash
|
37
|
+
# * <tt>:both</tt> - Remove blank values and keys from a hash if both are blank
|
38
|
+
#
|
39
|
+
# {1 => 2}.compact! #=> nil
|
40
|
+
# {1 => 2, nil => ''}.compact! #=> {1 => 2}
|
41
|
+
# {1 => 2, nil => ' '}.compact! #=> {1 => 2}
|
42
|
+
#
|
43
|
+
# {1 => 2, nil => 1, 2 => nil}.compact!(:keys) #=> {1 => 2, 2 => nil}
|
44
|
+
# {1 => 2, nil => 1, 2 => nil}.compact!(:values) #=> {1 => 2, nil => 1}
|
45
|
+
# {1 => 2, nil => 1, 2 => nil}.compact!(:either) #=> {1 => 2}
|
46
|
+
# {1 => 2, nil => 1, 2 => nil}.compact!(:both) #=> {1 => 2, nil => 1, 2 => nil}
|
47
|
+
def compact!(option = nil)
|
48
|
+
@option = option; changed = false
|
49
|
+
each_pair do |key, value|
|
50
|
+
(self.delete(key); changed = true) if check_for_blank?(key, value)
|
51
|
+
end
|
52
|
+
changed ? self : nil
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns a copy of self with all nil,
|
56
|
+
# empty and blank nested elements removed.
|
57
|
+
#
|
58
|
+
# You can also pass option to use
|
59
|
+
# * <tt>:keys</tt> - Only remove blank keys from a hash
|
60
|
+
# * <tt>:values</tt> - Only remove blank values from a hash
|
61
|
+
# * <tt>:either</tt> - Remove blank values or keys from a hash
|
62
|
+
# * <tt>:both</tt> - Remove blank values and keys from a hash if both are blank
|
63
|
+
#
|
64
|
+
# {1 => 2}.deep_compact #=> {1 => 2}
|
65
|
+
# {1 => 2, nil => {nil => ''}}.deep_compact #=> {1 => 2}
|
66
|
+
# {1 => 2, nil => {nil => ' '}}.deep_compact #=> {1 => 2}
|
67
|
+
#
|
68
|
+
# {1 => 2, {nil => 1} => {2 => nil, nil => 1}}.deep_compact(:keys) #=> {1 => 2}
|
69
|
+
# {1 => 2, {nil => 1} => {2 => nil, nil => 1}}.deep_compact(:values) #=> {1 => 2, {nil => 1} => {nil => 1}}
|
70
|
+
# {1 => 2, {nil => 1} => {2 => nil, nil => 1}}.deep_compact(:either) #=> {1 => 2}
|
71
|
+
# {1 => 2, {nil => 1} => {2 => nil, nil => 1}}.deep_compact(:both) #=> {1 => 2, {nil => 1} => {2 => nil, nil => 1}}
|
72
|
+
def deep_compact(option = nil)
|
73
|
+
@option = option
|
74
|
+
result = {}
|
75
|
+
each_pair do |key, value|
|
76
|
+
key = key.deep_compact(option) if key.is_a?(Hash)
|
77
|
+
value = value.deep_compact(option) if value.is_a?(Hash)
|
78
|
+
|
79
|
+
result[key] = value unless check_for_blank?(key, value)
|
80
|
+
end
|
81
|
+
result
|
82
|
+
end
|
83
|
+
|
84
|
+
# Removes nil, empty and blank nested elements from the hash.
|
85
|
+
# Returns nil if no changes were made, otherwise returns hash.
|
86
|
+
#
|
87
|
+
# You can also pass option to use
|
88
|
+
# * <tt>:keys</tt> - Only remove blank keys from a hash
|
89
|
+
# * <tt>:values</tt> - Only remove blank values from a hash
|
90
|
+
# * <tt>:either</tt> - Remove blank values or keys from a hash
|
91
|
+
# * <tt>:both</tt> - Remove blank values and keys from a hash if both are blank
|
92
|
+
#
|
93
|
+
# {1 => 2}.deep_compact! #=> {1 => 2}
|
94
|
+
# {1 => 2, nil => {nil => ''}}.deep_compact! #=> {1 => 2}
|
95
|
+
# {1 => 2, nil => {nil => ' '}}.deep_compact! #=> {1 => 2}
|
96
|
+
#
|
97
|
+
# {1 => 2, {nil => 1} => {2 => nil, nil => 1}}.deep_compact!(:keys) #=> {1 => 2}
|
98
|
+
# {1 => 2, {nil => 1} => {2 => nil, nil => 1}}.deep_compact!(:values) #=> {1 => 2, {nil => 1} => {nil => 1}}
|
99
|
+
# {1 => 2, {nil => 1} => {2 => nil, nil => 1}}.deep_compact!(:either) #=> {1 => 2}
|
100
|
+
# {1 => 2, {nil => 1} => {2 => nil, nil => 1}}.deep_compact!(:both) #=> nil
|
101
|
+
def deep_compact!(option = nil)
|
102
|
+
@option = option; changed = false
|
103
|
+
each_pair do |key, value|
|
104
|
+
(changed = true unless key.deep_compact!(option).nil?) if key.is_a?(Hash)
|
105
|
+
(changed = true unless value.deep_compact!(option).nil?) if value.is_a?(Hash)
|
106
|
+
|
107
|
+
(self.delete(key); changed = true) if check_for_blank?(key, value)
|
108
|
+
end
|
109
|
+
changed ? self : nil
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
def check_for_blank?(key, value)
|
114
|
+
@option ||= :both
|
115
|
+
case @option.to_sym
|
116
|
+
when :keys
|
117
|
+
key.blank?
|
118
|
+
when :values
|
119
|
+
value.blank?
|
120
|
+
when :either
|
121
|
+
key.blank? || value.blank?
|
122
|
+
else :both
|
123
|
+
key.blank? && value.blank?
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'base64'
|
2
|
-
require 'base32'
|
3
|
-
|
4
1
|
class String
|
5
2
|
@@common_formats = {bin: 'B*', hex: 'H*'}
|
6
3
|
|
@@ -8,8 +5,8 @@ class String
|
|
8
5
|
# Supported formats: :bin, :hex, :base32, :base64
|
9
6
|
#
|
10
7
|
# You can also pass options for Base64 to use
|
11
|
-
# *
|
12
|
-
# *
|
8
|
+
# * <tt>:strict</tt>
|
9
|
+
# * <tt>:url_safe</tt>
|
13
10
|
#
|
14
11
|
# "hello world!".decode_string(:hex) #=> 68656c6c6f20776f726c6421
|
15
12
|
# "hi".decode_string(:bin) #=> 0110100001101001
|
@@ -38,8 +35,8 @@ class String
|
|
38
35
|
# Supported formats: :bin, :hex, :base32, :base64
|
39
36
|
#
|
40
37
|
# You can also pass options for Base64 to use
|
41
|
-
# *
|
42
|
-
# *
|
38
|
+
# * <tt>:strict</tt>
|
39
|
+
# * <tt>:url_safe</tt>
|
43
40
|
#
|
44
41
|
# "68656c6c6f20776f726c6421".decode_string(:hex) #=> hello world!
|
45
42
|
# "0110100001101001".decode_string(:bin) #=> hi
|
@@ -74,14 +71,17 @@ class String
|
|
74
71
|
end
|
75
72
|
|
76
73
|
def base32_encode
|
74
|
+
require 'base32'
|
77
75
|
Base32.encode(self)
|
78
76
|
end
|
79
77
|
|
80
78
|
def base32_decode
|
79
|
+
require 'base32'
|
81
80
|
Base32.decode(self)
|
82
81
|
end
|
83
82
|
|
84
83
|
def base64_encode
|
84
|
+
require 'base64'
|
85
85
|
if @options[:strict]
|
86
86
|
Base64.strict_encode64(self)
|
87
87
|
elsif @options[:url_safe]
|
@@ -92,6 +92,7 @@ class String
|
|
92
92
|
end
|
93
93
|
|
94
94
|
def base64_decode
|
95
|
+
require 'base64'
|
95
96
|
if @options[:strict]
|
96
97
|
Base64.strict_decode64(self)
|
97
98
|
elsif @options[:url_safe]
|
data/lib/rcore_ext/version.rb
CHANGED
data/rcore-ext.gemspec
CHANGED
@@ -1,25 +1,29 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rcore_ext/version'
|
3
5
|
|
4
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rcore-ext"
|
8
|
+
spec.version = RCoreExt::VERSION
|
9
|
+
spec.authors = ["Abd ar-Rahman Hamidi"]
|
10
|
+
spec.email = ["bakhtiyor.h@gmail.com"]
|
11
|
+
spec.description = %q{Ruby Core Extensions in a gem}
|
12
|
+
spec.summary = %q{Ruby Core Extension}
|
13
|
+
spec.homepage = "https://github.com/hbakhtiyor/rcore-ext"
|
14
|
+
spec.license = "MIT"
|
5
15
|
|
6
|
-
|
7
|
-
s.authors = ["Bakhtiyor Homidov"]
|
8
|
-
s.email = ["bakhtiyor.h@gmail.com"]
|
9
|
-
s.homepage = "https://github.com/hbakhtiyor/rcore-ext"
|
10
|
-
s.description = %q{Ruby Core Extensions in a gem}
|
11
|
-
s.summary = %q{Ruby Core Extension}
|
12
|
-
|
13
|
-
s.rubyforge_project = "rcore-ext"
|
16
|
+
spec.rubyforge_project = "rcore-ext"
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
s.version = RCoreExt::VERSION
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
23
26
|
|
24
|
-
|
27
|
+
spec.add_runtime_dependency "base32"
|
28
|
+
spec.add_runtime_dependency "activesupport"
|
25
29
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rcore_ext/array/compact'
|
3
|
+
|
4
|
+
describe "Array deep compact functionality" do
|
5
|
+
describe "respond to additinal methods" do
|
6
|
+
it "should respond to deep_compact method" do
|
7
|
+
[].should respond_to :deep_compact
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should respond to deep_compact! method" do
|
11
|
+
[].should respond_to :deep_compact!
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "deep compact functionality" do
|
16
|
+
let(:nested_nils) { [nil, nil, [nil, 0, [nil, 1, [2, nil, nil, [[[[[nil, [[]]]]]]], 3]]], 0, 1, 2, [nil, nil, [1, 0, nil], [nil], [' '], [], {}, 1]] }
|
17
|
+
let(:deleted_nils) { [[0, [1, [2, 3]]], 0, 1, 2, [[1, 0], 1]] }
|
18
|
+
|
19
|
+
describe "not mutates itself" do
|
20
|
+
it "should delele nested nils" do
|
21
|
+
nested_nils.deep_compact.should == deleted_nils
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should not mutates itself" do
|
25
|
+
nested_nils.deep_compact
|
26
|
+
nested_nils.should_not == deleted_nils
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "mutates itself" do
|
31
|
+
it "should delete nested nils" do
|
32
|
+
nested_nils.deep_compact!.should == deleted_nils
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return nil if no changes" do
|
36
|
+
[1, 2, 3, [4, [5]]].deep_compact!.should be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not return nil if changes" do
|
40
|
+
[1, 2, 3, [4, []]].deep_compact!.should_not be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should mutates itself" do
|
44
|
+
nested_nils.deep_compact!
|
45
|
+
nested_nils.should == deleted_nils
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rcore_ext/hash/compact'
|
3
|
+
|
4
|
+
describe "Hash additinal functionality" do
|
5
|
+
describe "respond to additinal methods" do
|
6
|
+
it "should respond to compact method" do
|
7
|
+
{}.should respond_to :compact
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should respond to compact! method" do
|
11
|
+
{}.should respond_to :compact!
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should respond to deep_compact method" do
|
15
|
+
{}.should respond_to :deep_compact
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should respond to deep_compact! method" do
|
19
|
+
{}.should respond_to :deep_compact!
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "compact functionality" do
|
24
|
+
let(:nils) { { nil => 0, 0 => nil, 1 => 1 } }
|
25
|
+
let(:nils_both) { { nil => nil, 0 => nil, 1 => 1 } }
|
26
|
+
|
27
|
+
let(:delete_keys) { { 0 => nil, 1 => 1 } }
|
28
|
+
let(:delete_values) { { nil => 0, 1 => 1 } }
|
29
|
+
let(:delete_either) { { 1 => 1 } }
|
30
|
+
let(:delete_both) { { 0 => nil, 1 => 1 } }
|
31
|
+
|
32
|
+
describe "not mutates itself" do
|
33
|
+
it "with keys options" do
|
34
|
+
nils.compact(:keys).should == delete_keys
|
35
|
+
end
|
36
|
+
|
37
|
+
it "with values option" do
|
38
|
+
nils.compact(:values).should == delete_values
|
39
|
+
end
|
40
|
+
|
41
|
+
it "with either option" do
|
42
|
+
nils.compact(:either).should == delete_either
|
43
|
+
end
|
44
|
+
|
45
|
+
it "with both option" do
|
46
|
+
nils_both.compact.should == delete_both
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not mutates itself" do
|
50
|
+
nils_both.compact
|
51
|
+
nils_both.should_not == delete_both
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "mutates itself" do
|
56
|
+
it "with keys options" do
|
57
|
+
nils.compact!(:keys).should == delete_keys
|
58
|
+
end
|
59
|
+
|
60
|
+
it "with values option" do
|
61
|
+
nils.compact!(:values).should == delete_values
|
62
|
+
end
|
63
|
+
|
64
|
+
it "with either option" do
|
65
|
+
nils.compact!(:either).should == delete_either
|
66
|
+
end
|
67
|
+
|
68
|
+
it "with both option" do
|
69
|
+
nils_both.compact!.should == delete_both
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return nil if no changes" do
|
73
|
+
{a: :b}.compact!.should be_nil
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should mutates itself" do
|
77
|
+
nils_both.compact!
|
78
|
+
nils_both.should == delete_both
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "deep compact functionality" do
|
84
|
+
let(:nested_nils) { { nil => 0, 0 => nil, 2 => { nil => 0, 3 => { nil => 1, 4 => { nil => 0, 1 => nil },
|
85
|
+
1 => nil, 2 => 2, 3 => nil }, 0 => nil, 1 => 0, 2 => nil }, 1 => 1 } }
|
86
|
+
let(:nested_key_nils_1) { { { nil => 0 } => { nil => nil }, nil => 0, 0 => nil, 2 => { nil => 0, 3 => { nil => 1, 4 => { nil => nil, 1 => nil },
|
87
|
+
1 => nil, 2 => 2, 3 => nil }, 0 => nil, 1 => 0, 2 => nil }, 1 => 1 } }
|
88
|
+
let(:nested_key_nils_2) { { 1 => 2, { nil => 1 } => { 2 => nil, nil => 1 } } }
|
89
|
+
let(:nested_nils_both) { { nil => nil, 2 => { 3 => { nil => nil, 0 => nil }, nil => nil, 0 => nil }, 0 => nil, 1 => 1 } }
|
90
|
+
|
91
|
+
let(:delete_keys) { { 0 => nil, 2 => { 3 => { 4 => { 1 => nil }, 1 => nil, 2 => 2, 3 => nil }, 0 => nil, 1 => 0, 2 => nil }, 1 => 1 } }
|
92
|
+
let(:delete_values) { { nil => 0, 2 => { nil => 0, 3 => { nil => 1, 4 => { nil => 0 }, 2 => 2 }, 1 => 0 }, 1 => 1 } }
|
93
|
+
let(:delete_either) { { 2 => { 3 => { 2 => 2 }, 1 => 0 }, 1 => 1 } }
|
94
|
+
let(:delete_both) { { 2 => { 3 => { 0 => nil }, 0 => nil }, 0 => nil, 1 => 1 } }
|
95
|
+
|
96
|
+
describe "not mutates itself" do
|
97
|
+
it "with keys option" do
|
98
|
+
nested_nils.deep_compact(:keys).should == delete_keys
|
99
|
+
end
|
100
|
+
|
101
|
+
it "first nested key with keys option" do
|
102
|
+
nested_key_nils_1.deep_compact(:keys).should == delete_keys
|
103
|
+
end
|
104
|
+
|
105
|
+
it "second nested key with keys option" do
|
106
|
+
nested_key_nils_2.deep_compact(:keys).should == {1 => 2}
|
107
|
+
end
|
108
|
+
|
109
|
+
it "second nested key with either option" do
|
110
|
+
nested_key_nils_2.deep_compact(:either).should == {1 => 2}
|
111
|
+
end
|
112
|
+
|
113
|
+
it "with values option" do
|
114
|
+
nested_nils.deep_compact(:values).should == delete_values
|
115
|
+
end
|
116
|
+
|
117
|
+
it "with either option" do
|
118
|
+
nested_nils.deep_compact(:either).should == delete_either
|
119
|
+
end
|
120
|
+
|
121
|
+
it "with both option" do
|
122
|
+
nested_nils_both.deep_compact.should == delete_both
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should not mutates itself" do
|
126
|
+
nested_nils_both.deep_compact
|
127
|
+
nested_nils_both.should_not == delete_both
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "mutates itself" do
|
132
|
+
it "with keys option" do
|
133
|
+
nested_nils.deep_compact!(:keys).should == delete_keys
|
134
|
+
end
|
135
|
+
|
136
|
+
it "first nested key with keys option" do
|
137
|
+
pending
|
138
|
+
nested_key_nils_1.deep_compact!(:keys).should == delete_keys
|
139
|
+
end
|
140
|
+
|
141
|
+
it "second nested key with keys option" do
|
142
|
+
pending
|
143
|
+
nested_key_nils_2.deep_compact!(:keys).should == {1 => 2}
|
144
|
+
end
|
145
|
+
|
146
|
+
it "second nested key with either option" do
|
147
|
+
pending
|
148
|
+
nested_key_nils_2.deep_compact!(:either).should == {1 => 2}
|
149
|
+
end
|
150
|
+
|
151
|
+
it "with values option" do
|
152
|
+
nested_nils.deep_compact!(:values).should == delete_values
|
153
|
+
end
|
154
|
+
|
155
|
+
it "with either option" do
|
156
|
+
nested_nils.deep_compact!(:either).should == delete_either
|
157
|
+
end
|
158
|
+
|
159
|
+
it "with both option" do
|
160
|
+
nested_nils_both.deep_compact!.should == delete_both
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should return nil if no changes" do
|
164
|
+
{a: :b, c: {d: {e: :f}}}.deep_compact!.should be_nil
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should not return nil if changes" do
|
168
|
+
{a: :b, c: {d: {nil => nil}}}.deep_compact!.should_not be_nil
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should mutates itself" do
|
172
|
+
nested_nils_both.deep_compact!
|
173
|
+
nested_nils_both.should == delete_both
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
metadata
CHANGED
@@ -1,18 +1,34 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rcore-ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- Abd ar-Rahman Hamidi
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-04-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
17
33
|
none: false
|
18
34
|
requirements:
|
@@ -28,7 +44,7 @@ dependencies:
|
|
28
44
|
- !ruby/object:Gem::Version
|
29
45
|
version: '0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
47
|
+
name: rspec
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
33
49
|
none: false
|
34
50
|
requirements:
|
@@ -59,6 +75,22 @@ dependencies:
|
|
59
75
|
- - ! '>='
|
60
76
|
- !ruby/object:Gem::Version
|
61
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: activesupport
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
62
94
|
description: Ruby Core Extensions in a gem
|
63
95
|
email:
|
64
96
|
- bakhtiyor.h@gmail.com
|
@@ -67,20 +99,31 @@ extensions: []
|
|
67
99
|
extra_rdoc_files: []
|
68
100
|
files:
|
69
101
|
- .gitignore
|
102
|
+
- .rspec
|
70
103
|
- CHANGELOG.md
|
71
104
|
- Gemfile
|
72
105
|
- Gemfile.lock
|
106
|
+
- LICENSE.txt
|
73
107
|
- README.md
|
108
|
+
- Rakefile
|
74
109
|
- lib/rcore_ext.rb
|
110
|
+
- lib/rcore_ext/array.rb
|
111
|
+
- lib/rcore_ext/array/compact.rb
|
112
|
+
- lib/rcore_ext/hash.rb
|
113
|
+
- lib/rcore_ext/hash/compact.rb
|
114
|
+
- lib/rcore_ext/string.rb
|
75
115
|
- lib/rcore_ext/string/format.rb
|
76
116
|
- lib/rcore_ext/string/numeric.rb
|
77
117
|
- lib/rcore_ext/version.rb
|
78
118
|
- rcore-ext.gemspec
|
119
|
+
- spec/rcore_ext/array/compact_spec.rb
|
120
|
+
- spec/rcore_ext/hash/compact_spec.rb
|
79
121
|
- spec/rcore_ext/string/format_spec.rb
|
80
122
|
- spec/rcore_ext/string/numeric_spec.rb
|
81
123
|
- spec/spec_helper.rb
|
82
124
|
homepage: https://github.com/hbakhtiyor/rcore-ext
|
83
|
-
licenses:
|
125
|
+
licenses:
|
126
|
+
- MIT
|
84
127
|
post_install_message:
|
85
128
|
rdoc_options: []
|
86
129
|
require_paths:
|
@@ -103,4 +146,9 @@ rubygems_version: 1.8.23
|
|
103
146
|
signing_key:
|
104
147
|
specification_version: 3
|
105
148
|
summary: Ruby Core Extension
|
106
|
-
test_files:
|
149
|
+
test_files:
|
150
|
+
- spec/rcore_ext/array/compact_spec.rb
|
151
|
+
- spec/rcore_ext/hash/compact_spec.rb
|
152
|
+
- spec/rcore_ext/string/format_spec.rb
|
153
|
+
- spec/rcore_ext/string/numeric_spec.rb
|
154
|
+
- spec/spec_helper.rb
|