more_core_extensions 3.2.0 → 3.3.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +9 -0
  3. data/.rubocop.yml +4 -0
  4. data/.rubocop_local.yml +0 -0
  5. data/.travis.yml +18 -0
  6. data/CHANGELOG.md +54 -0
  7. data/Gemfile +8 -0
  8. data/README.md +21 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/more_core_extensions/all.rb +2 -0
  13. data/lib/more_core_extensions/core_ext/hash/sorting.rb +20 -0
  14. data/lib/more_core_extensions/core_ext/hash.rb +2 -1
  15. data/lib/more_core_extensions/core_ext/object/descendants.rb +17 -0
  16. data/lib/more_core_extensions/core_ext/object.rb +1 -0
  17. data/lib/more_core_extensions/core_ext/range/step_value.rb +45 -0
  18. data/lib/more_core_extensions/core_ext/range.rb +1 -0
  19. data/lib/more_core_extensions/core_ext/string/decimal_suffix.rb +17 -0
  20. data/lib/more_core_extensions/core_ext/string.rb +1 -0
  21. data/lib/more_core_extensions/core_ext/symbol/to_i.rb +9 -0
  22. data/lib/more_core_extensions/core_ext/symbol.rb +1 -0
  23. data/lib/more_core_extensions/version.rb +1 -1
  24. data/more_core_extensions.gemspec +32 -0
  25. metadata +36 -44
  26. data/spec/core_ext/array/deletes_spec.rb +0 -15
  27. data/spec/core_ext/array/duplicates_spec.rb +0 -9
  28. data/spec/core_ext/array/element_counts_spec.rb +0 -16
  29. data/spec/core_ext/array/inclusions_spec.rb +0 -51
  30. data/spec/core_ext/array/math_spec.rb +0 -16
  31. data/spec/core_ext/array/nested_spec.rb +0 -183
  32. data/spec/core_ext/array/random_spec.rb +0 -21
  33. data/spec/core_ext/array/stretch_spec.rb +0 -94
  34. data/spec/core_ext/array/tableize_spec.rb +0 -156
  35. data/spec/core_ext/hash/deletes_spec.rb +0 -15
  36. data/spec/core_ext/hash/nested_spec.rb +0 -232
  37. data/spec/core_ext/numeric/clamp_spec.rb +0 -17
  38. data/spec/core_ext/numeric/math_spec.rb +0 -6
  39. data/spec/core_ext/numeric/rounding_spec.rb +0 -19
  40. data/spec/core_ext/object/namespace_spec.rb +0 -30
  41. data/spec/core_ext/string/formats_spec.rb +0 -77
  42. data/spec/core_ext/string/hex_dump_spec.rb +0 -84
  43. data/spec/core_ext/string/iec60027_2_spec.rb +0 -13
  44. data/spec/spec_helper.rb +0 -85
@@ -1,183 +0,0 @@
1
- shared_examples_for "core_ext/array/nested will not modify arguments" do |meth|
2
- it "will not modify arguments" do
3
- args = (meth == :store_path ? [1] : [])
4
-
5
- key = [3, 0, 1, 2]
6
- key2 = key.dup
7
- array.send(meth, key2, *args)
8
- expect(key2).to eq(key)
9
-
10
- key = [4, 0, 1]
11
- key2 = key.dup
12
- array.send(meth, key2, *args)
13
- expect(key2).to eq(key)
14
- end
15
- end
16
-
17
- shared_examples_for "core_ext/array/nested" do
18
- context '#fetch_path' do
19
- it "with various values" do
20
- expect(array.fetch_path(0)).to eq(1)
21
- expect(array.fetch_path(1)).to eq([])
22
- expect(array.fetch_path(1, 0)).to be_nil
23
- expect(array.fetch_path(1, 0, 1)).to be_nil
24
- expect(array.fetch_path(2)).to eq([2])
25
- expect(array.fetch_path(2, 0)).to eq(2)
26
- expect(array.fetch_path(-4, 0)).to eq(2)
27
- expect(array.fetch_path(2, 0, 1)).to be_nil
28
- expect(array.fetch_path(3, 0, 0, 0)).to eq(3)
29
- expect(array.fetch_path(3, 0, 1, 999)).to be_nil
30
- expect(array.fetch_path(3, 0, 1, 2, 3)).to be_nil
31
- expect(array.fetch_path(4)).to eq([nil, nil, nil, nil])
32
- expect(array.fetch_path(4, 0)).to be_nil
33
- expect(array.fetch_path(4, 0, 1)).to be_nil
34
- expect(array.fetch_path(5)).to eq([])
35
- expect(array.fetch_path(5, 0)).to be_nil
36
- expect(array.fetch_path(5, 0, 1)).to be_nil
37
- end
38
-
39
- it "with a nil value should raise ArgumentError" do
40
- expect { array.fetch_path(nil) }.to raise_error(ArgumentError)
41
- expect { array.fetch_path(3, nil, 0) }.to raise_error(ArgumentError)
42
- end
43
-
44
- it "with invalid values" do
45
- expect { array.fetch_path }.to raise_error(ArgumentError)
46
- end
47
-
48
- include_examples "core_ext/array/nested will not modify arguments", :fetch_path
49
- end
50
-
51
- context "#store_path" do
52
- it "on an empty array" do
53
- a = described_class.new
54
- a.store_path(0, 1)
55
- expect(a).to eq([1])
56
-
57
- a = described_class.new
58
- a.store_path(1, 0, 2)
59
- expect(a).to eq([nil, [2]])
60
- end
61
-
62
- it "on an existing array" do
63
- array.store_path(1, 0, 2)
64
- expect(array[1]).to eq([2])
65
- array.store_path(2, 0, 3)
66
- expect(array[2]).to eq([3])
67
- array.store_path(-4, 0, 3)
68
- expect(array[2]).to eq([3])
69
- end
70
-
71
- it "on an existing item that is not a array" do
72
- array.store_path(0, 2)
73
- expect(array[0]).to eq(2)
74
- array.store_path(0, 0, 3)
75
- expect(array[0]).to eq([3])
76
- end
77
-
78
- it "with an array of args" do
79
- a = described_class.new
80
- a.store_path([3, 0, 1, 2], 3)
81
- expect(a).to eq([nil, nil, nil, [[nil, [nil, nil, 3]]]])
82
- end
83
-
84
- it "with a nil value" do
85
- a = described_class.new
86
- a.store_path(0, 1, nil)
87
- expect(a).to eq([[nil, nil]])
88
- end
89
-
90
- it "with an Array value" do
91
- a = described_class.new
92
- a.store_path(0, 1, [2, 3])
93
- expect(a).to eq([[nil, [2, 3]]])
94
- end
95
-
96
- it "with a Hash value" do
97
- a = described_class.new
98
- a.store_path(0, 1, {2 => 3})
99
- expect(a).to eq([[nil, {2 => 3}]])
100
- end
101
-
102
- it "with invalid values" do
103
- expect { described_class.new.store_path }.to raise_error(ArgumentError)
104
- expect { described_class.new.store_path(1) }.to raise_error(ArgumentError)
105
- end
106
-
107
- include_examples "core_ext/array/nested will not modify arguments", :store_path
108
- end
109
-
110
- context '#has_key_path?' do
111
- it "with various values" do
112
- expect(array.has_key_path?(0)).to be_truthy
113
- expect(array.has_key_path?(1)).to be_truthy
114
- expect(array.has_key_path?(1, 0)).to be_falsey
115
- expect(array.has_key_path?(1, 0, 1)).to be_falsey
116
- expect(array.has_key_path?(2)).to be_truthy
117
- expect(array.has_key_path?(2, 0)).to be_truthy
118
- expect(array.has_key_path?(2, 0, 1)).to be_falsey
119
- expect(array.has_key_path?(3, 0, 1, 2)).to be_falsey
120
- expect(array.has_key_path?(3, 0, 1, 999)).to be_falsey
121
- expect(array.has_key_path?(3, 0, 1, 2, 3)).to be_falsey
122
- expect(array.has_key_path?(4)).to be_truthy
123
- expect(array.has_key_path?(4, 0)).to be_truthy
124
- expect(array.has_key_path?(4, 0, 1)).to be_falsey
125
- expect(array.has_key_path?(5)).to be_truthy
126
- expect(array.has_key_path?(5, 0)).to be_falsey
127
- expect(array.has_key_path?(5, 0, 1)).to be_falsey
128
- end
129
-
130
- it "with a nil value" do
131
- expect { array.fetch_path(nil) }.to raise_error(ArgumentError)
132
- expect { array.fetch_path(3, nil, 0) }.to raise_error(ArgumentError)
133
- end
134
-
135
- it "with invalid values" do
136
- expect { array.has_key_path? }.to raise_error(ArgumentError)
137
- end
138
-
139
- include_examples "core_ext/array/nested will not modify arguments", :has_key_path?
140
- end
141
-
142
- context "#delete_path" do
143
- it "on a nested array" do
144
- array.delete_path(3, 0, 0, 0)
145
- expect(array[3]).to eq([[[]]])
146
- end
147
-
148
- it "with an invalid path" do
149
- array.delete_path(3, 0, 5)
150
- expect(array[3]).to eq([[[3]]])
151
- end
152
-
153
- include_examples "core_ext/array/nested will not modify arguments", :delete_path
154
- end
155
-
156
- it "#delete_blank_paths" do
157
- expect(array.delete_blank_paths).to eq([1, [2], [[[3]]]])
158
- end
159
-
160
- context "#find_path" do
161
- it "with a real value" do
162
- expect(array.find_path(3)).to eq([3, 0, 0, 0])
163
- end
164
-
165
- it "with non-existent value" do
166
- expect(array.find_path(42)).to eq([])
167
- end
168
- end
169
- end
170
-
171
- describe Array do
172
- let(:array) do
173
- [ 1,
174
- [],
175
- [2],
176
- [[[3]]],
177
- Array.new(4),
178
- described_class.new
179
- ]
180
- end
181
-
182
- include_examples "core_ext/array/nested"
183
- end
@@ -1,21 +0,0 @@
1
- describe Array do
2
- around do |example|
3
- old_seed = srand(12072)
4
- example.call
5
- srand(old_seed)
6
- end
7
-
8
- it '#random_index' do
9
- expect(20.times.collect { [].random_index }.uniq.sort).to eq([nil])
10
- expect(20.times.collect { %w{a}.random_index }.uniq.sort).to eq([0])
11
- expect(20.times.collect { %w{a b}.random_index }.uniq.sort).to eq([0, 1])
12
- expect(20.times.collect { %w{a b c d}.random_index }.uniq.sort).to eq([0, 1, 2, 3])
13
- end
14
-
15
- it '#random_element' do
16
- expect(20.times.collect { [].random_element }.uniq.sort).to eq([nil])
17
- expect(20.times.collect { %w{a}.random_element }.uniq.sort).to eq(%w{a})
18
- expect(20.times.collect { %w{a b}.random_element }.uniq.sort).to eq(%w{a b})
19
- expect(20.times.collect { %w{a b c d}.random_element }.uniq.sort).to eq(%w{a b c d})
20
- end
21
- end
@@ -1,94 +0,0 @@
1
- describe Array do
2
- STRETCH_CASES = [
3
- # 2 parameter cases
4
- # Message Test case Expected
5
- "receiver same size as parameter", [[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]],
6
-
7
- "receiver longer than parameter", [[1, 2, 3], [4, 5]], [[1, 2, 3], [4, 5, nil]],
8
- "receiver shorter than parameter", [[1, 2], [4, 5, 6]], [[1, 2, nil], [4, 5, 6]],
9
-
10
- "receiver with empty parameter", [[1, 2, 3], []], [[1, 2, 3], [nil, nil, nil]],
11
- "receiver is empty with 1 parameter", [[], [4, 5, 6]], [[nil, nil, nil], [4, 5, 6]],
12
-
13
- # 3 parameter cases
14
- # Message Test Case Expected
15
- "receiver longer than some parameters", [[1, 2, 3], [4, 5], [7, 8, 9]], [[1, 2, 3], [4, 5, nil], [7, 8, 9]],
16
- "receiver shorter than parameters", [[1, 2], [4, 5, 6], [7, 8, 9]], [[1, 2, nil], [4, 5, 6], [7, 8, 9]],
17
-
18
- "receiver longer than all parameters", [[1, 2, 3], [4, 5], [7, 8]], [[1, 2, 3], [4, 5, nil], [7, 8, nil]],
19
- "receiver shorter than some parameters", [[1, 2], [4, 5, 6], [7, 8]], [[1, 2, nil], [4, 5, 6], [7, 8, nil]],
20
-
21
- "receiver is empty with 2 parameters", [[], [], [7, 8, 9]], [[nil, nil, nil], [nil, nil, nil], [7, 8, 9]],
22
- ]
23
-
24
- context '.stretch' do
25
- STRETCH_CASES.each_slice(3) do |msg, test_case, expected|
26
- it "where #{msg}" do
27
- result = Array.stretch(*test_case)
28
- result.each_with_index do |r, i|
29
- expect(r).not_to equal(test_case[i])
30
- expect(r).to eq(expected[i])
31
- end
32
- end
33
- end
34
- end
35
-
36
- context '.stretch!' do
37
- STRETCH_CASES.each_slice(3) do |msg, test_case, expected|
38
- it "where #{msg}" do
39
- result = Array.stretch!(*test_case)
40
- result.each_with_index do |r, i|
41
- expect(r).to equal(test_case[i])
42
- expect(r).to eq(expected[i])
43
- end
44
- end
45
- end
46
- end
47
-
48
- context '#stretch' do
49
- STRETCH_CASES.each_slice(3) do |msg, test_case, expected|
50
- it "where #{msg}" do
51
- receiver, params = test_case[0], test_case[1..-1]
52
- result = receiver.stretch(*params)
53
- expect(result).not_to equal(receiver)
54
- expect(result).to eq(expected[0])
55
- end
56
- end
57
- end
58
-
59
- context '#stretch!' do
60
- STRETCH_CASES.each_slice(3) do |msg, test_case, expected|
61
- it "where #{msg}" do
62
- receiver, params = test_case[0].dup, test_case[1..-1]
63
- result = receiver.stretch!(*params)
64
- expect(result).to equal(receiver)
65
- expect(result).to eq(expected[0])
66
- end
67
- end
68
- end
69
-
70
- ZIP_STRETCHED_CASES = [
71
- # 1 parameter tests
72
- # Message Test case Expected
73
- "receiver same size as parameter", [1, 2, 3], [[4, 5, 6]], [[1, 4], [2, 5], [3, 6]],
74
- "receiver longer than parameter", [1, 2, 3], [[4, 5]], [[1, 4], [2, 5], [3, nil]],
75
- "receiver shorter than parameter", [1, 2], [[4, 5, 6]], [[1, 4], [2, 5], [nil, 6]], # Different than zip
76
-
77
- # 2 parameter tests
78
- # Message Test case Expected
79
- "receiver same size as all parameters", [1, 2, 3], [[4, 5, 6], [7, 8, 9]], [[1, 4, 7], [2, 5, 8], [3, 6, 9]],
80
- "receiver longer than first parameter", [1, 2, 3], [[4, 5], [7, 8, 9]], [[1, 4, 7], [2, 5, 8], [3, nil, 9]],
81
- "receiver shorter than all parameters", [1, 2], [[4, 5, 6], [7, 8, 9]], [[1, 4, 7], [2, 5, 8], [nil, 6, 9]], # Different than zip
82
- "receiver shorter than first parameter", [1, 2], [[4, 5, 6], [7, 8]], [[1, 4, 7], [2, 5, 8], [nil, 6, nil]], # Different than zip
83
- "receiver shorter than last parameter", [1, 2], [[4, 5], [7, 8, 9]], [[1, 4, 7], [2, 5, 8], [nil, nil, 9]], # Different than zip
84
- "receiver is empty with 2 parameters", [], [[4, 5, 6], [7, 8, 9]], [[nil, 4, 7], [nil, 5, 8], [nil, 6, 9]], # Different than zip
85
- ]
86
-
87
- context '#zip_stretched' do
88
- ZIP_STRETCHED_CASES.each_slice(4) do |msg, receiver, params, expected|
89
- it "where #{msg}" do
90
- expect(receiver.zip_stretched(*params)).to eq(expected)
91
- end
92
- end
93
- end
94
- end
@@ -1,156 +0,0 @@
1
- describe Array do
2
- context '#tableize' do
3
- context "on an Array of Arrays" do
4
- it 'normal case' do
5
- test = [["Col1", "Col2"], ["Val1", "Val2"], ["Value3", "Value4"]]
6
- expected = <<-EOF
7
- Col1 | Col2
8
- --------+--------
9
- Val1 | Val2
10
- Value3 | Value4
11
- EOF
12
- expect(test.tableize).to eq(expected)
13
- end
14
-
15
- it 'with numeric column values right justified' do
16
- test = [["Col1", "Col2"], ["Val1", 200], ["Value3", 30]]
17
- expected = <<-EOF
18
- Col1 | Col2
19
- --------+------
20
- Val1 | 200
21
- Value3 | 30
22
- EOF
23
- expect(test.tableize).to eq(expected)
24
- end
25
-
26
- it 'with really long column value' do
27
- test = [["Col1", "Col2"], ["Val1", "Val2"], ["Really Really Long Value3", "Value4"]]
28
- expected = <<-EOF
29
- Col1 | Col2
30
- ---------------------------+--------
31
- Val1 | Val2
32
- Really Really Long Value3 | Value4
33
- EOF
34
- expect(test.tableize).to eq(expected)
35
- end
36
-
37
- it 'with really long column value and :max_width option' do
38
- test = [["Col1", "Col2"], ["Val1", "Val2"], ["Really Really Long Value3", "Value4"]]
39
- expected = <<-EOF
40
- Col1 | Col2
41
- ------------+--------
42
- Val1 | Val2
43
- Really Rea | Value4
44
- EOF
45
- expect(test.tableize(:max_width => 10)).to eq(expected)
46
- end
47
-
48
- it 'with :header => false option' do
49
- test = [["Col1", "Col2"], ["Val1", "Val2"], ["Value3", "Value4"]]
50
- expected = <<-EOF
51
- Col1 | Col2
52
- Val1 | Val2
53
- Value3 | Value4
54
- EOF
55
- expect(test.tableize(:header => false)).to eq(expected)
56
- end
57
- end
58
-
59
- context "on an Array of Hashes" do
60
- before do
61
- @str_case = [{"Col3" => "Val3", "Col2" => "Val2", "Col1" => "Val1"}, {"Col3" => "Value6", "Col2" => "Value5", "Col1" => "Value4"}]
62
- @sym_case = [{:Col3 => "Val3", :Col2 => "Val2", :Col1 => "Val1"}, {:Col3 => "Value6", :Col2 => "Value5", :Col1 => "Value4"}]
63
- end
64
-
65
- it "normal case" do
66
- expected = <<-EOF
67
- Col1 | Col2 | Col3
68
- --------+--------+--------
69
- Val1 | Val2 | Val3
70
- Value4 | Value5 | Value6
71
- EOF
72
-
73
- expect(@str_case.tableize).to eq(expected)
74
- expect(@sym_case.tableize).to eq(expected)
75
- end
76
-
77
- context "with :columns option" do
78
- before do
79
- @expected = <<-EOF
80
- Col3 | Col1 | Col2
81
- --------+--------+--------
82
- Val3 | Val1 | Val2
83
- Value6 | Value4 | Value5
84
- EOF
85
- end
86
-
87
- it "normal case" do
88
- expect(@str_case.tableize(:columns => ["Col3", "Col1", "Col2"])).to eq(@expected)
89
- expect(@sym_case.tableize(:columns => [:Col3, :Col1, :Col2 ])).to eq(@expected)
90
- end
91
-
92
- it "with only some values" do
93
- expected = <<-EOF
94
- Col3 | Col1
95
- --------+--------
96
- Val3 | Val1
97
- Value6 | Value4
98
- EOF
99
-
100
- expect(@str_case.tableize(:columns => ["Col3", "Col1"])).to eq(expected)
101
- expect(@sym_case.tableize(:columns => [:Col3, :Col1 ])).to eq(expected)
102
- end
103
-
104
- it "and :leading_columns option" do
105
- expect(@str_case.tableize(:columns => ["Col3", "Col1", "Col2"], :leading_columns => ["Col1"])).to eq(@expected)
106
- expect(@sym_case.tableize(:columns => [:Col3, :Col1, :Col2 ], :leading_columns => [:Col1 ])).to eq(@expected)
107
- end
108
-
109
- it "and :trailing_columns option" do
110
- expect(@str_case.tableize(:columns => ["Col3", "Col1", "Col2"], :trailing_columns => ["Col1"])).to eq(@expected)
111
- expect(@sym_case.tableize(:columns => [:Col3, :Col1, :Col2 ], :trailing_columns => [:Col1 ])).to eq(@expected)
112
- end
113
- end
114
-
115
- it "with :leading_columns option" do
116
- expected = <<-EOF
117
- Col3 | Col2 | Col1
118
- --------+--------+--------
119
- Val3 | Val2 | Val1
120
- Value6 | Value5 | Value4
121
- EOF
122
-
123
- expect(@str_case.tableize(:leading_columns => ["Col3", "Col2"])).to eq(expected)
124
- expect(@sym_case.tableize(:leading_columns => [:Col3, :Col2 ])).to eq(expected)
125
- end
126
-
127
- it "with :trailing_columns option" do
128
- expected = <<-EOF
129
- Col1 | Col3 | Col2
130
- --------+--------+--------
131
- Val1 | Val3 | Val2
132
- Value4 | Value6 | Value5
133
- EOF
134
-
135
- expect(@str_case.tableize(:trailing_columns => ["Col3", "Col2"])).to eq(expected)
136
- expect(@sym_case.tableize(:trailing_columns => [:Col3, :Col2 ])).to eq(expected)
137
- end
138
-
139
- it "with both :leading_columns and :trailing_columns options" do
140
- expected = <<-EOF
141
- Col3 | Col1 | Col2
142
- --------+--------+--------
143
- Val3 | Val1 | Val2
144
- Value6 | Value4 | Value5
145
- EOF
146
-
147
- expect(@str_case.tableize(:leading_columns => ["Col3"], :trailing_columns => ["Col2"])).to eq(expected)
148
- expect(@sym_case.tableize(:leading_columns => [:Col3 ], :trailing_columns => [:Col2 ])).to eq(expected)
149
- end
150
- end
151
-
152
- it 'with an invalid receiver' do
153
- expect { [1, 2, 3].tableize }.to raise_error(RuntimeError)
154
- end
155
- end
156
- end
@@ -1,15 +0,0 @@
1
- describe Hash do
2
- it "#delete_nils" do
3
- expect({}.delete_nils).to eq({})
4
- expect({:a => 1}.delete_nils).to eq({:a => 1})
5
- expect({:c => nil}.delete_nils).to eq({})
6
- expect({:a => 1, :b => [], :c => nil}.delete_nils).to eq({:a => 1, :b => []})
7
- end
8
-
9
- it "#delete_blanks" do
10
- expect({}.delete_blanks).to eq({})
11
- expect({:a => 1}.delete_blanks).to eq({:a => 1})
12
- expect({:c => nil}.delete_blanks).to eq({})
13
- expect({:a => 1, :b => [], :c => nil}.delete_blanks).to eq({:a => 1})
14
- end
15
- end