darthjee-core_ext 1.7.4 → 2.0.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.
- checksums.yaml +4 -4
- data/.circleci/config.yml +7 -1
- data/.gitignore +2 -0
- data/.reek.yml +3 -0
- data/ARRAY_README.md +72 -15
- data/CLASS_README.md +154 -0
- data/DATE_README.md +19 -9
- data/Dockerfile +2 -5
- data/ENUMERABLE_README.md +154 -4
- data/HASH_README.md +276 -135
- data/MATH_README.md +14 -10
- data/OBJECT_README.md +18 -35
- data/README.md +8 -4
- data/Rakefile +1 -0
- data/SYMBOL_README.md +13 -18
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.yml +45 -3
- data/core_ext.gemspec +10 -8
- data/docker-compose.yml +15 -7
- data/lib/darthjee/core_ext/array.rb +17 -12
- data/lib/darthjee/core_ext/array/hash_builder.rb +20 -4
- data/lib/darthjee/core_ext/class.rb +28 -10
- data/lib/darthjee/core_ext/date.rb +1 -0
- data/lib/darthjee/core_ext/enumerable.rb +67 -30
- data/lib/darthjee/core_ext/hash.rb +4 -2
- data/lib/darthjee/core_ext/hash/cameliazable.rb +39 -8
- data/lib/darthjee/core_ext/hash/chain_fetcher.rb +17 -1
- data/lib/darthjee/core_ext/hash/changeable.rb +88 -0
- data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +127 -63
- data/lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb +112 -0
- data/lib/darthjee/core_ext/hash/key_changeable.rb +126 -54
- data/lib/darthjee/core_ext/hash/key_changer.rb +140 -40
- data/lib/darthjee/core_ext/hash/keys_sorter.rb +42 -6
- data/lib/darthjee/core_ext/hash/squasher.rb +131 -18
- data/lib/darthjee/core_ext/hash/transformable.rb +133 -41
- data/lib/darthjee/core_ext/hash/transposeable.rb +37 -8
- data/lib/darthjee/core_ext/hash/value_changer.rb +76 -36
- data/lib/darthjee/core_ext/math.rb +37 -6
- data/lib/darthjee/core_ext/numeric.rb +10 -0
- data/lib/darthjee/core_ext/object.rb +29 -0
- data/lib/darthjee/core_ext/symbol.rb +22 -2
- data/lib/darthjee/core_ext/version.rb +1 -1
- data/scripts/check_readme.sh +6 -0
- data/scripts/rubycritic.sh +10 -0
- data/spec/integration/readme/array_spec.rb +96 -0
- data/spec/integration/readme/class_spec.rb +123 -0
- data/spec/integration/readme/date_spec.rb +20 -0
- data/spec/integration/readme/enumerable_spec.rb +87 -0
- data/spec/integration/readme/hash_spec.rb +391 -0
- data/spec/integration/readme/math_spec.rb +31 -0
- data/spec/integration/readme/object_spec.rb +49 -0
- data/spec/integration/readme/symbol_spec.rb +29 -0
- data/spec/integration/yard/darthjee/core_ext/enumerable_spec.rb +24 -4
- data/spec/integration/yard/darthjee/core_ext/hash/cameliazable_spec.rb +11 -0
- data/spec/integration/yard/darthjee/core_ext/hash/changeable_spec.rb +68 -0
- data/spec/integration/yard/darthjee/core_ext/hash/deep_hash_constructor/setter_spec.rb +34 -0
- data/spec/integration/yard/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +65 -0
- data/spec/integration/yard/darthjee/core_ext/hash/key_changeable_spec.rb +8 -0
- data/spec/integration/yard/darthjee/core_ext/hash/key_changer_spec.rb +59 -0
- data/spec/integration/yard/darthjee/core_ext/hash/keys_sorter_spec.rb +25 -0
- data/spec/integration/yard/darthjee/core_ext/hash/squasher_spec.rb +60 -0
- data/spec/integration/yard/darthjee/core_ext/hash/transformable_spec.rb +66 -36
- data/spec/integration/yard/darthjee/core_ext/hash/transposeable_spec.rb +35 -0
- data/spec/integration/yard/darthjee/core_ext/hash_spec.rb +13 -2
- data/spec/integration/yard/darthjee/core_ext/math_spec.rb +28 -0
- data/spec/integration/yard/darthjee/core_ext/numeric_spec.rb +11 -0
- data/spec/integration/yard/darthjee/core_ext/object_spec.rb +47 -0
- data/spec/integration/yard/darthjee/core_ext/symbol_spec.rb +17 -0
- data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor/setter_spec.rb +64 -0
- data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +16 -156
- data/spec/lib/darthjee/core_ext/hash/keys_sorter_spec.rb +5 -4
- data/spec/lib/darthjee/core_ext/hash/squasher_spec.rb +8 -2
- data/spec/lib/darthjee/core_ext/hash/value_changer_spec.rb +4 -12
- data/spec/lib/hash_spec.rb +92 -146
- data/spec/lib/object_spec.rb +32 -0
- data/spec/support/models/client.rb +22 -0
- data/spec/support/shared_examples/hash/deep_hash.rb +166 -0
- data/spec/support/shared_examples/hash/hash_squasher.rb +54 -9
- data/spec/support/shared_examples/hash/keys_sorter.rb +266 -6
- metadata +70 -8
- data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +0 -25
- data/spec/lib/darthjee/core_ext/hash/to_hash_mapper_spec.rb +0 -11
data/MATH_README.md
CHANGED
@@ -4,12 +4,9 @@
|
|
4
4
|
calculates the average of the given values
|
5
5
|
|
6
6
|
```ruby
|
7
|
-
Math.average([
|
8
|
-
```
|
7
|
+
Math.average([0, 1, 2, 3, 4, 5])
|
9
8
|
|
10
|
-
returns
|
11
|
-
```
|
12
|
-
2
|
9
|
+
# returns 2.5
|
13
10
|
```
|
14
11
|
|
15
12
|
It can also be used to calculate the average of weighted values
|
@@ -20,11 +17,18 @@ Math.average(
|
|
20
17
|
1 => 2,
|
21
18
|
2 => 3,
|
22
19
|
3 => 4,
|
23
|
-
4 =>
|
20
|
+
4 => 6
|
24
21
|
)
|
25
|
-
```
|
26
22
|
|
27
|
-
returns
|
28
|
-
```
|
29
|
-
2.6666
|
23
|
+
# returns 2.6666
|
30
24
|
```
|
25
|
+
|
26
|
+
## Added by Object
|
27
|
+
- [#is_any?](OBJECT_README.md#is_any?)
|
28
|
+
- [#trueful?](OBJECT_README.md#trueful?)
|
29
|
+
|
30
|
+
## Added by Class
|
31
|
+
- [.default_value](CLASS_README.md#default_value)
|
32
|
+
- [.default_values](CLASS_README.md#default_values)
|
33
|
+
- [.default_reader](CLASS_README.md#default_reader)
|
34
|
+
- [.default_readers](CLASS_README.md#default_readers)
|
data/OBJECT_README.md
CHANGED
@@ -1,50 +1,33 @@
|
|
1
1
|
## Object
|
2
2
|
|
3
|
-
###
|
4
|
-
|
3
|
+
### #is_any?
|
4
|
+
returns if the object is an instance of any of the given classes
|
5
5
|
|
6
6
|
```ruby
|
7
|
-
|
8
|
-
default_value :the_method, 10
|
9
|
-
end.new.the_method
|
10
|
-
```
|
11
|
-
|
12
|
-
will return
|
7
|
+
1.is_any?(String, Symbol)
|
13
8
|
|
14
|
-
|
15
|
-
10
|
9
|
+
# returns false
|
16
10
|
```
|
17
11
|
|
18
|
-
### .default_values
|
19
|
-
Add several methods with single return value
|
20
|
-
|
21
12
|
```ruby
|
22
|
-
|
23
|
-
default_values :is_child?, :is_final?, true
|
24
|
-
end.new.is_child?
|
25
|
-
```
|
26
|
-
|
27
|
-
will return
|
13
|
+
1.is_any?(String, Symbol, Numeric)
|
28
14
|
|
29
|
-
|
30
|
-
true
|
15
|
+
# returns true
|
31
16
|
```
|
32
17
|
|
33
|
-
|
34
|
-
returns if the object is an instance of any of the given classes
|
18
|
+
## #trueful?
|
35
19
|
|
36
20
|
```ruby
|
37
|
-
|
38
|
-
|
39
|
-
returns
|
40
|
-
|
41
|
-
|
21
|
+
nil.trueful? # returns false
|
22
|
+
[].trueful? # returns true
|
23
|
+
{}.trueful? # returns true
|
24
|
+
"".trueful? # returns true
|
25
|
+
0.trueful? # returns true
|
26
|
+
Object.new.trueful? # returns true
|
42
27
|
```
|
43
28
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
false
|
50
|
-
```
|
29
|
+
## Added by Class
|
30
|
+
- [.default_value](CLASS_README.md#default_value)
|
31
|
+
- [.default_values](CLASS_README.md#default_values)
|
32
|
+
- [.default_reader](CLASS_README.md#default_reader)
|
33
|
+
- [.default_readers](CLASS_README.md#default_readers)
|
data/README.md
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
Darthjee/CoreExt
|
2
2
|
========
|
3
3
|
|
4
|
-
](https://codeclimate.com/github/darthjee/core_ext)
|
5
|
+
[](https://codeclimate.com/github/darthjee/core_ext/coverage)
|
6
|
+
[](https://codeclimate.com/github/darthjee/core_ext)
|
7
|
+
[](https://badge.fury.io/rb/darthjee-core_ext)
|
5
8
|
|
6
|
-
|
7
|
-
[](https://codeclimate.com/github/darthjee/core_ext/test_coverage)
|
9
|
+

|
8
10
|
|
9
11
|
Yard Documentation
|
10
12
|
-------------------
|
11
|
-
https://www.rubydoc.info/gems/darthjee-core_ext/
|
13
|
+
https://www.rubydoc.info/gems/darthjee-core_ext/2.0.0
|
12
14
|
|
13
15
|
# Usage
|
14
16
|
This project adds some new methods to the core ruby classes
|
@@ -45,3 +47,5 @@ bundle install darthjee-core_ext
|
|
45
47
|
[OBJECT_README.md](OBJECT_README.md)
|
46
48
|
## Math
|
47
49
|
[MATH_README.md](MATH_README.md)
|
50
|
+
## Class
|
51
|
+
[CLASS_README.md](CLASS_README.md)
|
data/Rakefile
CHANGED
data/SYMBOL_README.md
CHANGED
@@ -5,31 +5,20 @@ Camelize the symbol 'just like String#camelize'
|
|
5
5
|
|
6
6
|
```ruby
|
7
7
|
:underscore_sym.camelize(:upper)
|
8
|
-
```
|
9
|
-
|
10
|
-
Returns
|
11
8
|
|
12
|
-
|
13
|
-
:UnderscoreSym
|
9
|
+
# returns :UnderscoreSym
|
14
10
|
```
|
15
11
|
|
16
12
|
```ruby
|
17
13
|
:underscore_sym.camelize(:lower)
|
18
|
-
```
|
19
|
-
Returns
|
20
14
|
|
21
|
-
|
22
|
-
:underscoreSym
|
15
|
+
# returns :underscoreSym
|
23
16
|
```
|
24
17
|
|
25
18
|
```ruby
|
26
19
|
:underscore_sym.camelize
|
27
|
-
```
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
```ruby
|
32
|
-
:UnderscoreSym
|
21
|
+
# returns :UnderscoreSym
|
33
22
|
```
|
34
23
|
|
35
24
|
### #underscore
|
@@ -37,10 +26,16 @@ underscore the symbol 'just like String#underscore'
|
|
37
26
|
|
38
27
|
```ruby
|
39
28
|
:symBol.underscore
|
29
|
+
|
30
|
+
# returns :sym_bol
|
40
31
|
```
|
41
32
|
|
42
|
-
|
33
|
+
## Added by Object
|
34
|
+
- [#is_any?](OBJECT_README.md#is_any?)
|
35
|
+
- [#trueful?](OBJECT_README.md#trueful?)
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
37
|
+
## Added by Class
|
38
|
+
- [.default_value](CLASS_README.md#default_value)
|
39
|
+
- [.default_values](CLASS_README.md#default_values)
|
40
|
+
- [.default_reader](CLASS_README.md#default_reader)
|
41
|
+
- [.default_readers](CLASS_README.md#default_readers)
|
data/config/rubycritc.rb
ADDED
data/config/yardstick.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
threshold:
|
1
|
+
threshold: 100
|
2
2
|
require_exact_threshold: false
|
3
3
|
rules:
|
4
4
|
ApiTag::Presence:
|
@@ -19,11 +19,53 @@ rules:
|
|
19
19
|
ReturnTag:
|
20
20
|
enabled: true
|
21
21
|
exclude:
|
22
|
-
- Darthjee::CoreExt::
|
22
|
+
- Darthjee::CoreExt::Array::HashBuilder#values
|
23
|
+
- Darthjee::CoreExt::Array::HashBuilder#keys
|
24
|
+
- Darthjee::CoreExt::Array::HashBuilder#values=
|
25
|
+
- Darthjee::CoreExt::Array::HashBuilder#keys=
|
26
|
+
- Darthjee::CoreExt::Hash::ChainFetcher#hash
|
27
|
+
- Darthjee::CoreExt::Hash::ChainFetcher#keys
|
28
|
+
- Darthjee::CoreExt::Hash::ChainFetcher#block
|
29
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor#separator
|
30
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor::Setter#hash
|
31
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor::Setter#base_key
|
32
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor::Setter#key
|
33
|
+
- Darthjee::CoreExt::Hash::KeyChanger#hash
|
34
|
+
- Darthjee::CoreExt::Hash::KeysSorter#hash
|
35
|
+
- Darthjee::CoreExt::Hash::KeysSorter#recursive
|
36
|
+
- Darthjee::CoreExt::Hash::Squasher#joiner
|
37
|
+
- Darthjee::CoreExt::Hash::ValueChanger#recursive
|
38
|
+
- Darthjee::CoreExt::Hash::ValueChanger#skip_inner
|
39
|
+
- Darthjee::CoreExt::Hash::ValueChanger#block
|
23
40
|
Summary::Presence:
|
24
41
|
enabled: true
|
25
42
|
exclude:
|
26
|
-
- Darthjee::CoreExt::Hash::
|
43
|
+
- Darthjee::CoreExt::Hash::KeyChanger#initialize
|
44
|
+
- Darthjee::CoreExt::Array::HashBuilder#initialize
|
45
|
+
- Darthjee::CoreExt::Hash::ChainFetcher#initialize
|
46
|
+
- Darthjee::CoreExt::Hash::ValueChanger#initialize
|
47
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor#initialize
|
48
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor::Setter#initialize
|
49
|
+
- Darthjee::CoreExt::Hash::KeysSorter#initialize
|
50
|
+
- Darthjee::CoreExt::Hash::Squasher#initialize
|
51
|
+
- Darthjee::CoreExt::Array::HashBuilder#values
|
52
|
+
- Darthjee::CoreExt::Array::HashBuilder#keys
|
53
|
+
- Darthjee::CoreExt::Array::HashBuilder#values=
|
54
|
+
- Darthjee::CoreExt::Array::HashBuilder#keys=
|
55
|
+
- Darthjee::CoreExt::Hash::ChainFetcher#hash
|
56
|
+
- Darthjee::CoreExt::Hash::ChainFetcher#keys
|
57
|
+
- Darthjee::CoreExt::Hash::ChainFetcher#block
|
58
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor#separator
|
59
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor::Setter#hash
|
60
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor::Setter#base_key
|
61
|
+
- Darthjee::CoreExt::Hash::DeepHashConstructor::Setter#key
|
62
|
+
- Darthjee::CoreExt::Hash::KeyChanger#hash
|
63
|
+
- Darthjee::CoreExt::Hash::KeysSorter#hash
|
64
|
+
- Darthjee::CoreExt::Hash::KeysSorter#recursive
|
65
|
+
- Darthjee::CoreExt::Hash::Squasher#joiner
|
66
|
+
- Darthjee::CoreExt::Hash::ValueChanger#recursive
|
67
|
+
- Darthjee::CoreExt::Hash::ValueChanger#skip_inner
|
68
|
+
- Darthjee::CoreExt::Hash::ValueChanger#block
|
27
69
|
Summary::Length:
|
28
70
|
enabled: true
|
29
71
|
exclude: []
|
data/core_ext.gemspec
CHANGED
@@ -5,13 +5,14 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
5
|
require 'darthjee/core_ext/version'
|
6
6
|
|
7
7
|
Gem::Specification.new do |gem|
|
8
|
-
gem.name
|
9
|
-
gem.version
|
10
|
-
gem.authors
|
11
|
-
gem.email
|
12
|
-
gem.summary
|
13
|
-
gem.homepage
|
14
|
-
gem.description
|
8
|
+
gem.name = 'darthjee-core_ext'
|
9
|
+
gem.version = Darthjee::CoreExt::VERSION
|
10
|
+
gem.authors = ['Darthjee']
|
11
|
+
gem.email = ['darthjee@gmail.com']
|
12
|
+
gem.summary = 'Core Extensions'
|
13
|
+
gem.homepage = 'https://github.com/darthjee/core_ext'
|
14
|
+
gem.description = 'Extension of basic classes with usefull methods'
|
15
|
+
gem.required_ruby_version = '>= 2.4.0'
|
15
16
|
|
16
17
|
gem.files = `git ls-files -z`.split("\x0")
|
17
18
|
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
@@ -20,12 +21,13 @@ Gem::Specification.new do |gem|
|
|
20
21
|
|
21
22
|
gem.add_runtime_dependency 'activesupport', '>= 5.2.x'
|
22
23
|
|
23
|
-
gem.add_development_dependency 'bundler', '~> 1.17.
|
24
|
+
gem.add_development_dependency 'bundler', '~> 1.17.3'
|
24
25
|
gem.add_development_dependency 'pry-nav', '~> 0.2.4'
|
25
26
|
gem.add_development_dependency 'rake', '>= 12.3.1'
|
26
27
|
gem.add_development_dependency 'rspec', '>= 3.8'
|
27
28
|
gem.add_development_dependency 'rubocop', '0.58.1'
|
28
29
|
gem.add_development_dependency 'rubocop-rspec', '1.30.0'
|
30
|
+
gem.add_development_dependency 'rubycritic', '>= 4.0.2'
|
29
31
|
gem.add_development_dependency 'simplecov', '~> 0.16.x'
|
30
32
|
gem.add_development_dependency 'yard', '>= 0.9.18'
|
31
33
|
gem.add_development_dependency 'yardstick', '>= 0.9.9'
|
data/docker-compose.yml
CHANGED
@@ -1,15 +1,23 @@
|
|
1
|
-
version: '
|
1
|
+
version: '3'
|
2
2
|
services:
|
3
3
|
base: &base
|
4
|
-
|
5
|
-
|
6
|
-
working_dir: /home/app/core_ext
|
4
|
+
image: core_ext
|
5
|
+
working_dir: /home/app/app
|
7
6
|
volumes:
|
8
|
-
- .:/home/app/
|
7
|
+
- .:/home/app/app
|
9
8
|
|
10
|
-
|
9
|
+
base_build:
|
10
|
+
<<: *base
|
11
|
+
build: .
|
12
|
+
command: echo done
|
11
13
|
|
12
14
|
core_ext:
|
13
15
|
<<: *base
|
14
16
|
container_name: core_ext
|
15
|
-
|
17
|
+
depends_on: [base_build]
|
18
|
+
command: /bin/bash -c 'rspec'
|
19
|
+
|
20
|
+
test_all:
|
21
|
+
<<: *base
|
22
|
+
depends_on: [base_build]
|
23
|
+
command: /bin/bash -c 'rspec && yard && rake yardstick_measure && rake verify_measurements'
|
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
module Darthjee
|
4
4
|
module CoreExt
|
5
|
+
# @api public
|
6
|
+
#
|
5
7
|
# Module containing new usefull methods to Ruby vanilla Array
|
6
8
|
module Array
|
7
9
|
autoload :HashBuilder, 'darthjee/core_ext/array/hash_builder'
|
@@ -36,8 +38,7 @@ module Darthjee
|
|
36
38
|
sum * 1.0 / length
|
37
39
|
end
|
38
40
|
|
39
|
-
# Maps the array using the given methods on each
|
40
|
-
# element of the array
|
41
|
+
# Maps the array using the given methods on each element of the array
|
41
42
|
#
|
42
43
|
# @param [::String,::Symbol] methods List of methods to be called
|
43
44
|
# sequentially on each element of the array
|
@@ -99,11 +100,15 @@ module Darthjee
|
|
99
100
|
end
|
100
101
|
|
101
102
|
# Joins elements in a string using a proc
|
102
|
-
#
|
103
|
+
#
|
104
|
+
# Uses the proc given elements to Strig and a
|
105
|
+
# block for determinating the joining string
|
103
106
|
#
|
104
107
|
# @param [Proc] mapper Proc that will be used to map values
|
105
108
|
# to string before joining
|
106
109
|
#
|
110
|
+
# @return [String]
|
111
|
+
#
|
107
112
|
# @yield [previous, nexte]
|
108
113
|
# defines the string to be used to join the previous and
|
109
114
|
# next element
|
@@ -122,21 +127,19 @@ module Darthjee
|
|
122
127
|
# end # returns '1.0 +2.0 -3.0 -4.0 +5.0'
|
123
128
|
def procedural_join(mapper = proc(&:to_s))
|
124
129
|
return '' if empty?
|
125
|
-
|
126
|
-
previous = first
|
127
|
-
list[0] = mapper.call(previous).to_s
|
130
|
+
map = map_to_hash(&mapper)
|
128
131
|
|
129
|
-
|
130
|
-
link =
|
131
|
-
next_string = mapper.call(value)
|
132
|
-
previous = value
|
132
|
+
map.inject do |(previous, string), (nexte, nexte_string)|
|
133
|
+
link = yield(previous, nexte) if block_given?
|
133
134
|
|
134
|
-
"#{string}#{link}#{
|
135
|
-
end
|
135
|
+
[nexte, "#{string}#{link}#{nexte_string}"]
|
136
|
+
end.last.to_s
|
136
137
|
end
|
137
138
|
|
138
139
|
# Reeturns a random element of the array without altering it
|
139
140
|
#
|
141
|
+
# @return [Object] random element of the array
|
142
|
+
#
|
140
143
|
# @example Picking a random element of numeric array
|
141
144
|
# array = [10, 20, 30]
|
142
145
|
# array.random # might return 10, 20 or 30
|
@@ -147,6 +150,8 @@ module Darthjee
|
|
147
150
|
|
148
151
|
# Reeturns a random element of the array removing it from the array
|
149
152
|
#
|
153
|
+
# @return [Object] random element of the array
|
154
|
+
#
|
150
155
|
# @example Slicing a random element of a numeric array
|
151
156
|
# array = [10, 20, 30]
|
152
157
|
# array.random! # might return 10, 20 or 30 ... lets say 20
|
@@ -3,6 +3,9 @@
|
|
3
3
|
module Darthjee
|
4
4
|
module CoreExt
|
5
5
|
module Array
|
6
|
+
# @api private
|
7
|
+
# @author Darthjee
|
8
|
+
#
|
6
9
|
# Class responsible for building a Hash from 2 arrays
|
7
10
|
#
|
8
11
|
# @attribute [::Array] values
|
@@ -26,8 +29,6 @@ module Darthjee
|
|
26
29
|
#
|
27
30
|
# builder.build == hash # returns true
|
28
31
|
class HashBuilder
|
29
|
-
attr_accessor :values, :keys
|
30
|
-
|
31
32
|
# @param [::Array] values List of values of the hash
|
32
33
|
# @param [::Array] keys List of keys of the hash
|
33
34
|
def initialize(values, keys)
|
@@ -36,8 +37,11 @@ module Darthjee
|
|
36
37
|
end
|
37
38
|
|
38
39
|
# Builds the hash
|
39
|
-
#
|
40
|
-
#
|
40
|
+
#
|
41
|
+
# The building happens through the pairing of
|
42
|
+
# keys and values
|
43
|
+
#
|
44
|
+
# @return [::Hash]
|
41
45
|
def build
|
42
46
|
fixes_sizes
|
43
47
|
|
@@ -46,11 +50,23 @@ module Darthjee
|
|
46
50
|
|
47
51
|
private
|
48
52
|
|
53
|
+
attr_reader :values, :keys
|
54
|
+
|
55
|
+
# @private
|
56
|
+
#
|
57
|
+
# Fixes the size of values array to match keys array size
|
58
|
+
#
|
59
|
+
# @return [::Array]
|
49
60
|
def fixes_sizes
|
50
61
|
return unless needs_resizing?
|
51
62
|
values.concat ::Array.new(keys.size - values.size)
|
52
63
|
end
|
53
64
|
|
65
|
+
# @private
|
66
|
+
#
|
67
|
+
# Checks if values array needs to be resized
|
68
|
+
#
|
69
|
+
# @return [::TrueClass,::FalseClass]
|
54
70
|
def needs_resizing?
|
55
71
|
keys.size > values.size
|
56
72
|
end
|