darthjee-core_ext 1.7.4 → 3.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 +5 -5
- data/.circleci/config.yml +63 -9
- data/.gitignore +2 -0
- data/.reek.yml +3 -0
- data/.rubocop.yml +18 -1
- data/.rubocop_todo.yml +15 -8
- data/ARRAY_README.md +72 -15
- data/CLASS_README.md +154 -0
- data/DATE_README.md +19 -9
- data/Dockerfile +18 -5
- data/ENUMERABLE_README.md +154 -4
- data/Gemfile +13 -0
- data/HASH_README.md +276 -135
- data/MATH_README.md +14 -10
- data/Makefile +7 -0
- data/OBJECT_README.md +18 -35
- data/README.md +12 -4
- data/Rakefile +1 -0
- data/SYMBOL_README.md +13 -18
- data/config/check_specs.yml +20 -0
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.yml +45 -3
- data/core_ext.gemspec +10 -18
- data/docker-compose.yml +24 -7
- data/lib/darthjee/core_ext/array/hash_builder.rb +22 -5
- data/lib/darthjee/core_ext/array.rb +22 -14
- data/lib/darthjee/core_ext/class.rb +29 -10
- data/lib/darthjee/core_ext/date.rb +1 -0
- data/lib/darthjee/core_ext/enumerable.rb +67 -29
- data/lib/darthjee/core_ext/hash/cameliazable.rb +52 -21
- data/lib/darthjee/core_ext/hash/chain_fetcher.rb +18 -1
- data/lib/darthjee/core_ext/hash/changeable.rb +88 -0
- data/lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb +112 -0
- data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +127 -62
- data/lib/darthjee/core_ext/hash/key_changeable.rb +138 -66
- data/lib/darthjee/core_ext/hash/key_changer.rb +146 -45
- data/lib/darthjee/core_ext/hash/keys_sorter.rb +43 -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/hash.rb +6 -4
- data/lib/darthjee/core_ext/math.rb +37 -6
- data/lib/darthjee/core_ext/numeric.rb +13 -2
- data/lib/darthjee/core_ext/object.rb +27 -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 +400 -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/array_spec.rb +1 -1
- data/spec/integration/yard/darthjee/core_ext/class/default_value_spec.rb +10 -8
- 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 +67 -37
- data/spec/integration/yard/darthjee/core_ext/hash/transposeable_spec.rb +35 -0
- data/spec/integration/yard/darthjee/core_ext/hash/value_changer_spec.rb +1 -1
- 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/array_spec.rb +2 -1
- 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 +5 -13
- data/spec/lib/hash_spec.rb +87 -147
- data/spec/lib/object_spec.rb +32 -0
- data/spec/lib/symbol_spec.rb +2 -2
- data/spec/spec_helper.rb +4 -2
- data/spec/support/models/client.rb +23 -0
- data/spec/support/models/dummy_iterator.rb +4 -4
- data/spec/support/models/hash/value_changer/dummy.rb +1 -0
- data/spec/support/shared_examples/array/array_random.rb +2 -3
- data/spec/support/shared_examples/hash/chain_hash_keys_changer.rb +1 -1
- data/spec/support/shared_examples/hash/deep_hash.rb +166 -0
- data/spec/support/shared_examples/hash/hash_keys_changer.rb +3 -3
- data/spec/support/shared_examples/hash/hash_squasher.rb +54 -9
- data/spec/support/shared_examples/hash/keys_sorter.rb +266 -6
- data/spec/support/shared_examples/hash/map_to_hash.rb +1 -1
- data/spec/support/shared_examples/hash/remap.rb +4 -4
- data/spec/support/shared_examples/hash/value_changer.rb +2 -2
- metadata +41 -189
- 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/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)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
ignore:
|
2
|
+
- lib/darthjee/core_ext/array/hash_builder.rb
|
3
|
+
- lib/darthjee/core_ext/array.rb
|
4
|
+
- lib/darthjee/core_ext/class.rb
|
5
|
+
- lib/darthjee/core_ext/date.rb
|
6
|
+
- lib/darthjee/core_ext/enumerable.rb
|
7
|
+
- lib/darthjee/core_ext/hash/cameliazable.rb
|
8
|
+
- lib/darthjee/core_ext/hash/changeable.rb
|
9
|
+
- lib/darthjee/core_ext/hash/key_changeable.rb
|
10
|
+
- lib/darthjee/core_ext/hash/transformable.rb
|
11
|
+
- lib/darthjee/core_ext/hash/transposeable.rb
|
12
|
+
- lib/darthjee/core_ext/hash.rb
|
13
|
+
- lib/darthjee/core_ext/math.rb
|
14
|
+
- lib/darthjee/core_ext/numeric.rb
|
15
|
+
- lib/darthjee/core_ext/object.rb
|
16
|
+
- lib/darthjee/core_ext/symbol.rb
|
17
|
+
- lib/darthjee/core_ext/time.rb
|
18
|
+
- lib/darthjee/core_ext/version.rb
|
19
|
+
- lib/darthjee/core_ext.rb
|
20
|
+
- lib/darthjee.rb
|
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,28 +5,20 @@ $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 = '>= 3.3.1'
|
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) }
|
18
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ['lib']
|
20
20
|
|
21
|
-
gem.
|
21
|
+
gem.add_dependency 'activesupport', '~> 7.x'
|
22
22
|
|
23
|
-
gem.
|
24
|
-
gem.add_development_dependency 'pry-nav', '~> 0.2.4'
|
25
|
-
gem.add_development_dependency 'rake', '>= 12.3.1'
|
26
|
-
gem.add_development_dependency 'rspec', '>= 3.8'
|
27
|
-
gem.add_development_dependency 'rubocop', '0.58.1'
|
28
|
-
gem.add_development_dependency 'rubocop-rspec', '1.30.0'
|
29
|
-
gem.add_development_dependency 'simplecov', '~> 0.16.x'
|
30
|
-
gem.add_development_dependency 'yard', '>= 0.9.18'
|
31
|
-
gem.add_development_dependency 'yardstick', '>= 0.9.9'
|
23
|
+
gem.metadata['rubygems_mfa_required'] = 'true'
|
32
24
|
end
|
data/docker-compose.yml
CHANGED
@@ -1,15 +1,32 @@
|
|
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'
|
24
|
+
|
25
|
+
core_ext_circleci:
|
26
|
+
image: darthjee/circleci_ruby_331:1.0.2
|
27
|
+
container_name: core_ext_circleci
|
28
|
+
command: /bin/bash
|
29
|
+
volumes:
|
30
|
+
- .:/home/circleci/project
|
31
|
+
environment:
|
32
|
+
PROJECT: core_ext
|
@@ -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,21 +37,37 @@ 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
|
|
44
|
-
|
48
|
+
[keys, values].transpose.to_h
|
45
49
|
end
|
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?
|
62
|
+
|
51
63
|
values.concat ::Array.new(keys.size - values.size)
|
52
64
|
end
|
53
65
|
|
66
|
+
# @private
|
67
|
+
#
|
68
|
+
# Checks if values array needs to be resized
|
69
|
+
#
|
70
|
+
# @return [::TrueClass,::FalseClass]
|
54
71
|
def needs_resizing?
|
55
72
|
keys.size > values.size
|
56
73
|
end
|
@@ -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'
|
@@ -33,11 +35,11 @@ module Darthjee
|
|
33
35
|
# [].average # returns 0
|
34
36
|
def average
|
35
37
|
return 0 if empty?
|
38
|
+
|
36
39
|
sum * 1.0 / length
|
37
40
|
end
|
38
41
|
|
39
|
-
# Maps the array using the given methods on each
|
40
|
-
# element of the array
|
42
|
+
# Maps the array using the given methods on each element of the array
|
41
43
|
#
|
42
44
|
# @param [::String,::Symbol] methods List of methods to be called
|
43
45
|
# sequentially on each element of the array
|
@@ -59,13 +61,14 @@ module Darthjee
|
|
59
61
|
# output = words.chain_map(:size) do |size|
|
60
62
|
# (size % 2).zero? ? 'even size' : 'odd size'
|
61
63
|
# end # returns ["even size", "even size", "odd size"]
|
62
|
-
def chain_map(*methods, &
|
64
|
+
def chain_map(*methods, &)
|
63
65
|
result = methods.inject(self) do |array, method|
|
64
66
|
array.map(&method)
|
65
67
|
end
|
66
68
|
|
67
69
|
return result unless block_given?
|
68
|
-
|
70
|
+
|
71
|
+
result.map(&)
|
69
72
|
end
|
70
73
|
|
71
74
|
# Maps array chain fetching the keys of the hashes inside
|
@@ -99,11 +102,15 @@ module Darthjee
|
|
99
102
|
end
|
100
103
|
|
101
104
|
# Joins elements in a string using a proc
|
102
|
-
#
|
105
|
+
#
|
106
|
+
# Uses the proc given elements to Strig and a
|
107
|
+
# block for determinating the joining string
|
103
108
|
#
|
104
109
|
# @param [Proc] mapper Proc that will be used to map values
|
105
110
|
# to string before joining
|
106
111
|
#
|
112
|
+
# @return [String]
|
113
|
+
#
|
107
114
|
# @yield [previous, nexte]
|
108
115
|
# defines the string to be used to join the previous and
|
109
116
|
# next element
|
@@ -122,21 +129,20 @@ module Darthjee
|
|
122
129
|
# end # returns '1.0 +2.0 -3.0 -4.0 +5.0'
|
123
130
|
def procedural_join(mapper = proc(&:to_s))
|
124
131
|
return '' if empty?
|
125
|
-
list = dup
|
126
|
-
previous = first
|
127
|
-
list[0] = mapper.call(previous).to_s
|
128
132
|
|
129
|
-
|
130
|
-
link = yield(previous, value) if block_given?
|
131
|
-
next_string = mapper.call(value)
|
132
|
-
previous = value
|
133
|
+
map = map_to_hash(&mapper)
|
133
134
|
|
134
|
-
|
135
|
-
|
135
|
+
map.inject do |(previous, string), (nexte, nexte_string)|
|
136
|
+
link = yield(previous, nexte) if block_given?
|
137
|
+
|
138
|
+
[nexte, "#{string}#{link}#{nexte_string}"]
|
139
|
+
end.last.to_s
|
136
140
|
end
|
137
141
|
|
138
142
|
# Reeturns a random element of the array without altering it
|
139
143
|
#
|
144
|
+
# @return [Object] random element of the array
|
145
|
+
#
|
140
146
|
# @example Picking a random element of numeric array
|
141
147
|
# array = [10, 20, 30]
|
142
148
|
# array.random # might return 10, 20 or 30
|
@@ -147,6 +153,8 @@ module Darthjee
|
|
147
153
|
|
148
154
|
# Reeturns a random element of the array removing it from the array
|
149
155
|
#
|
156
|
+
# @return [Object] random element of the array
|
157
|
+
#
|
150
158
|
# @example Slicing a random element of a numeric array
|
151
159
|
# array = [10, 20, 30]
|
152
160
|
# array.random! # might return 10, 20 or 30 ... lets say 20
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
module Darthjee
|
4
4
|
module CoreExt
|
5
|
+
# @api public
|
5
6
|
module Class
|
6
7
|
private
|
7
8
|
|
@@ -22,8 +23,6 @@ module Darthjee
|
|
22
23
|
# MyClass.new.name # returns 'John'
|
23
24
|
#
|
24
25
|
# @example Comparing value across instances
|
25
|
-
# # frozen_string_literal: false
|
26
|
-
#
|
27
26
|
# class MyClass
|
28
27
|
# default_value :name, 'John'
|
29
28
|
# end
|
@@ -33,6 +32,8 @@ module Darthjee
|
|
33
32
|
#
|
34
33
|
# instance.name.equal?('John') # returns false
|
35
34
|
# instance.name.equal?(other.name) # returns true
|
35
|
+
#
|
36
|
+
# @return [::Symbol] The name of the added method
|
36
37
|
def default_value(name, value)
|
37
38
|
define_method(name) { |*_| value }
|
38
39
|
end
|
@@ -78,17 +79,24 @@ module Darthjee
|
|
78
79
|
#
|
79
80
|
# instance.nick_name.equal?('John') # returns false
|
80
81
|
# instance.nick_name.equal?(instance.name) # returns true
|
82
|
+
#
|
83
|
+
# @return [::Array<::Symbol>] Name of defined methods
|
81
84
|
def default_values(*names, value)
|
82
|
-
names.
|
85
|
+
names.map do |name|
|
83
86
|
default_value(name, value)
|
84
87
|
end
|
85
88
|
end
|
86
89
|
|
87
90
|
# @!visibility public
|
88
91
|
#
|
89
|
-
# Creates a method that will act as reader
|
90
|
-
#
|
91
|
-
#
|
92
|
+
# Creates a method that will act as reader with default value
|
93
|
+
#
|
94
|
+
# The method will be a reader, but when no value was
|
95
|
+
# defined for the instance variable, it will
|
96
|
+
# return a default
|
97
|
+
#
|
98
|
+
# @param [::Symbol,::String] name Name of the method to be added
|
99
|
+
# @param [::Object] value default value
|
92
100
|
#
|
93
101
|
# @example Defining a default value
|
94
102
|
# class Person
|
@@ -119,18 +127,27 @@ module Darthjee
|
|
119
127
|
# model.name = 'Bob'
|
120
128
|
# model.name # returns 'Bob'
|
121
129
|
# Person.new.name # returns 'John Doe'
|
130
|
+
#
|
131
|
+
# @return [::Symbol] Defined method name
|
122
132
|
def default_reader(name, value)
|
123
133
|
define_method(name) do
|
124
134
|
return value unless instance_variable_defined?("@#{name}")
|
135
|
+
|
125
136
|
instance_variable_get("@#{name}")
|
126
137
|
end
|
127
138
|
end
|
128
139
|
|
129
140
|
# @!visibility public
|
130
141
|
#
|
131
|
-
# Creates methods that will act as
|
132
|
-
#
|
133
|
-
#
|
142
|
+
# Creates methods that will act as reader with default value
|
143
|
+
#
|
144
|
+
# The methods will be readers, but when no value was
|
145
|
+
# defined for the instance variable, it will
|
146
|
+
# return a default
|
147
|
+
#
|
148
|
+
# @param [::Array<::Symbol,::String>] names Names of the
|
149
|
+
# methods to be added
|
150
|
+
# @param [::Object] value default value
|
134
151
|
#
|
135
152
|
# @example Defining default values
|
136
153
|
# class Person
|
@@ -166,8 +183,10 @@ module Darthjee
|
|
166
183
|
# model.cars # returns 'none'
|
167
184
|
# model.cars.equal?('none') # returns false
|
168
185
|
# model.nick_name.equal?(model.houses) # returns true
|
186
|
+
#
|
187
|
+
# @return [::Array<::Symbol>] Name of defined methods
|
169
188
|
def default_readers(*names, value)
|
170
|
-
names.
|
189
|
+
names.map do |name|
|
171
190
|
default_reader(name, value)
|
172
191
|
end
|
173
192
|
end
|
@@ -1,10 +1,30 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# @api public
|
3
4
|
module Enumerable
|
4
|
-
#
|
5
|
+
# Removes any element that is nil or empty
|
6
|
+
#
|
7
|
+
# @see #clean!
|
5
8
|
#
|
6
9
|
# This method does not change the original
|
7
10
|
# enumerable
|
11
|
+
#
|
12
|
+
# @example cleaning a Hash
|
13
|
+
# hash = {
|
14
|
+
# keep: 'value',
|
15
|
+
# nil_value: nil,
|
16
|
+
# empty_array: [],
|
17
|
+
# empty_string: '',
|
18
|
+
# empty_hash: {}
|
19
|
+
# }
|
20
|
+
#
|
21
|
+
# hash.clean # returns { keep: 'value' } without changing the hash
|
22
|
+
#
|
23
|
+
# @example cleaning an Array
|
24
|
+
# array = ['value', nil, [], '', {}]
|
25
|
+
#
|
26
|
+
# array.clean # returns ['value'] without changing the array
|
27
|
+
# @return [::Enumerable] same class of +self+
|
8
28
|
def clean
|
9
29
|
deep_dup.clean!
|
10
30
|
end
|
@@ -21,23 +41,23 @@ module Enumerable
|
|
21
41
|
# empty_string: '',
|
22
42
|
# empty_hash: {}
|
23
43
|
# }
|
24
|
-
#
|
25
|
-
#
|
44
|
+
#
|
45
|
+
# hash.clean! # changes the hash to { keep: 'value' }
|
26
46
|
#
|
27
47
|
# @example cleaning an Array
|
28
48
|
# array = ['value', nil, [], '', {}]
|
29
|
-
#
|
30
|
-
#
|
49
|
+
#
|
50
|
+
# array.clean! # changes the array to be ['value']
|
51
|
+
#
|
52
|
+
# @return [::Enumerable] same class of +self+
|
31
53
|
def clean!
|
32
|
-
|
33
|
-
delete_if { |_k, v| empty_value?(v) }
|
34
|
-
else
|
35
|
-
delete_if { |v| empty_value?(v) }
|
36
|
-
end
|
54
|
+
delete_if { |*args| empty_value?(args.last) }
|
37
55
|
end
|
38
56
|
|
39
|
-
# Maps the elements into a new value, returning
|
40
|
-
#
|
57
|
+
# Maps the elements into a new value, returning only one
|
58
|
+
#
|
59
|
+
# The result to be returned is
|
60
|
+
# the first mapping that is evaluated to true
|
41
61
|
#
|
42
62
|
# This method is equivalent to #map#find but
|
43
63
|
# only calling the map block up to when a value
|
@@ -57,10 +77,14 @@ module Enumerable
|
|
57
77
|
#
|
58
78
|
# keys = %i[a b c d e]
|
59
79
|
#
|
60
|
-
# keys.map_and_find
|
61
|
-
#
|
80
|
+
# keys.map_and_find do |key| #
|
81
|
+
# service_values.delete(key) #
|
82
|
+
# end # returns 'found me'
|
83
|
+
#
|
62
84
|
# service_map # has lost only 3 keys returning
|
63
85
|
# # { d: nil, e: 'didnt find me' }
|
86
|
+
#
|
87
|
+
# @return [::Object]
|
64
88
|
def map_and_find
|
65
89
|
mapped = nil
|
66
90
|
find do |*args|
|
@@ -69,8 +93,10 @@ module Enumerable
|
|
69
93
|
mapped || nil
|
70
94
|
end
|
71
95
|
|
72
|
-
# Maps the elements into a new value returning
|
73
|
-
#
|
96
|
+
# Maps the elements into a new value returning a subset
|
97
|
+
#
|
98
|
+
# The subset returned has the values mapped to non
|
99
|
+
# false values
|
74
100
|
#
|
75
101
|
# This method is equivalent to call #map#select
|
76
102
|
#
|
@@ -88,15 +114,18 @@ module Enumerable
|
|
88
114
|
# end
|
89
115
|
#
|
90
116
|
# values # returns [3, 1]
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
mapped.select { |e| e }
|
117
|
+
#
|
118
|
+
# @return [::Array<::Object>]
|
119
|
+
def map_and_select(&)
|
120
|
+
map(&).select(&:trueful?)
|
96
121
|
end
|
97
122
|
|
98
|
-
# Maps values and creates a hash
|
99
|
-
#
|
123
|
+
# Maps values and creates a hash
|
124
|
+
#
|
125
|
+
# The keys will be the original values used in the
|
126
|
+
# mapping and the values the result of the #map
|
127
|
+
#
|
128
|
+
# @return [::Hash]
|
100
129
|
#
|
101
130
|
# @yield (*args) the mapping block
|
102
131
|
#
|
@@ -105,17 +134,26 @@ module Enumerable
|
|
105
134
|
#
|
106
135
|
# strings.map_to_hash(&:size) # returns { 'word' => 4, 'big_word' => 8 }
|
107
136
|
def map_to_hash
|
108
|
-
|
109
|
-
|
110
|
-
hash[element] = yield(element)
|
111
|
-
end
|
137
|
+
to_h do |value|
|
138
|
+
[value, yield(value)]
|
112
139
|
end
|
113
140
|
end
|
114
141
|
|
115
142
|
private
|
116
143
|
|
144
|
+
# @api private
|
145
|
+
#
|
146
|
+
# @private
|
147
|
+
#
|
148
|
+
# Checks if a value is considered empty
|
149
|
+
#
|
150
|
+
# This also clean empty values
|
151
|
+
#
|
152
|
+
# @return [::TrueClass,::FalseClass]
|
117
153
|
def empty_value?(value)
|
118
|
-
|
119
|
-
|
154
|
+
return true unless value.present?
|
155
|
+
return false unless value.is_a?(Enumerable)
|
156
|
+
|
157
|
+
value.clean!.empty?
|
120
158
|
end
|
121
159
|
end
|