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
@@ -1,30 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
module
|
5
|
-
|
6
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Hash
|
6
|
+
module Squasher
|
7
|
+
class Builder
|
8
|
+
attr_reader :key, :value
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def initialize(key, value)
|
11
|
+
@value = value
|
12
|
+
@key = key
|
13
|
+
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
def to_h
|
16
|
+
if value.is_a? Hash
|
17
|
+
value.squash.inject({}) do |hash, (k, v)|
|
18
|
+
new_key = [key, k].join('.')
|
19
|
+
hash.merge!(new_key => v)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
{ key => value }
|
23
|
+
end
|
18
24
|
end
|
19
|
-
else
|
20
|
-
{ key => value }
|
21
25
|
end
|
22
|
-
end
|
23
|
-
end
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
def self.squash(origin)
|
28
|
+
origin.inject({}) do |hash, (key, value)|
|
29
|
+
hash.merge!(Builder.new(key, value).to_h)
|
30
|
+
end
|
31
|
+
end
|
28
32
|
end
|
29
33
|
end
|
30
34
|
end
|
@@ -1,21 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Hash
|
6
|
+
class ToHashMapper
|
7
|
+
def initialize(hash)
|
8
|
+
@hash = hash
|
9
|
+
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
def map
|
12
|
+
{}.tap do |new_hash|
|
13
|
+
hash.each do |k, v|
|
14
|
+
new_hash[k] = yield(k, v)
|
15
|
+
end
|
16
|
+
end
|
13
17
|
end
|
14
|
-
end
|
15
|
-
end
|
16
18
|
|
17
|
-
|
19
|
+
private
|
18
20
|
|
19
|
-
|
21
|
+
attr_reader :hash
|
22
|
+
end
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
@@ -1,25 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Hash
|
6
|
+
module Transformable
|
7
|
+
def squash
|
8
|
+
Hash::Squasher.squash(self)
|
9
|
+
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
def to_deep_hash(separator = '.')
|
12
|
+
Hash::DeepHashConstructor.new(separator).deep_hash(self)
|
13
|
+
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
15
|
+
def map_to_hash(&block)
|
16
|
+
Hash::ToHashMapper.new(self).map(&block)
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
19
|
+
def exclusive_merge(hash)
|
20
|
+
dup.exclusive_merge!(hash)
|
21
|
+
end
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
+
def exclusive_merge!(hash)
|
24
|
+
merge!(hash.slice(*keys))
|
25
|
+
end
|
26
|
+
end
|
23
27
|
end
|
24
28
|
end
|
25
29
|
end
|
@@ -1,20 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Hash
|
6
|
+
module Transposeable
|
7
|
+
def transpose!
|
8
|
+
aux = dup
|
9
|
+
keys.each { |k| delete(k) }
|
10
|
+
aux.each do |k, v|
|
11
|
+
self[v] = k
|
12
|
+
end
|
13
|
+
self
|
14
|
+
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def transpose
|
17
|
+
{}.tap do |new_hash|
|
18
|
+
each do |k, v|
|
19
|
+
new_hash[v] = k
|
20
|
+
end
|
21
|
+
end
|
18
22
|
end
|
19
23
|
end
|
20
24
|
end
|
@@ -1,66 +1,70 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Hash
|
6
|
+
class ValueChanger
|
7
|
+
attr_accessor :options, :block
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
def initialize(options, &block)
|
10
|
+
@options = {
|
11
|
+
recursive: true,
|
12
|
+
skip_inner: true
|
13
|
+
}.merge(options)
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
+
@block = block
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
def change(object)
|
19
|
+
if object.respond_to?(:change_values)
|
20
|
+
change_hash(object)
|
21
|
+
elsif iterable?(object)
|
22
|
+
change_array(object)
|
23
|
+
end
|
24
|
+
end
|
23
25
|
|
24
|
-
|
26
|
+
private
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
def change_hash(original_hash)
|
29
|
+
original_hash.tap do |hash|
|
30
|
+
original_hash.each do |key, value|
|
31
|
+
value = new_value(value)
|
32
|
+
hash[key] = value
|
33
|
+
end
|
34
|
+
end
|
31
35
|
end
|
32
|
-
end
|
33
|
-
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
+
def change_array(array)
|
38
|
+
method = %w[map! map].find { |m| array.respond_to? m }
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
array.public_send(method) do |value|
|
41
|
+
if value.respond_to?(:change_values)
|
42
|
+
value.change_values(options, &block)
|
43
|
+
elsif iterable?(value)
|
44
|
+
change_array(value)
|
45
|
+
else
|
46
|
+
new_value(value)
|
47
|
+
end
|
48
|
+
end
|
45
49
|
end
|
46
|
-
end
|
47
|
-
end
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
51
|
+
def change_value?(value)
|
52
|
+
!iterable?(value) || !options[:skip_inner]
|
53
|
+
end
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
-
|
55
|
+
def iterable?(value)
|
56
|
+
value.respond_to?(:each)
|
57
|
+
end
|
56
58
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
def new_value(value)
|
60
|
+
value = block.call(value) if change_value?(value)
|
61
|
+
apply_recursion?(value) ? change(value) : value
|
62
|
+
end
|
61
63
|
|
62
|
-
|
63
|
-
|
64
|
+
def apply_recursion?(value)
|
65
|
+
iterable?(value) && options[:recursive]
|
66
|
+
end
|
67
|
+
end
|
64
68
|
end
|
65
69
|
end
|
66
70
|
end
|
@@ -1,8 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Numeric
|
6
|
+
def percent_of(total)
|
7
|
+
return Float::INFINITY if total&.zero?
|
8
|
+
(to_f / total.to_f) * 100.0
|
9
|
+
end
|
10
|
+
end
|
7
11
|
end
|
8
12
|
end
|
13
|
+
|
14
|
+
class Numeric
|
15
|
+
include Darthjee::CoreExt::Numeric
|
16
|
+
end
|
@@ -1,11 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Object
|
6
|
+
def is_any?(*classes)
|
7
|
+
classes.any? do |clazz|
|
8
|
+
is_a?(clazz)
|
9
|
+
end
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
11
13
|
end
|
14
|
+
|
15
|
+
class Object
|
16
|
+
include Darthjee::CoreExt::Object
|
17
|
+
end
|
@@ -1,11 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Symbol
|
6
|
+
def camelize(type = :upper)
|
7
|
+
to_s.camelize(type).to_sym
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
+
def underscore
|
11
|
+
to_s.underscore.to_sym
|
12
|
+
end
|
13
|
+
end
|
10
14
|
end
|
11
15
|
end
|
16
|
+
|
17
|
+
class Symbol
|
18
|
+
include Darthjee::CoreExt::Symbol
|
19
|
+
end
|
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe Class do
|
6
6
|
describe '.default_value' do
|
7
|
-
subject {
|
7
|
+
subject { DefaultValueModel.new }
|
8
8
|
|
9
9
|
it 'accepts default value' do
|
10
10
|
expect(subject.x).to eq(10)
|
@@ -16,7 +16,7 @@ describe Object do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe '.default_values' do
|
19
|
-
subject {
|
19
|
+
subject { DefaultValueModel.new }
|
20
20
|
|
21
21
|
it 'accepts default values' do
|
22
22
|
expect(subject.y).to eq(20)
|