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,30 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Hash
4
- module Squasher
5
- class Builder
6
- attr_reader :key, :value
3
+ module Darthjee
4
+ module CoreExt
5
+ module Hash
6
+ module Squasher
7
+ class Builder
8
+ attr_reader :key, :value
7
9
 
8
- def initialize(key, value)
9
- @value = value
10
- @key = key
11
- end
10
+ def initialize(key, value)
11
+ @value = value
12
+ @key = key
13
+ end
12
14
 
13
- def to_h
14
- if value.is_a? Hash
15
- value.squash.inject({}) do |hash, (k, v)|
16
- new_key = [key, k].join('.')
17
- hash.merge!(new_key => v)
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
- def self.squash(origin)
26
- origin.inject({}) do |hash, (key, value)|
27
- hash.merge!(Builder.new(key, value).to_h)
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
- class Hash
4
- class ToHashMapper
5
- def initialize(hash)
6
- @hash = hash
7
- end
3
+ module Darthjee
4
+ module CoreExt
5
+ module Hash
6
+ class ToHashMapper
7
+ def initialize(hash)
8
+ @hash = hash
9
+ end
8
10
 
9
- def map
10
- {}.tap do |new_hash|
11
- hash.each do |k, v|
12
- new_hash[k] = yield(k, v)
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
- private
19
+ private
18
20
 
19
- attr_reader :hash
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
- class Hash
4
- module Transformable
5
- def squash
6
- Squasher.squash(self)
7
- end
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
- def to_deep_hash(separator = '.')
10
- Hash::DeepHashConstructor.new(separator).deep_hash(self)
11
- end
11
+ def to_deep_hash(separator = '.')
12
+ Hash::DeepHashConstructor.new(separator).deep_hash(self)
13
+ end
12
14
 
13
- def map_to_hash(&block)
14
- ToHashMapper.new(self).map(&block)
15
- end
15
+ def map_to_hash(&block)
16
+ Hash::ToHashMapper.new(self).map(&block)
17
+ end
16
18
 
17
- def exclusive_merge(hash)
18
- dup.exclusive_merge!(hash)
19
- end
19
+ def exclusive_merge(hash)
20
+ dup.exclusive_merge!(hash)
21
+ end
20
22
 
21
- def exclusive_merge!(hash)
22
- merge!(hash.slice(*keys))
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
- class Hash
4
- module Transposeable
5
- def transpose!
6
- aux = dup
7
- keys.each { |k| delete(k) }
8
- aux.each do |k, v|
9
- self[v] = k
10
- end
11
- self
12
- end
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
- def transpose
15
- {}.tap do |new_hash|
16
- each do |k, v|
17
- new_hash[v] = k
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
- class Hash
4
- class ValueChanger
5
- attr_accessor :options, :block
3
+ module Darthjee
4
+ module CoreExt
5
+ module Hash
6
+ class ValueChanger
7
+ attr_accessor :options, :block
6
8
 
7
- def initialize(options, &block)
8
- @options = {
9
- recursive: true,
10
- skip_inner: true
11
- }.merge(options)
9
+ def initialize(options, &block)
10
+ @options = {
11
+ recursive: true,
12
+ skip_inner: true
13
+ }.merge(options)
12
14
 
13
- @block = block
14
- end
15
+ @block = block
16
+ end
15
17
 
16
- def change(object)
17
- if object.respond_to?(:change_values)
18
- change_hash(object)
19
- elsif iterable?(object)
20
- change_array(object)
21
- end
22
- end
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
- private
26
+ private
25
27
 
26
- def change_hash(original_hash)
27
- original_hash.tap do |hash|
28
- original_hash.each do |key, value|
29
- value = new_value(value)
30
- hash[key] = value
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
- def change_array(array)
36
- method = %w[map! map].find { |m| array.respond_to? m }
37
+ def change_array(array)
38
+ method = %w[map! map].find { |m| array.respond_to? m }
37
39
 
38
- array.public_send(method) do |value|
39
- if value.respond_to?(:change_values)
40
- value.change_values(options, &block)
41
- elsif iterable?(value)
42
- change_array(value)
43
- else
44
- new_value(value)
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
- def change_value?(value)
50
- !iterable?(value) || !options[:skip_inner]
51
- end
51
+ def change_value?(value)
52
+ !iterable?(value) || !options[:skip_inner]
53
+ end
52
54
 
53
- def iterable?(value)
54
- value.respond_to?(:each)
55
- end
55
+ def iterable?(value)
56
+ value.respond_to?(:each)
57
+ end
56
58
 
57
- def new_value(value)
58
- value = block.call(value) if change_value?(value)
59
- apply_recursion?(value) ? change(value) : value
60
- end
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
- def apply_recursion?(value)
63
- iterable?(value) && options[:recursive]
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
- class Numeric
4
- def percent_of(total)
5
- return Float::INFINITY if total&.zero?
6
- (to_f / total.to_f) * 100.0
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
- require 'darthjee/core_ext/object/default_value'
4
-
5
- class Object
6
- def is_any?(*classes)
7
- classes.any? do |clazz|
8
- is_a?(clazz)
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
- class Symbol
4
- def camelize(type = :upper)
5
- to_s.camelize(type).to_sym
6
- end
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
- def underscore
9
- to_s.underscore.to_sym
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,6 +2,6 @@
2
2
 
3
3
  module Darthjee
4
4
  module CoreExt
5
- VERSION = '1.6.2'
5
+ VERSION = '1.7.0'
6
6
  end
7
7
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Object do
5
+ describe Class do
6
6
  describe '.default_value' do
7
- subject { DefaultValue.new }
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 { DefaultValue.new }
19
+ subject { DefaultValueModel.new }
20
20
 
21
21
  it 'accepts default values' do
22
22
  expect(subject.y).to eq(20)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Hash::ChainFetcher do
5
+ describe Darthjee::CoreExt::Hash::ChainFetcher do
6
6
  subject { described_class.new(hash, *keys, &block) }
7
7
 
8
8
  it_behaves_like 'an object with capable of performing chain fetch' do
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Hash::DeepHashConstructor do
5
+ describe Darthjee::CoreExt::Hash::DeepHashConstructor do
6
6
  let(:subject) { described_class.new('.') }
7
7
  let(:deep_hash) { subject.deep_hash(hash) }
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Hash::KeyChanger do
5
+ describe Darthjee::CoreExt::Hash::KeyChanger do
6
6
  let(:subject) { described_class.new(hash) }
7
7
 
8
8
  describe '#remap_keys!' do
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Hash::KeysSorter do
5
+ describe Darthjee::CoreExt::Hash::KeysSorter do
6
6
  it_behaves_like 'a class with a keys sort method' do
7
7
  subject { described_class.new(hash, **options) }
8
8
  let(:result) { subject.sort }
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Hash::Squasher do
5
+ describe Darthjee::CoreExt::Hash::Squasher do
6
6
  it_behaves_like 'a class that has a method to squash a hash' do
7
7
  let(:squashed) { described_class.squash(hash) }
8
8
  end