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.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +7 -1
  3. data/.gitignore +2 -0
  4. data/.reek.yml +3 -0
  5. data/ARRAY_README.md +72 -15
  6. data/CLASS_README.md +154 -0
  7. data/DATE_README.md +19 -9
  8. data/Dockerfile +2 -5
  9. data/ENUMERABLE_README.md +154 -4
  10. data/HASH_README.md +276 -135
  11. data/MATH_README.md +14 -10
  12. data/OBJECT_README.md +18 -35
  13. data/README.md +8 -4
  14. data/Rakefile +1 -0
  15. data/SYMBOL_README.md +13 -18
  16. data/config/rubycritc.rb +12 -0
  17. data/config/yardstick.yml +45 -3
  18. data/core_ext.gemspec +10 -8
  19. data/docker-compose.yml +15 -7
  20. data/lib/darthjee/core_ext/array.rb +17 -12
  21. data/lib/darthjee/core_ext/array/hash_builder.rb +20 -4
  22. data/lib/darthjee/core_ext/class.rb +28 -10
  23. data/lib/darthjee/core_ext/date.rb +1 -0
  24. data/lib/darthjee/core_ext/enumerable.rb +67 -30
  25. data/lib/darthjee/core_ext/hash.rb +4 -2
  26. data/lib/darthjee/core_ext/hash/cameliazable.rb +39 -8
  27. data/lib/darthjee/core_ext/hash/chain_fetcher.rb +17 -1
  28. data/lib/darthjee/core_ext/hash/changeable.rb +88 -0
  29. data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +127 -63
  30. data/lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb +112 -0
  31. data/lib/darthjee/core_ext/hash/key_changeable.rb +126 -54
  32. data/lib/darthjee/core_ext/hash/key_changer.rb +140 -40
  33. data/lib/darthjee/core_ext/hash/keys_sorter.rb +42 -6
  34. data/lib/darthjee/core_ext/hash/squasher.rb +131 -18
  35. data/lib/darthjee/core_ext/hash/transformable.rb +133 -41
  36. data/lib/darthjee/core_ext/hash/transposeable.rb +37 -8
  37. data/lib/darthjee/core_ext/hash/value_changer.rb +76 -36
  38. data/lib/darthjee/core_ext/math.rb +37 -6
  39. data/lib/darthjee/core_ext/numeric.rb +10 -0
  40. data/lib/darthjee/core_ext/object.rb +29 -0
  41. data/lib/darthjee/core_ext/symbol.rb +22 -2
  42. data/lib/darthjee/core_ext/version.rb +1 -1
  43. data/scripts/check_readme.sh +6 -0
  44. data/scripts/rubycritic.sh +10 -0
  45. data/spec/integration/readme/array_spec.rb +96 -0
  46. data/spec/integration/readme/class_spec.rb +123 -0
  47. data/spec/integration/readme/date_spec.rb +20 -0
  48. data/spec/integration/readme/enumerable_spec.rb +87 -0
  49. data/spec/integration/readme/hash_spec.rb +391 -0
  50. data/spec/integration/readme/math_spec.rb +31 -0
  51. data/spec/integration/readme/object_spec.rb +49 -0
  52. data/spec/integration/readme/symbol_spec.rb +29 -0
  53. data/spec/integration/yard/darthjee/core_ext/enumerable_spec.rb +24 -4
  54. data/spec/integration/yard/darthjee/core_ext/hash/cameliazable_spec.rb +11 -0
  55. data/spec/integration/yard/darthjee/core_ext/hash/changeable_spec.rb +68 -0
  56. data/spec/integration/yard/darthjee/core_ext/hash/deep_hash_constructor/setter_spec.rb +34 -0
  57. data/spec/integration/yard/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +65 -0
  58. data/spec/integration/yard/darthjee/core_ext/hash/key_changeable_spec.rb +8 -0
  59. data/spec/integration/yard/darthjee/core_ext/hash/key_changer_spec.rb +59 -0
  60. data/spec/integration/yard/darthjee/core_ext/hash/keys_sorter_spec.rb +25 -0
  61. data/spec/integration/yard/darthjee/core_ext/hash/squasher_spec.rb +60 -0
  62. data/spec/integration/yard/darthjee/core_ext/hash/transformable_spec.rb +66 -36
  63. data/spec/integration/yard/darthjee/core_ext/hash/transposeable_spec.rb +35 -0
  64. data/spec/integration/yard/darthjee/core_ext/hash_spec.rb +13 -2
  65. data/spec/integration/yard/darthjee/core_ext/math_spec.rb +28 -0
  66. data/spec/integration/yard/darthjee/core_ext/numeric_spec.rb +11 -0
  67. data/spec/integration/yard/darthjee/core_ext/object_spec.rb +47 -0
  68. data/spec/integration/yard/darthjee/core_ext/symbol_spec.rb +17 -0
  69. data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor/setter_spec.rb +64 -0
  70. data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +16 -156
  71. data/spec/lib/darthjee/core_ext/hash/keys_sorter_spec.rb +5 -4
  72. data/spec/lib/darthjee/core_ext/hash/squasher_spec.rb +8 -2
  73. data/spec/lib/darthjee/core_ext/hash/value_changer_spec.rb +4 -12
  74. data/spec/lib/hash_spec.rb +92 -146
  75. data/spec/lib/object_spec.rb +32 -0
  76. data/spec/support/models/client.rb +22 -0
  77. data/spec/support/shared_examples/hash/deep_hash.rb +166 -0
  78. data/spec/support/shared_examples/hash/hash_squasher.rb +54 -9
  79. data/spec/support/shared_examples/hash/keys_sorter.rb +266 -6
  80. metadata +70 -8
  81. data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +0 -25
  82. data/spec/lib/darthjee/core_ext/hash/to_hash_mapper_spec.rb +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 103b4b6d1acc61970210009223f1dafb42ffe566
4
- data.tar.gz: 6887e4ef498e5e8edaa14c604ac8b79b91f9e709
3
+ metadata.gz: 8fa1c8ad67a3ebb2c4e0d3e44d2ece2404cb679e
4
+ data.tar.gz: 2362ff51b7d433bc82fb51314496af9801145bde
5
5
  SHA512:
6
- metadata.gz: 21eed9f42d42a66a273413cdd784b32e3f1b07b9be39e6c34e8dba7162c442ad2cb4b96be93177a4d6b33e87e20365524b4e5d72bccbe27cbc22d1f0943c28db
7
- data.tar.gz: 6de12c71400aa5f5cbd50b7614df418c50815ddf4f222cfe56b3cc852acbd2a745108e3f56394f065de4fb9c518fe02b0a77ddc02669f626b17f0c77b7b17054
6
+ metadata.gz: ac6b7fe0376149cb813893fdb64822f69e0f78972b21bb19d8d1be2192b395b386a2d565956197c613576aff80075d1eb609683af342a3b0728553c6fb32e9a8
7
+ data.tar.gz: 278121be9eb0a87815c2b878059a37c583b27296d9667c848e75a9f05bdd4b09b85051a0f2c254aafbb83da0691d6fa6fe5d611810536bac3e62b3056b52701f
@@ -2,7 +2,7 @@ version: 2
2
2
  jobs:
3
3
  build:
4
4
  docker:
5
- - image: darthjee/circleci_ruby_240:0.1.0
5
+ - image: darthjee/circleci_ruby_gems_240:0.0.2
6
6
  steps:
7
7
  - checkout
8
8
  - run:
@@ -23,3 +23,9 @@ jobs:
23
23
  - run:
24
24
  name: Yardstick coverage check
25
25
  command: bundle exec rake verify_measurements
26
+ - run:
27
+ name: Check version documentation
28
+ command: scripts/check_readme.sh
29
+ - run:
30
+ name: Rubycritcs check
31
+ command: scripts/rubycritic.sh
data/.gitignore CHANGED
@@ -4,3 +4,5 @@ Gemfile.lock
4
4
  doc
5
5
  .yardoc
6
6
  measurement
7
+ rubycritic
8
+ **/.*.swp
@@ -0,0 +1,3 @@
1
+ detectors:
2
+ BooleanParameter:
3
+ enabled: false
@@ -1,19 +1,26 @@
1
1
  ## Array
2
- ### #average
3
- returns the average of the values in the array
2
+
3
+ ### #as_hash
4
+ Creates a hash from the array using the argument array as keys
4
5
 
5
6
  ```ruby
6
- array = [1, 2, 3, 4]
7
- array.average # returns 2.5
7
+ array = [10, 20, 30, 40]
8
+ keys = %i[ten twenty thirty fourty]
9
+
10
+ array.as_hash(keys) # returns {
11
+ # ten: 10,
12
+ # twenty: 20,
13
+ # thirty: 30,
14
+ # fourty: 40
15
+ # }
8
16
  ```
9
17
 
10
- ### #map_to_hash
11
- map returning a hash with the original array for keys
18
+ ### #average
19
+ Returns the average of the values in the array
12
20
 
13
21
  ```ruby
14
- array = %w(a ab)
15
- arrays.map_to_hash { |val| val.length }
16
- { 'a' => 1, 'b' => 2 }
22
+ array = [1, 2, 3, 4]
23
+ array.average # returns 2.5
17
24
  ```
18
25
 
19
26
  ### #chain_map
@@ -22,7 +29,8 @@ applies map in a chain
22
29
  ```ruby
23
30
  array = [ :a, :long_name, :sym ]
24
31
  array.chain_map(:to_s, :size, :to_s)
25
- [ '1', '9', '3' ]
32
+
33
+ # returns [ '1', '9', '3' ]
26
34
  ```
27
35
 
28
36
  ```ruby
@@ -31,13 +39,62 @@ array.chain_map(:to_s, :size) { |v| "final: #{v}" }
31
39
  [ 'final: 1', 'final: 9', 'final: 3' ]
32
40
  ```
33
41
 
34
- ### #as_hash
35
- Creates a hash from the array using the argumen array as keys
42
+ ### #mapk
43
+ Maps by fetching values from hashes inside array
44
+
45
+ ```ruby
46
+ array = [
47
+ { a: { b: 1 }, b: 2 },
48
+ { a: { b: 3 }, b: 4 }
49
+ ]
50
+
51
+ array.mapk(:a, :b) # return [1, 3]
52
+ ```
53
+
54
+ ### #procedural_join
55
+ Maps values to strings and joins then by evaluating which
56
+ string to be used on joining
36
57
 
37
58
  ```ruby
38
- [1, 2, 3].as_hash %w(a b c)
59
+ mapper = proc { |value| value.to_f.to_s }
60
+ array.procedural_join(mapper) do |_previous, nexte|
61
+ nexte.positive? ? ' +' : ' '
62
+ end # returns '1.0 +2.0 -3.0 -4.0 +5.0'
39
63
  ```
40
- returns
64
+
65
+ ### #random
66
+ Returns a random element of the array
67
+
41
68
  ```ruby
42
- { 'a' => 1, 'b' => 2, 'c' => 3 } }
69
+ array = [10, 20, 30]
70
+ array.random # returns any of the elements
71
+ array.include?(array.random!) # returns true
43
72
  ```
73
+
74
+ ### #random!
75
+ Removes a random element of the array
76
+
77
+ ```ruby
78
+ array = [10, 20, 30]
79
+ array.random! # returns any of the elements
80
+
81
+ array = [10, 20, 30]
82
+ array.include?(array.random!) # returns false
83
+ ```
84
+
85
+ ## Added by Enumerable
86
+ - [#clean](ENUMERABLE_README.md#clean)
87
+ - [#clean!](ENUMERABLE_README.md#clean!)
88
+ - [#map_and_find](ENUMERABLE_README.md#map_and_find)
89
+ - [#map_and_select](ENUMERABLE_README.md#map_and_select)
90
+ - [#map_to_hash](ENUMERABLE_README.md#map_to_hash)
91
+
92
+ ## Added by Object
93
+ - [#is_any?](OBJECT_README.md#is_any?)
94
+ - [#trueful?](OBJECT_README.md#trueful?)
95
+
96
+ ## Added by Class
97
+ - [.default_value](CLASS_README.md#default_value)
98
+ - [.default_values](CLASS_README.md#default_values)
99
+ - [.default_reader](CLASS_README.md#default_reader)
100
+ - [.default_readers](CLASS_README.md#default_readers)
@@ -0,0 +1,154 @@
1
+ ## Class
2
+
3
+ ### #default_value
4
+ Adds a method that will return a default value
5
+
6
+ the value is evaluated on class definition, meaning that
7
+ everytime it is called it will be the same instance
8
+
9
+ ```ruby
10
+ class MyClass
11
+ default_value :name, 'John'
12
+ end
13
+
14
+ MyClass.new.name # returns 'John'
15
+ ```
16
+
17
+ ```ruby
18
+ class MyClass
19
+ default_value :name, 'John'
20
+ end
21
+
22
+ instance = MyClass.new
23
+ other = MyClass.new
24
+
25
+ instance.name.equal?('John') # returns false
26
+ instance.name.equal?(other.name) # returns true
27
+ ```
28
+
29
+ ### #default_values
30
+ Adds methods that will return a default value
31
+
32
+ the value is evaluated on class definition, meaning that
33
+ everytime any of them are called they will return the same instance
34
+ of value
35
+
36
+ ```ruby
37
+ class MyClass
38
+ default_values :name, :nick_name, 'John'
39
+ end
40
+
41
+ MyClass.new.name # returns 'John'
42
+ MyClass.new.nick_name # returns 'John'
43
+ ```
44
+
45
+ ```ruby
46
+ class MyClass
47
+ default_values :name, :nick_name, 'John'
48
+ end
49
+
50
+ instance = MyClass.new
51
+ other = MyClass.new
52
+
53
+ instance.name.equal?('John') # returns false
54
+ instance.name.equal?(other.name) # returns true
55
+ ```
56
+
57
+ ```ruby
58
+ class MyClass
59
+ default_values :name, :nick_name, 'John'
60
+ end
61
+
62
+ instance = MyClass.new
63
+
64
+ instance.nick_name.equal?('John') # returns false
65
+ instance.nick_name.equal?(instance.name) # returns true
66
+ ```
67
+
68
+ ### #default_reader
69
+ Creates a method that will act as reader, but will
70
+ return a default value when the instance variable
71
+ was never set
72
+
73
+ ```ruby
74
+ class Person
75
+ attr_writer :name
76
+ default_reader :name, 'John Doe'
77
+ end
78
+
79
+ model = Person.new
80
+
81
+ model.name # returns 'John Doe'
82
+ ```
83
+
84
+ ```ruby
85
+ model = Person.new
86
+ model.name # returns 'John Doe'
87
+
88
+ model.name = 'Joe'
89
+ model.name # returns 'Joe'
90
+
91
+ model.name = nil
92
+ model.name # returns nil
93
+ ```
94
+
95
+ ```ruby
96
+ model = Person.new
97
+ model.name # returns 'John Doe'
98
+
99
+ model.name = 'Bob'
100
+ model.name # returns 'Bob'
101
+ Person.new.name # returns 'John Doe'
102
+ ```
103
+
104
+ ### #default_readers
105
+ Creates methods that will act as readers, but will
106
+ return a default value when the instance variables
107
+ ware never set
108
+
109
+ ```ruby
110
+ class Person
111
+ attr_writer :cars, :houses
112
+ default_reader :cars, :houses, 'none'
113
+ end
114
+
115
+ model = Person.new
116
+
117
+ model.cars # returns 'none'
118
+ ```
119
+
120
+ ```ruby
121
+ model = Person.new
122
+ model.cars # returns 'none'
123
+
124
+ model.cars = ['volvo']
125
+ model.cars # returns ['volvo']
126
+
127
+ model.cars = nil
128
+ model.cars # returns nil
129
+ ```
130
+
131
+ ```ruby
132
+ model = Person.new
133
+ model.cars # returns 'none'
134
+
135
+ model.cars = ['volvo']
136
+ model.cars # returns ['volvo']
137
+ Person.new.houses # returns 'none'
138
+ ```
139
+
140
+ ```ruby
141
+ model.cars # returns 'none'
142
+ model.cars.equal?('none') # returns false
143
+ model.nick_name.equal?(model.houses) # returns true
144
+ ```
145
+
146
+ ## Added by Object
147
+ - [#is_any?](OBJECT_README.md#is_any?)
148
+ - [#trueful?](OBJECT_README.md#trueful?)
149
+
150
+ ## Added by Class
151
+ - [.default_value](CLASS_README.md#default_value)
152
+ - [.default_values](CLASS_README.md#default_values)
153
+ - [.default_reader](CLASS_README.md#default_reader)
154
+ - [.default_readers](CLASS_README.md#default_readers)
@@ -5,15 +5,25 @@
5
5
  Returns the number of days between 2 dates
6
6
 
7
7
  ```ruby
8
- d1 = Date.new(2106, 10, 11)
9
- d2 = d1 + 1.year
10
- d3 = d1 - 1,year
11
- ```
8
+ date = Date.new(2106, 10, 11)
9
+ future = date + 1.year
10
+ past = date - 1.year
12
11
 
13
- ```ruby
14
- d1.days_between(d2) == 365
15
- ```
12
+ date.days_between(future)
16
13
 
17
- ```ruby
18
- d1.days_between(d3) = 366
14
+ # returns 365
15
+
16
+ date.days_between(past)
17
+
18
+ # returns 366
19
19
  ```
20
+
21
+ ## Added by Object
22
+ - [#is_any?](ENUMERABLE_README.md#is_any?)
23
+ - [#trueful?](ENUMERABLE_README.md#trueful?)
24
+
25
+ ## Added by Class
26
+ - [.default_value](CLASS_README.md#default_value)
27
+ - [.default_values](CLASS_README.md#default_values)
28
+ - [.default_reader](CLASS_README.md#default_reader)
29
+ - [.default_readers](CLASS_README.md#default_readers)
data/Dockerfile CHANGED
@@ -1,9 +1,6 @@
1
- FROM darthjee/ruby_240:0.2.2
1
+ FROM darthjee/ruby_gems_240:0.0.2
2
2
 
3
3
  USER app
4
- COPY ./ /home/app/app/
4
+ COPY --chown=app ./ /home/app/app/
5
5
 
6
- RUN gem uninstall bundler
7
- RUN gem install bundler -v '1.17.3'
8
6
  RUN bundle install
9
-
@@ -1,12 +1,162 @@
1
1
  ## Enumerable
2
2
 
3
+ ### #clean
4
+ Retruns a copy of the enumerator removing empty values from it
5
+
6
+ ```ruby
7
+ hash = { b: [], c: nil, d: {}, e: '' }
8
+ array = [1, [], nil, {}, '', hash]
9
+
10
+ array.clean # returns [1]
11
+ ```
12
+
13
+
3
14
  ### #clean!
4
- Cleans empty values from a hash
15
+ Cleans empty values from an enumerator
16
+
17
+ ```ruby
18
+ hash = {
19
+ a: 1,
20
+ b: [],
21
+ c: nil,
22
+ d: {},
23
+ e: '',
24
+ f: {
25
+ b: [],
26
+ c: nil,
27
+ d: {},
28
+ e: ''
29
+ }
30
+ }
31
+
32
+ hash.clean! # changes hash to { a: 1 }
33
+ ```
34
+
35
+ ### #map_and_find
36
+
37
+ Applies ```#map``` stoping and returning on the first result
38
+ that is evaluated as false
39
+
40
+ ```ruby
41
+ class Client
42
+ def initialize(existing_ids)
43
+ @existing_ids = existing_ids
44
+ end
45
+
46
+ def request(id)
47
+ requested << id
48
+
49
+ return unless existing_ids.include?(id)
50
+ { id: id }
51
+ end
52
+
53
+ def requested
54
+ @requested ||= []
55
+ end
56
+
57
+ private
58
+
59
+ attr_reader :existing_ids
60
+ end
61
+
62
+ client = Client.new([1, 11, 21, 31, 41, 51])
63
+
64
+ ids = [10, 21, 30, 31, 51, 55]
65
+
66
+ ids.map_and_find { |id| client.request(id) } # returns { id: 21 }
67
+
68
+ client.requested # returns [10, 21]
69
+ ```
70
+
71
+ ### #map_and_select
72
+
73
+ Maps returning only non-false and non-nil values
74
+
5
75
  ```ruby
6
- { a: 1, b: [], c: nil, d: {}, e: '', f: { b: [], c: nil, d: {}, e: '' } }.clean!
76
+ class Client
77
+ def initialize(existing_ids)
78
+ @existing_ids = existing_ids
79
+ end
80
+
81
+ def request(id)
82
+ requested << id
83
+
84
+ return unless existing_ids.include?(id)
85
+ { id: id }
86
+ end
87
+
88
+ def requested
89
+ @requested ||= []
90
+ end
91
+
92
+ private
93
+
94
+ attr_reader :existing_ids
95
+ end
96
+
97
+ client = Client.new([1, 11, 21, 31, 41, 51])
98
+
99
+ ids = [10, 21, 30, 31, 51, 55]
100
+
101
+ ids.map_and_select do |id|
102
+ client.request(id)
103
+ end # returns [{
104
+ # id: 21
105
+ # }, {
106
+ # id: 31
107
+ # }, {
108
+ # id: 51
109
+ # }]
7
110
  ```
8
- returns
111
+
112
+ ### #map_to_hash
113
+ Maps and uses mapped values as hash values, and original values
114
+ as hash keys
115
+
9
116
  ```ruby
10
- {}
117
+ class Client
118
+ def initialize(existing_ids)
119
+ @existing_ids = existing_ids
120
+ end
121
+
122
+ def request(id)
123
+ requested << id
124
+
125
+ return unless existing_ids.include?(id)
126
+ { id: id }
127
+ end
128
+
129
+ def requested
130
+ @requested ||= []
131
+ end
132
+
133
+ private
134
+
135
+ attr_reader :existing_ids
136
+ end
137
+
138
+ client = Client.new([1, 11, 21, 31, 41, 51])
139
+
140
+ ids = [10, 21, 30, 31, 51, 55]
141
+
142
+ ids.map_to_hash do |id|
143
+ client.request(id)
144
+ end # returns {
145
+ # 10 => nil,
146
+ # 21 => { id: 21 },
147
+ # 30 => nil,
148
+ # 31 => { id: 31 },
149
+ # 51 => { id: 51 },
150
+ # 55 => nil
151
+ # }
11
152
  ```
12
153
 
154
+ ## Added by Object
155
+ - [#is_any?](OBJECT_README.md#is_any?)
156
+ - [#trueful?](OBJECT_README.md#trueful?)
157
+
158
+ ## Added by Class
159
+ - [.default_value](CLASS_README.md#default_value)
160
+ - [.default_values](CLASS_README.md#default_values)
161
+ - [.default_reader](CLASS_README.md#default_reader)
162
+ - [.default_readers](CLASS_README.md#default_readers)