darthjee-core_ext 1.6.2 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/core_ext.gemspec +1 -1
  3. data/lib/darthjee/core_ext.rb +10 -6
  4. data/lib/darthjee/core_ext/array.rb +42 -34
  5. data/lib/darthjee/core_ext/array/hash_builder.rb +22 -17
  6. data/lib/darthjee/core_ext/class.rb +23 -0
  7. data/lib/darthjee/core_ext/date.rb +11 -3
  8. data/lib/darthjee/core_ext/hash.rb +24 -16
  9. data/lib/darthjee/core_ext/hash/chain_fetcher.rb +27 -23
  10. data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +73 -64
  11. data/lib/darthjee/core_ext/hash/key_changeable.rb +137 -133
  12. data/lib/darthjee/core_ext/hash/key_changer.rb +84 -80
  13. data/lib/darthjee/core_ext/hash/keys_sorter.rb +25 -21
  14. data/lib/darthjee/core_ext/hash/squasher.rb +24 -20
  15. data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +17 -13
  16. data/lib/darthjee/core_ext/hash/transformable.rb +20 -16
  17. data/lib/darthjee/core_ext/hash/transposeable.rb +18 -14
  18. data/lib/darthjee/core_ext/hash/value_changer.rb +52 -48
  19. data/lib/darthjee/core_ext/numeric.rb +12 -4
  20. data/lib/darthjee/core_ext/object.rb +12 -6
  21. data/lib/darthjee/core_ext/symbol.rb +14 -6
  22. data/lib/darthjee/core_ext/version.rb +1 -1
  23. data/spec/lib/{object/default_value_spe.rb → class_spec.rb} +3 -3
  24. data/spec/lib/{hash → darthjee/core_ext/hash}/chain_fetcher_spec.rb +1 -1
  25. data/spec/lib/{hash → darthjee/core_ext/hash}/deep_hash_constructor_spec.rb +1 -1
  26. data/spec/lib/{hash → darthjee/core_ext/hash}/key_changer_spec.rb +1 -1
  27. data/spec/lib/{hash → darthjee/core_ext/hash}/keys_sorter_spec.rb +1 -1
  28. data/spec/lib/{hash → darthjee/core_ext/hash}/squasher_spec.rb +1 -1
  29. data/spec/lib/{hash → darthjee/core_ext/hash}/to_hash_mapper_spec.rb +1 -1
  30. data/spec/support/models/{default_value.rb → default_value_model.rb} +1 -1
  31. metadata +23 -23
  32. data/lib/darthjee/core_ext/object/default_value.rb +0 -17
@@ -1,139 +1,143 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Hash
4
- module KeyChangeable
5
- def remap_keys(remap)
6
- dup.remap_keys!(remap)
7
- end
8
-
9
- def remap_keys!(keys_map)
10
- KeyChanger.new(self).remap(keys_map)
11
- end
12
-
13
- def lower_camelize_keys(options = {})
14
- dup.lower_camelize_keys!(options)
15
- end
16
-
17
- def lower_camelize_keys!(options = {})
18
- options = options.merge(uppercase_first_letter: false)
19
-
20
- camelize_keys!(options)
21
- end
22
-
23
- def camelize_keys(options = {})
24
- dup.camelize_keys!(options)
25
- end
26
-
27
- def camelize_keys!(options = {})
28
- Hash::KeyChanger.new(self).camelize_keys(options)
29
- end
30
-
31
- def underscore_keys(options = {})
32
- dup.underscore_keys!(options)
33
- end
34
-
35
- def underscore_keys!(options = {})
36
- Hash::KeyChanger.new(self).underscore_keys(options)
37
- end
38
-
39
- # change all keys returning the new map
40
- # options: { recursive: true }
41
- # ex: { "a" =>1 }.change_keys{ |key| key.upcase } == { "A" => 1 }
42
- def change_keys(options = {}, &block)
43
- deep_dup.change_keys!(options, &block)
44
- end
45
-
46
- # change all keys returning the new map
47
- # options: { recursive: true }
48
- # ex: { "a":1 }.change_keys{ |key| key.upcase } == { "A":1 }
49
- def change_keys!(options = {}, &block)
50
- Hash::KeyChanger.new(self).change_keys(options, &block)
51
- end
52
-
53
- # change all publicaly sending method calls
54
- # options: { recursive: true }
55
- # ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
56
- def chain_change_keys(*calls)
57
- deep_dup.chain_change_keys!(*calls)
58
- end
59
-
60
- # change all publicaly sending method calls
61
- # options: { recursive: true }
62
- # ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
63
- def chain_change_keys!(*calls)
64
- options = calls.extract_options!
65
-
66
- calls.inject(self) do |h, m|
67
- h.change_keys!(options, &m)
3
+ module Darthjee
4
+ module CoreExt
5
+ module Hash
6
+ module KeyChangeable
7
+ def remap_keys(remap)
8
+ dup.remap_keys!(remap)
9
+ end
10
+
11
+ def remap_keys!(keys_map)
12
+ KeyChanger.new(self).remap(keys_map)
13
+ end
14
+
15
+ def lower_camelize_keys(options = {})
16
+ dup.lower_camelize_keys!(options)
17
+ end
18
+
19
+ def lower_camelize_keys!(options = {})
20
+ options = options.merge(uppercase_first_letter: false)
21
+
22
+ camelize_keys!(options)
23
+ end
24
+
25
+ def camelize_keys(options = {})
26
+ dup.camelize_keys!(options)
27
+ end
28
+
29
+ def camelize_keys!(options = {})
30
+ Hash::KeyChanger.new(self).camelize_keys(options)
31
+ end
32
+
33
+ def underscore_keys(options = {})
34
+ dup.underscore_keys!(options)
35
+ end
36
+
37
+ def underscore_keys!(options = {})
38
+ Hash::KeyChanger.new(self).underscore_keys(options)
39
+ end
40
+
41
+ # change all keys returning the new map
42
+ # options: { recursive: true }
43
+ # ex: { "a" =>1 }.change_keys{ |key| key.upcase } == { "A" => 1 }
44
+ def change_keys(options = {}, &block)
45
+ deep_dup.change_keys!(options, &block)
46
+ end
47
+
48
+ # change all keys returning the new map
49
+ # options: { recursive: true }
50
+ # ex: { "a":1 }.change_keys{ |key| key.upcase } == { "A":1 }
51
+ def change_keys!(options = {}, &block)
52
+ Hash::KeyChanger.new(self).change_keys(options, &block)
53
+ end
54
+
55
+ # change all publicaly sending method calls
56
+ # options: { recursive: true }
57
+ # ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
58
+ def chain_change_keys(*calls)
59
+ deep_dup.chain_change_keys!(*calls)
60
+ end
61
+
62
+ # change all publicaly sending method calls
63
+ # options: { recursive: true }
64
+ # ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
65
+ def chain_change_keys!(*calls)
66
+ options = calls.extract_options!
67
+
68
+ calls.inject(self) do |h, m|
69
+ h.change_keys!(options, &m)
70
+ end
71
+ end
72
+
73
+ # prepend a string to all keys
74
+ # options {
75
+ # recursive: true,
76
+ # type: :keep [keep, string, symbol] (key type to be returned)
77
+ # }
78
+ # ex: { :a => 1, "b"=> 2 }.prepend_to_keys("foo_")
79
+ # # returns { :foo_a => 1, "foo_b"=> 2 }
80
+ def prepend_to_keys(str, options = {})
81
+ change_key_text(options) do |key|
82
+ "#{str}#{key}"
83
+ end
84
+ end
85
+
86
+ # append a string to all keys
87
+ # options {
88
+ # recursive: true,
89
+ # type: :keep [keep, string, symbol] (key type to be returned)
90
+ # }
91
+ # ex: { :a => 1, "b"=> 2 }.append_to_keys("_bar")
92
+ # # returns { :a_bar => 1, "b_bar"=> 2 }
93
+ def append_to_keys(str, options = {})
94
+ change_key_text(options) do |key|
95
+ "#{key}#{str}"
96
+ end
97
+ end
98
+
99
+ # sorts keys for hash
100
+ # options: { recursive: true }
101
+ # ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }
102
+ def sort_keys(options = {})
103
+ Hash::KeysSorter.new(self, **options).sort
104
+ end
105
+
106
+ ##########################################
107
+ # Value change methods
108
+ ##########################################
109
+
110
+ # creates a new hash with changes in its values
111
+ # options: {
112
+ # recursive: true,
113
+ # skip_hash:true
114
+ # }
115
+ # ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 }
116
+ # ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s }
117
+ # # returns { a:"1", b:"{ c=>1 }
118
+ # ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s }
119
+ # # returns { a:"1", b:{ c=>"1" } }
120
+ def change_values(options = {}, &block)
121
+ deep_dup.change_values!(options, &block)
122
+ end
123
+
124
+ def change_values!(options = {}, &block)
125
+ Hash::ValueChanger.new(options, &block).change(self)
126
+ end
127
+
128
+ private
129
+
130
+ # changes the text of the keys
131
+ # options {
132
+ # recursive: true,
133
+ # type: :keep [keep, string, symbol] (key type to be returned)
134
+ # }
135
+ # ex: { :a => 1, "b"=> 2 }.change_key_text{ |key| key.upcase }
136
+ # # returns { :A => 1, "B"=> 2 }
137
+ def change_key_text(options = {}, &block)
138
+ Hash::KeyChanger.new(self).change_text(options, &block)
139
+ end
68
140
  end
69
141
  end
70
-
71
- # prepend a string to all keys
72
- # options {
73
- # recursive: true,
74
- # type: :keep [keep, string, symbol] (key type to be returned)
75
- # }
76
- # ex: { :a => 1, "b"=> 2 }.prepend_to_keys("foo_")
77
- # # returns { :foo_a => 1, "foo_b"=> 2 }
78
- def prepend_to_keys(str, options = {})
79
- change_key_text(options) do |key|
80
- "#{str}#{key}"
81
- end
82
- end
83
-
84
- # append a string to all keys
85
- # options {
86
- # recursive: true,
87
- # type: :keep [keep, string, symbol] (key type to be returned)
88
- # }
89
- # ex: { :a => 1, "b"=> 2 }.append_to_keys("_bar")
90
- # # returns { :a_bar => 1, "b_bar"=> 2 }
91
- def append_to_keys(str, options = {})
92
- change_key_text(options) do |key|
93
- "#{key}#{str}"
94
- end
95
- end
96
-
97
- # sorts keys for hash
98
- # options: { recursive: true }
99
- # ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }
100
- def sort_keys(options = {})
101
- Hash::KeysSorter.new(self, **options).sort
102
- end
103
-
104
- ##########################################
105
- # Value change methods
106
- ##########################################
107
-
108
- # creates a new hash with changes in its values
109
- # options: {
110
- # recursive: true,
111
- # skip_hash:true
112
- # }
113
- # ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 }
114
- # ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s }
115
- # # returns { a:"1", b:"{ c=>1 }
116
- # ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s }
117
- # # returns { a:"1", b:{ c=>"1" } }
118
- def change_values(options = {}, &block)
119
- deep_dup.change_values!(options, &block)
120
- end
121
-
122
- def change_values!(options = {}, &block)
123
- Hash::ValueChanger.new(options, &block).change(self)
124
- end
125
-
126
- private
127
-
128
- # changes the text of the keys
129
- # options {
130
- # recursive: true,
131
- # type: :keep [keep, string, symbol] (key type to be returned)
132
- # }
133
- # ex: { :a => 1, "b"=> 2 }.change_key_text{ |key| key.upcase }
134
- # # returns { :A => 1, "B"=> 2 }
135
- def change_key_text(options = {}, &block)
136
- Hash::KeyChanger.new(self).change_text(options, &block)
137
- end
138
142
  end
139
143
  end
@@ -1,86 +1,90 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Hash
4
- class KeyChanger
5
- def initialize(hash)
6
- @hash = hash
7
- end
8
-
9
- def remap(keys_map)
10
- new_hash = {}
11
- keys_map.each do |o, n|
12
- new_hash[n] = hash.delete(o)
13
- end
14
- hash.merge! new_hash
15
- end
16
-
17
- def change_keys(settings = {}, &block)
18
- merge_options({
19
- recursive: true
20
- }, settings)
21
-
22
- if options[:recursive]
23
- hash.deep_transform_keys!(&block)
24
- else
25
- hash.transform_keys!(&block)
26
- end
27
- end
28
-
29
- def camelize_keys(settings = {})
30
- merge_options({
31
- uppercase_first_letter: true
32
- }, settings)
33
-
34
- type = options[:uppercase_first_letter] ? :upper : :lower
35
-
36
- change_keys do |k|
37
- k.camelize(type)
3
+ module Darthjee
4
+ module CoreExt
5
+ module Hash
6
+ class KeyChanger
7
+ def initialize(hash)
8
+ @hash = hash
9
+ end
10
+
11
+ def remap(keys_map)
12
+ new_hash = {}
13
+ keys_map.each do |o, n|
14
+ new_hash[n] = hash.delete(o)
15
+ end
16
+ hash.merge! new_hash
17
+ end
18
+
19
+ def change_keys(settings = {}, &block)
20
+ merge_options({
21
+ recursive: true
22
+ }, settings)
23
+
24
+ if options[:recursive]
25
+ hash.deep_transform_keys!(&block)
26
+ else
27
+ hash.transform_keys!(&block)
28
+ end
29
+ end
30
+
31
+ def camelize_keys(settings = {})
32
+ merge_options({
33
+ uppercase_first_letter: true
34
+ }, settings)
35
+
36
+ type = options[:uppercase_first_letter] ? :upper : :lower
37
+
38
+ change_keys do |k|
39
+ k.camelize(type)
40
+ end
41
+ end
42
+
43
+ def underscore_keys(settings = {})
44
+ merge_options({}, settings)
45
+
46
+ change_keys(&:underscore)
47
+ end
48
+
49
+ def change_text(options = {})
50
+ merge_options({
51
+ type: :keep
52
+ }, options)
53
+
54
+ change_keys do |key|
55
+ cast_new_key yield(key), key.class
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ attr_reader :hash
62
+
63
+ def merge_options(default, custom)
64
+ @options = {}.merge!(default).merge!(custom).merge!(options)
65
+ end
66
+
67
+ def options
68
+ @options ||= {}
69
+ end
70
+
71
+ def cast_new_key(key, old_clazz)
72
+ case class_cast(old_clazz)
73
+ when :symbol then
74
+ key.to_sym
75
+ when :string then
76
+ key.to_s
77
+ end
78
+ end
79
+
80
+ def keep_class?
81
+ options[:type] == :keep
82
+ end
83
+
84
+ def class_cast(old_clazz)
85
+ keep_class? && old_clazz.to_s.downcase.to_sym || options[:type]
86
+ end
38
87
  end
39
88
  end
40
-
41
- def underscore_keys(settings = {})
42
- merge_options({}, settings)
43
-
44
- change_keys(&:underscore)
45
- end
46
-
47
- def change_text(options = {})
48
- merge_options({
49
- type: :keep
50
- }, options)
51
-
52
- change_keys do |key|
53
- cast_new_key yield(key), key.class
54
- end
55
- end
56
-
57
- private
58
-
59
- attr_reader :hash
60
-
61
- def merge_options(default, custom)
62
- @options = {}.merge!(default).merge!(custom).merge!(options)
63
- end
64
-
65
- def options
66
- @options ||= {}
67
- end
68
-
69
- def cast_new_key(key, old_clazz)
70
- case class_cast(old_clazz)
71
- when :symbol then
72
- key.to_sym
73
- when :string then
74
- key.to_s
75
- end
76
- end
77
-
78
- def keep_class?
79
- options[:type] == :keep
80
- end
81
-
82
- def class_cast(old_clazz)
83
- keep_class? && old_clazz.to_s.downcase.to_sym || options[:type]
84
- end
85
89
  end
86
90
  end
@@ -1,31 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Hash
4
- class KeysSorter
5
- def initialize(hash, recursive: true)
6
- @hash = hash
7
- @recursive = recursive
8
- end
3
+ module Darthjee
4
+ module CoreExt
5
+ module Hash
6
+ class KeysSorter
7
+ def initialize(hash, recursive: true)
8
+ @hash = hash
9
+ @recursive = recursive
10
+ end
9
11
 
10
- def sort
11
- {}.tap do |new_hash|
12
- sorted_keys.each do |key|
13
- new_hash[key] = change_value(hash[key])
12
+ def sort
13
+ {}.tap do |new_hash|
14
+ sorted_keys.each do |key|
15
+ new_hash[key] = change_value(hash[key])
16
+ end
17
+ end
14
18
  end
15
- end
16
- end
17
19
 
18
- private
20
+ private
19
21
 
20
- def sorted_keys
21
- hash.keys.sort
22
- end
22
+ def sorted_keys
23
+ hash.keys.sort
24
+ end
23
25
 
24
- def change_value(value)
25
- return value unless value.is_a?(Hash) && recursive
26
- value.sort_keys(recursive: true)
27
- end
26
+ def change_value(value)
27
+ return value unless value.is_a?(Hash) && recursive
28
+ value.sort_keys(recursive: true)
29
+ end
28
30
 
29
- attr_reader :hash, :recursive
31
+ attr_reader :hash, :recursive
32
+ end
33
+ end
30
34
  end
31
35
  end