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.
- checksums.yaml +4 -4
- data/core_ext.gemspec +1 -1
- data/lib/darthjee/core_ext.rb +10 -6
- data/lib/darthjee/core_ext/array.rb +42 -34
- data/lib/darthjee/core_ext/array/hash_builder.rb +22 -17
- data/lib/darthjee/core_ext/class.rb +23 -0
- data/lib/darthjee/core_ext/date.rb +11 -3
- data/lib/darthjee/core_ext/hash.rb +24 -16
- data/lib/darthjee/core_ext/hash/chain_fetcher.rb +27 -23
- data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +73 -64
- data/lib/darthjee/core_ext/hash/key_changeable.rb +137 -133
- data/lib/darthjee/core_ext/hash/key_changer.rb +84 -80
- data/lib/darthjee/core_ext/hash/keys_sorter.rb +25 -21
- data/lib/darthjee/core_ext/hash/squasher.rb +24 -20
- data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +17 -13
- data/lib/darthjee/core_ext/hash/transformable.rb +20 -16
- data/lib/darthjee/core_ext/hash/transposeable.rb +18 -14
- data/lib/darthjee/core_ext/hash/value_changer.rb +52 -48
- data/lib/darthjee/core_ext/numeric.rb +12 -4
- data/lib/darthjee/core_ext/object.rb +12 -6
- data/lib/darthjee/core_ext/symbol.rb +14 -6
- data/lib/darthjee/core_ext/version.rb +1 -1
- data/spec/lib/{object/default_value_spe.rb → class_spec.rb} +3 -3
- data/spec/lib/{hash → darthjee/core_ext/hash}/chain_fetcher_spec.rb +1 -1
- data/spec/lib/{hash → darthjee/core_ext/hash}/deep_hash_constructor_spec.rb +1 -1
- data/spec/lib/{hash → darthjee/core_ext/hash}/key_changer_spec.rb +1 -1
- data/spec/lib/{hash → darthjee/core_ext/hash}/keys_sorter_spec.rb +1 -1
- data/spec/lib/{hash → darthjee/core_ext/hash}/squasher_spec.rb +1 -1
- data/spec/lib/{hash → darthjee/core_ext/hash}/to_hash_mapper_spec.rb +1 -1
- data/spec/support/models/{default_value.rb → default_value_model.rb} +1 -1
- metadata +23 -23
- 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd01e58fe4568b6b2c651415bb7677f78265fee8
|
4
|
+
data.tar.gz: 21b3f51a75beeffad0404ce44696bf57ebe43bf4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0df37f38b68eae72c79633ab6e31a0e733591dc63164530b34dc326725a47cb158439813f567e3a7ecb5967ae0b245200001312f905cc4a86a5b067cf3f9e5d
|
7
|
+
data.tar.gz: 4c4955a0d35d16e0b7ccb1aebedecc8635a6534a58bafb665eb1f827a63e11c1a65809f16a390877cc6f684b08437a702ba87d63e0d2b2e740f548279a6789ea
|
data/core_ext.gemspec
CHANGED
@@ -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'
|
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'
|
data/lib/darthjee/core_ext.rb
CHANGED
@@ -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/
|
5
|
+
require 'darthjee/core_ext/class'
|
9
6
|
require 'darthjee/core_ext/date'
|
10
|
-
require 'darthjee/core_ext/
|
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
|
-
|
4
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
19
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
37
|
+
def as_hash(keys)
|
38
|
+
Array::HashBuilder.new(self, keys).build
|
39
|
+
end
|
34
40
|
|
35
|
-
|
36
|
-
|
37
|
-
|
41
|
+
def random
|
42
|
+
self[rand(size)]
|
43
|
+
end
|
38
44
|
|
39
|
-
|
40
|
-
|
45
|
+
def random!
|
46
|
+
slice!(rand(size))
|
47
|
+
end
|
48
|
+
end
|
41
49
|
end
|
50
|
+
end
|
42
51
|
|
43
|
-
|
44
|
-
|
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
|
-
|
4
|
-
|
5
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Array
|
6
|
+
class HashBuilder
|
7
|
+
attr_accessor :values, :keys
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def initialize(values, keys)
|
10
|
+
@values = values.dup
|
11
|
+
@keys = keys.dup
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
def build
|
15
|
+
fixes_sizes
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
+
::Hash[[keys, values].transpose]
|
18
|
+
end
|
17
19
|
|
18
|
-
|
20
|
+
private
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
def fixes_sizes
|
23
|
+
return unless needs_resizing?
|
24
|
+
values.concat ::Array.new(keys.size - values.size)
|
25
|
+
end
|
23
26
|
|
24
|
-
|
25
|
-
|
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
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
18
|
+
include Hash::KeyChangeable
|
19
|
+
include Hash::Transposeable
|
20
|
+
include Hash::Transformable
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
########################################
|
23
|
+
# Fetching methods
|
24
|
+
#########################################
|
23
25
|
|
24
|
-
|
25
|
-
|
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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
13
|
+
def fetch
|
14
|
+
block.present? ? fetch_with_block : fetch_without_block
|
15
|
+
end
|
14
16
|
|
15
|
-
|
17
|
+
private
|
16
18
|
|
17
|
-
|
19
|
+
attr_reader :hash, :keys, :block
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
4
|
-
|
5
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Hash
|
6
|
+
class DeepHashConstructor
|
7
|
+
attr_accessor :separator
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
def initialize(separator)
|
10
|
+
@separator = separator
|
11
|
+
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
23
|
+
private
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
35
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
47
|
-
|
50
|
+
match ? match[1..2] : key
|
51
|
+
end
|
48
52
|
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
62
|
-
|
65
|
+
def set_deep_hash_positioned_value(new_hash, base_key, value, child_key)
|
66
|
+
index = array_index(base_key)
|
63
67
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
78
|
+
def array_index(key)
|
79
|
+
match = key.match(/\[([^)]+)\]/)
|
80
|
+
match && match[1].to_i
|
81
|
+
end
|
75
82
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|