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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 738212a365d4f2b6182a1f71519d482998f71f7f
4
- data.tar.gz: a5028fd0f7b6f62b5d7ba0510204c21fbafb3a52
3
+ metadata.gz: dd01e58fe4568b6b2c651415bb7677f78265fee8
4
+ data.tar.gz: 21b3f51a75beeffad0404ce44696bf57ebe43bf4
5
5
  SHA512:
6
- metadata.gz: 6386535f1ed9bed1fc21e6f157d4117322454cc0413f6bb01461f130fd8da860d6b02a5aaa634f60116e0e5e2703fc4515f4dc4fce0dfc08af86fc6b0d77489f
7
- data.tar.gz: ff41c39af8f135b64f84f1cc5f21d51536490ada52003570c66add9c3887282b812a35249b3c6a43eb7ec628b1f43543757dbe5fc40aa3a02800986776dde1ae
6
+ metadata.gz: e0df37f38b68eae72c79633ab6e31a0e733591dc63164530b34dc326725a47cb158439813f567e3a7ecb5967ae0b245200001312f905cc4a86a5b067cf3f9e5d
7
+ data.tar.gz: 4c4955a0d35d16e0b7ccb1aebedecc8635a6534a58bafb665eb1f827a63e11c1a65809f16a390877cc6f684b08437a702ba87d63e0d2b2e740f548279a6789ea
@@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
20
20
 
21
21
  gem.add_runtime_dependency 'activesupport', '>= 5.2.x'
22
22
 
23
- gem.add_development_dependency 'bundler', '~> 1.15.4'
23
+ gem.add_development_dependency 'bundler'
24
24
  gem.add_development_dependency 'pry-nav', '~> 0.2.4'
25
25
  gem.add_development_dependency 'rake', '>= 12.3.1'
26
26
  gem.add_development_dependency 'rspec', '>= 3.8'
@@ -1,18 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/all'
4
- require 'darthjee/core_ext/enumerable'
5
- require 'darthjee/core_ext/hash'
6
- require 'darthjee/core_ext/symbol'
7
4
  require 'darthjee/core_ext/array'
8
- require 'darthjee/core_ext/numeric'
5
+ require 'darthjee/core_ext/class'
9
6
  require 'darthjee/core_ext/date'
10
- require 'darthjee/core_ext/time'
11
- require 'darthjee/core_ext/object'
7
+ require 'darthjee/core_ext/enumerable'
12
8
  require 'darthjee/core_ext/math'
9
+ require 'darthjee/core_ext/numeric'
10
+ require 'darthjee/core_ext/object'
11
+ require 'darthjee/core_ext/symbol'
12
+ require 'darthjee/core_ext/time'
13
13
 
14
14
  module Darthjee
15
15
  module CoreExt
16
+ PATH = 'darthjee/core_ext'
17
+
16
18
  require 'darthjee/core_ext/version'
19
+
20
+ require 'darthjee/core_ext/hash'
17
21
  end
18
22
  end
@@ -1,46 +1,54 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Array
4
- autoload :HashBuilder, 'darthjee/core_ext/array/hash_builder'
3
+ module Darthjee
4
+ module CoreExt
5
+ module Array
6
+ autoload :HashBuilder, 'darthjee/core_ext/array/hash_builder'
7
+
8
+ def mapk(*keys)
9
+ keys.inject(self) do |enum, key|
10
+ enum.map { |hash| hash[key] }
11
+ end
12
+ end
5
13
 
6
- def mapk(*keys)
7
- keys.inject(self) do |enum, key|
8
- enum.map { |hash| hash[key] }
9
- end
10
- end
14
+ def procedural_join(mapper = proc(&:to_s))
15
+ return '' if empty?
16
+ list = dup
17
+ prev = first
18
+ list[0] = mapper.call(prev).to_s
19
+
20
+ list.inject do |string, val|
21
+ j = yield(prev, val) if block_given?
22
+ "#{string}#{j}#{mapper.call(val)}".tap do
23
+ prev = val
24
+ end
25
+ end
26
+ end
11
27
 
12
- def procedural_join(mapper = proc(&:to_s))
13
- return '' if empty?
14
- list = dup
15
- prev = first
16
- list[0] = mapper.call(prev).to_s
28
+ def chain_map(*methods, &block)
29
+ result = methods.inject(self) do |array, method|
30
+ array.map(&method)
31
+ end
17
32
 
18
- list.inject do |string, val|
19
- j = yield(prev, val) if block_given?
20
- "#{string}#{j}#{mapper.call(val)}".tap do
21
- prev = val
33
+ return result unless block_given?
34
+ result.map(&block)
22
35
  end
23
- end
24
- end
25
-
26
- def chain_map(*methods, &block)
27
- result = methods.inject(self) do |array, method|
28
- array.map(&method)
29
- end
30
36
 
31
- return result unless block_given?
32
- result.map(&block)
33
- end
37
+ def as_hash(keys)
38
+ Array::HashBuilder.new(self, keys).build
39
+ end
34
40
 
35
- def as_hash(keys)
36
- Array::HashBuilder.new(self, keys).build
37
- end
41
+ def random
42
+ self[rand(size)]
43
+ end
38
44
 
39
- def random
40
- self[rand(size)]
45
+ def random!
46
+ slice!(rand(size))
47
+ end
48
+ end
41
49
  end
50
+ end
42
51
 
43
- def random!
44
- slice!(rand(size))
45
- end
52
+ class Array
53
+ include Darthjee::CoreExt::Array
46
54
  end
@@ -1,28 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Array
4
- class HashBuilder
5
- attr_accessor :values, :keys
3
+ module Darthjee
4
+ module CoreExt
5
+ module Array
6
+ class HashBuilder
7
+ attr_accessor :values, :keys
6
8
 
7
- def initialize(values, keys)
8
- @values = values.dup
9
- @keys = keys.dup
10
- end
9
+ def initialize(values, keys)
10
+ @values = values.dup
11
+ @keys = keys.dup
12
+ end
11
13
 
12
- def build
13
- fixes_sizes
14
+ def build
15
+ fixes_sizes
14
16
 
15
- Hash[[keys, values].transpose]
16
- end
17
+ ::Hash[[keys, values].transpose]
18
+ end
17
19
 
18
- private
20
+ private
19
21
 
20
- def fixes_sizes
21
- values.concat Array.new(keys.size - values.size) if needs_resizing?
22
- end
22
+ def fixes_sizes
23
+ return unless needs_resizing?
24
+ values.concat ::Array.new(keys.size - values.size)
25
+ end
23
26
 
24
- def needs_resizing?
25
- keys.size > values.size
27
+ def needs_resizing?
28
+ keys.size > values.size
29
+ end
30
+ end
26
31
  end
27
32
  end
28
33
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Darthjee
4
+ module CoreExt
5
+ module Class
6
+ private
7
+
8
+ def default_value(name, value)
9
+ define_method(name) { |*_| value }
10
+ end
11
+
12
+ def default_values(*names, value)
13
+ names.each do |name|
14
+ default_value(name, value)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class Class
22
+ include Darthjee::CoreExt::Class
23
+ end
@@ -1,7 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Date
4
- def days_between(other_date)
5
- (self - other_date.to_date).abs
3
+ module Darthjee
4
+ module CoreExt
5
+ module Date
6
+ def days_between(other_date)
7
+ (self - other_date.to_date).abs
8
+ end
9
+ end
6
10
  end
7
11
  end
12
+
13
+ class Date
14
+ include Darthjee::CoreExt::Date
15
+ end
@@ -4,24 +4,32 @@ require 'darthjee/core_ext/hash/key_changeable'
4
4
  require 'darthjee/core_ext/hash/transposeable'
5
5
  require 'darthjee/core_ext/hash/transformable'
6
6
 
7
- class Hash
8
- autoload :ChainFetcher, 'darthjee/core_ext/hash/chain_fetcher'
9
- autoload :DeepHashConstructor, 'darthjee/core_ext/hash/deep_hash_constructor'
10
- autoload :KeyChanger, 'darthjee/core_ext/hash/key_changer'
11
- autoload :KeysSorter, 'darthjee/core_ext/hash/keys_sorter'
12
- autoload :Squasher, 'darthjee/core_ext/hash/squasher'
13
- autoload :ValueChanger, 'darthjee/core_ext/hash/value_changer'
14
- autoload :ToHashMapper, 'darthjee/core_ext/hash/to_hash_mapper'
7
+ module Darthjee
8
+ module CoreExt
9
+ module Hash
10
+ autoload :ChainFetcher, "#{PATH}/hash/chain_fetcher"
11
+ autoload :DeepHashConstructor, "#{PATH}/hash/deep_hash_constructor"
12
+ autoload :KeyChanger, "#{PATH}/hash/key_changer"
13
+ autoload :KeysSorter, "#{PATH}/hash/keys_sorter"
14
+ autoload :Squasher, "#{PATH}/hash/squasher"
15
+ autoload :ValueChanger, "#{PATH}/hash/value_changer"
16
+ autoload :ToHashMapper, "#{PATH}/hash/to_hash_mapper"
15
17
 
16
- include Hash::KeyChangeable
17
- include Hash::Transposeable
18
- include Hash::Transformable
18
+ include Hash::KeyChangeable
19
+ include Hash::Transposeable
20
+ include Hash::Transformable
19
21
 
20
- ########################################
21
- # Fetching methods
22
- #########################################
22
+ ########################################
23
+ # Fetching methods
24
+ #########################################
23
25
 
24
- def chain_fetch(*keys, &block)
25
- ChainFetcher.new(self, *keys, &block).fetch
26
+ def chain_fetch(*keys, &block)
27
+ ::Hash::ChainFetcher.new(self, *keys, &block).fetch
28
+ end
29
+ end
26
30
  end
27
31
  end
32
+
33
+ class Hash
34
+ include Darthjee::CoreExt::Hash
35
+ end
@@ -1,33 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Hash
4
- class ChainFetcher
5
- def initialize(hash, *keys, &block)
6
- @hash = hash
7
- @keys = keys
8
- @block = block
9
- end
3
+ module Darthjee
4
+ module CoreExt
5
+ module Hash
6
+ class ChainFetcher
7
+ def initialize(hash, *keys, &block)
8
+ @hash = hash
9
+ @keys = keys
10
+ @block = block
11
+ end
10
12
 
11
- def fetch
12
- block.present? ? fetch_with_block : fetch_without_block
13
- end
13
+ def fetch
14
+ block.present? ? fetch_with_block : fetch_without_block
15
+ end
14
16
 
15
- private
17
+ private
16
18
 
17
- attr_reader :hash, :keys, :block
19
+ attr_reader :hash, :keys, :block
18
20
 
19
- def fetch_with_block
20
- @hash = hash.fetch(keys.shift) do |*args|
21
- missed_keys = keys
22
- @keys = []
23
- block.call(*(args + [missed_keys]))
24
- end until keys.empty?
25
- hash
26
- end
21
+ def fetch_with_block
22
+ @hash = hash.fetch(keys.shift) do |*args|
23
+ missed_keys = keys
24
+ @keys = []
25
+ block.call(*(args + [missed_keys]))
26
+ end until keys.empty?
27
+ hash
28
+ end
27
29
 
28
- def fetch_without_block
29
- @hash = hash.fetch(keys.shift) until keys.empty?
30
- hash
30
+ def fetch_without_block
31
+ @hash = hash.fetch(keys.shift) until keys.empty?
32
+ hash
33
+ end
34
+ end
31
35
  end
32
36
  end
33
37
  end
@@ -1,84 +1,93 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Hash
4
- class DeepHashConstructor
5
- attr_accessor :separator
3
+ module Darthjee
4
+ module CoreExt
5
+ module Hash
6
+ class DeepHashConstructor
7
+ attr_accessor :separator
6
8
 
7
- def initialize(separator)
8
- @separator = separator
9
- end
9
+ def initialize(separator)
10
+ @separator = separator
11
+ end
10
12
 
11
- def deep_hash(object)
12
- if object.is_a? Array
13
- array_deep_hash(object)
14
- elsif object.is_a? Hash
15
- hash_deep_hash(object)
16
- else
17
- object
18
- end
19
- end
13
+ def deep_hash(object)
14
+ if object.is_a? Array
15
+ array_deep_hash(object)
16
+ elsif object.is_a? Hash
17
+ hash_deep_hash(object)
18
+ else
19
+ object
20
+ end
21
+ end
20
22
 
21
- private
23
+ private
22
24
 
23
- def array_deep_hash(array)
24
- array.map { |v| v.is_a?(Hash) ? deep_hash(v) : v }
25
- end
26
-
27
- def hash_deep_hash(hash)
28
- {}.tap do |new_hash|
29
- hash.each do |k, v|
30
- base_key, child_key = split_key(k, separator)
31
- set_deep_hash_positioned_value(new_hash, base_key, v, child_key)
25
+ def array_deep_hash(array)
26
+ array.map do |value|
27
+ value.is_a?(Hash) ? deep_hash(value) : value
28
+ end
32
29
  end
33
30
 
34
- new_hash.each do |k, v|
35
- new_hash[k] = deep_hash(v)
31
+ def hash_deep_hash(hash)
32
+ {}.tap do |new_hash|
33
+ hash.each do |k, v|
34
+ base_key, child_key = split_key(k, separator)
35
+ set_deep_hash_positioned_value(new_hash, base_key, v, child_key)
36
+ end
37
+
38
+ new_hash.each do |k, v|
39
+ new_hash[k] = deep_hash(v)
40
+ end
41
+ end
36
42
  end
37
- end
38
- end
39
43
 
40
- def split_key(key, separator)
41
- separator_rxp = separator == '.' ? "\\#{separator}" : separator
42
- skipper = "[^#{separator}]"
43
- regexp = Regexp.new("^(#{skipper}*)#{separator_rxp}(.*)")
44
- match = key.match(regexp)
44
+ def split_key(key, separator)
45
+ separator_rxp = separator == '.' ? "\\#{separator}" : separator
46
+ skipper = "[^#{separator}]"
47
+ regexp = Regexp.new("^(#{skipper}*)#{separator_rxp}(.*)")
48
+ match = key.match(regexp)
45
49
 
46
- match ? match[1..2] : key
47
- end
50
+ match ? match[1..2] : key
51
+ end
48
52
 
49
- def set_deep_hash_array_value(hash, base_key, index, value, key = nil)
50
- key_without_index = base_key.gsub("[#{index}]", '')
51
- hash[key_without_index] ||= []
53
+ def set_deep_hash_array_value(hash, base_key, index, value, key = nil)
54
+ key_without_index = base_key.gsub("[#{index}]", '')
55
+ hash[key_without_index] ||= []
52
56
 
53
- if key.nil?
54
- hash[key_without_index][index] = value
55
- else
56
- hash[key_without_index][index] ||= {}
57
- hash[key_without_index][index][key] = value
58
- end
59
- end
57
+ if key.nil?
58
+ hash[key_without_index][index] = value
59
+ else
60
+ hash[key_without_index][index] ||= {}
61
+ hash[key_without_index][index][key] = value
62
+ end
63
+ end
60
64
 
61
- def set_deep_hash_positioned_value(new_hash, base_key, value, child_key)
62
- index = array_index(base_key)
65
+ def set_deep_hash_positioned_value(new_hash, base_key, value, child_key)
66
+ index = array_index(base_key)
63
67
 
64
- if index
65
- set_deep_hash_array_value(new_hash, base_key, index, value, child_key)
66
- else
67
- set_deep_hash_value(new_hash, base_key, value, child_key)
68
- end
69
- end
68
+ if index
69
+ set_deep_hash_array_value(
70
+ new_hash, base_key, index,
71
+ value, child_key
72
+ )
73
+ else
74
+ set_deep_hash_value(new_hash, base_key, value, child_key)
75
+ end
76
+ end
70
77
 
71
- def array_index(key)
72
- match = key.match(/\[([^)]+)\]/)
73
- match && match[1].to_i
74
- end
78
+ def array_index(key)
79
+ match = key.match(/\[([^)]+)\]/)
80
+ match && match[1].to_i
81
+ end
75
82
 
76
- def set_deep_hash_value(hash, base_key, value, key = nil)
77
- if key.nil?
78
- hash[base_key] = value
79
- else
80
- hash[base_key] ||= {}
81
- hash[base_key][key] = value
83
+ def set_deep_hash_value(hash, base_key, value, key = nil)
84
+ if key.nil?
85
+ hash[base_key] = value
86
+ else
87
+ hash[base_key] ||= {}
88
+ hash[base_key][key] = value
89
+ end
90
+ end
82
91
  end
83
92
  end
84
93
  end