darthjee-core_ext 1.5.6 → 1.6.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 (68) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +14 -0
  3. data/.rubocop.yml +17 -0
  4. data/.rubocop_todo.yml +12 -0
  5. data/Gemfile +2 -1
  6. data/Rakefile +2 -0
  7. data/core_ext.gemspec +6 -3
  8. data/lib/darthjee/core_ext/array/hash_builder.rb +21 -13
  9. data/lib/darthjee/core_ext/array.rb +4 -2
  10. data/lib/darthjee/core_ext/date.rb +2 -0
  11. data/lib/darthjee/core_ext/enumerable.rb +5 -3
  12. data/lib/darthjee/core_ext/hash/chain_fetcher.rb +33 -0
  13. data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +62 -60
  14. data/lib/darthjee/core_ext/hash/key_changer.rb +64 -54
  15. data/lib/darthjee/core_ext/hash/keys_sorter.rb +31 -0
  16. data/lib/darthjee/core_ext/hash/squasher.rb +31 -0
  17. data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +21 -0
  18. data/lib/darthjee/core_ext/hash/value_changer.rb +48 -44
  19. data/lib/darthjee/core_ext/hash.rb +28 -58
  20. data/lib/darthjee/core_ext/math.rb +4 -2
  21. data/lib/darthjee/core_ext/numeric.rb +5 -3
  22. data/lib/darthjee/core_ext/object/default_value.rb +2 -1
  23. data/lib/darthjee/core_ext/object.rb +2 -0
  24. data/lib/darthjee/core_ext/symbol.rb +2 -0
  25. data/lib/darthjee/core_ext/time.rb +2 -0
  26. data/lib/darthjee/core_ext/version.rb +3 -1
  27. data/lib/darthjee/core_ext.rb +2 -0
  28. data/lib/darthjee.rb +2 -1
  29. data/spec/lib/array_spec.rb +27 -19
  30. data/spec/lib/date_spec.rb +2 -0
  31. data/spec/lib/enumerable_spec.rb +10 -8
  32. data/spec/lib/hash/chain_fetcher_spec.rb +11 -0
  33. data/spec/lib/hash/deep_hash_constructor_spec.rb +3 -1
  34. data/spec/lib/hash/key_changer_spec.rb +19 -5
  35. data/spec/lib/hash/keys_sorter_spec.rb +10 -0
  36. data/spec/lib/hash/squasher_spec.rb +9 -0
  37. data/spec/lib/hash/to_hash_mapper_spec.rb +10 -0
  38. data/spec/lib/hash_spec.rb +16 -46
  39. data/spec/lib/math_spec.rb +2 -0
  40. data/spec/lib/numeric_spec.rb +2 -0
  41. data/spec/lib/object/default_value_spe.rb +2 -0
  42. data/spec/lib/object_spec.rb +2 -0
  43. data/spec/lib/symbol_spec.rb +2 -0
  44. data/spec/lib/time_spec.rb +2 -0
  45. data/spec/spec_helper.rb +2 -1
  46. data/spec/support/models/default_value.rb +2 -0
  47. data/spec/support/models/hash/value_changer/dummy.rb +19 -13
  48. data/spec/support/models/hash/value_changer/dummy_iteractor.rb +13 -8
  49. data/spec/support/shared_examples/{array_random.rb → array/array_random.rb} +5 -2
  50. data/spec/support/shared_examples/clean.rb +14 -2
  51. data/spec/support/shared_examples/date.rb +2 -0
  52. data/spec/support/shared_examples/expected.rb +2 -1
  53. data/spec/support/shared_examples/{chain_fetch.rb → hash/chain_fetch.rb} +9 -7
  54. data/spec/support/shared_examples/{chain_hash_keys_changer.rb → hash/chain_hash_keys_changer.rb} +14 -11
  55. data/spec/support/shared_examples/{hash_keys_changer.rb → hash/hash_keys_changer.rb} +7 -5
  56. data/spec/support/shared_examples/hash/hash_squasher.rb +23 -0
  57. data/spec/support/shared_examples/{hash_transpose.rb → hash/hash_transpose.rb} +8 -6
  58. data/spec/support/shared_examples/hash/keys_appender.rb +69 -0
  59. data/spec/support/shared_examples/{keys_camelizer.rb → hash/keys_camelizer.rb} +6 -4
  60. data/spec/support/shared_examples/hash/keys_sorter.rb +67 -0
  61. data/spec/support/shared_examples/{keys_underscorer.rb → hash/keys_underscorer.rb} +4 -3
  62. data/spec/support/shared_examples/{map_to_hash.rb → hash/map_to_hash.rb} +16 -16
  63. data/spec/support/shared_examples/{remap.rb → hash/remap.rb} +16 -13
  64. data/spec/support/shared_examples/hash/value_changer.rb +192 -0
  65. metadata +76 -30
  66. data/circle.yml +0 -10
  67. data/spec/support/shared_examples/keys_appender.rb +0 -43
  68. data/spec/support/shared_examples/value_changer.rb +0 -147
@@ -1,147 +0,0 @@
1
- shared_examples 'a class with change_values method' do
2
- let(:subject) { { a: 1, b: 2, c: { d: 3, e: 4 } } }
3
- let(:inner_hash) { subject[:c] }
4
-
5
- describe :change_values do
6
- it_behaves_like 'a method that change the hash values', :change_values
7
-
8
- it 'does not change original hash' do
9
- expect do
10
- subject.change_values { |value| value + 1 }
11
- end.not_to change { subject }
12
- end
13
-
14
- it 'does not change original hash' do
15
- expect do
16
- subject.change_values { |value| value + 1 }
17
- end.not_to change { inner_hash }
18
- end
19
-
20
- context 'when using an array' do
21
- let(:subject) { { a: [{ b: 1}]} }
22
- let(:inner_array) { subject[:a] }
23
-
24
- it 'does not change original hash' do
25
- expect do
26
- subject.change_values { |value| value + 1 }
27
- end.not_to change { inner_array }
28
- end
29
- end
30
- end
31
-
32
- describe :change_values! do
33
- it_behaves_like 'a method that change the hash values', :change_values!
34
-
35
- it 'changes original hash' do
36
- expect do
37
- subject.change_values! { |value| value + 1 }
38
- end.to change { subject }
39
- end
40
-
41
- it 'changes original hash' do
42
- expect do
43
- subject.change_values! { |value| value + 1 }
44
- end.to change { inner_hash }
45
- end
46
-
47
- context 'when using an array' do
48
- let(:subject) { { a: [{ b: 1}]} }
49
- let(:inner_array) { subject[:a] }
50
-
51
- it 'changes original hash' do
52
- expect do
53
- subject.change_values! { |value| value + 1 }
54
- end.to change { inner_array }
55
- end
56
- end
57
- end
58
- end
59
-
60
- shared_examples 'a method that change the hash values' do |method|
61
- context 'when using deeply nested hashes' do
62
- it 'updates values of hash' do
63
- expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
64
- end
65
-
66
- it 'works recursively when parameter is passed' do
67
- expect(subject.public_send(method, recursive: true) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
68
- end
69
-
70
- it 'does not work recursively when parameter is passed as false' do
71
- expect(subject.public_send(method, recursive: false) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
72
- end
73
-
74
- it 'does not ignore hash when option is passed' do
75
- expect(subject.public_send(method, skip_inner: false) { |value| value.is_a?(Hash) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: 12)
76
- end
77
-
78
- it 'ignore hash and work recursively when option is passed' do
79
- expect(subject.public_send(method, skip_inner: true) { |value| value.is_a?(Hash) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
80
- end
81
-
82
- it 'ignore hash and does not work recursively when option is passed' do
83
- expect(subject.public_send(method, skip_inner: false, recursive: false) { |value| value.is_a?(Hash) ? value : value + 1 }).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
84
- end
85
- end
86
-
87
- context 'when using deeply nested arrays' do
88
- let(:subject) { { a: 1, b: 2, c: [{ d: 3 }, { e: { f: 4 } }, 5 ] } }
89
-
90
- it 'goes recursivly true arrays' do
91
- expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }, 6])
92
- end
93
-
94
- it 'does not work recursively when parameter is passed as false' do
95
- expect(subject.public_send(method, recursive: false) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 3 }, { e: { f: 4 } }, 5])
96
- end
97
-
98
- it 'does not ignore array when option is passed' do
99
- expect(subject.public_send(method, skip_inner: false) { |value| value.is_a?(Array) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: 13)
100
- end
101
-
102
- it 'ignores array when option is passed' do
103
- expect(subject.public_send(method, skip_inner: true) { |value| value.is_a?(Array) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }, 6])
104
- end
105
-
106
- it 'ignore hash and does not work recursively when option is passed' do
107
- expect(subject.public_send(method, skip_inner: false, recursive: false) { |value| value.is_a?(Array) ? value : value + 1 }).to eq(a: 2, b: 3, c: [{ d: 3 }, { e: { f: 4 } }, 5])
108
- end
109
- end
110
-
111
- context 'when using a nested extra class' do
112
- let(:subject) { { a: 1, b: 2, c: Hash::ValueChanger::Dummy.new(3) } }
113
-
114
- it 'goes perform the mapping with the extra class' do
115
- expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: 4)
116
- end
117
-
118
- context 'when class is an interactor' do
119
- let(:subject) { { a: 1, b: 2, c: Hash::ValueChanger::DummyIteractor.new({ d: 3 }, { e: { f: 4 } }) } }
120
-
121
- it 'goes through the iteractor' do
122
- expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }])
123
- end
124
- end
125
-
126
- context 'when using mapping inner array with inner object into a new hash' do
127
- let(:object) { Hash::ValueChanger::Dummy.new(2) }
128
- let(:array) { Hash::ValueChanger::DummyIteractor.new(object) }
129
- let(:subject) { { a: 1, b: array } }
130
- let(:result) do
131
- subject.public_send(method, skip_inner: false) do |value|
132
- if value.is_a?(Numeric)
133
- value + 10
134
- elsif value.is_a?(Hash) || value.is_a?(Hash::ValueChanger::DummyIteractor)
135
- value
136
- else
137
- value.as_json
138
- end
139
- end
140
- end
141
-
142
- it 'process the object after processing the array' do
143
- expect(result).to eq(a: 11, b: [{ val: 12 }])
144
- end
145
- end
146
- end
147
- end