gorilla_patch 2.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f92aeb3963b9338d0e209fbc2711e72cbd1539f6673fee3b225b1c88c1b84cca
4
+ data.tar.gz: 1b6e53f0a1534575a7b19ff5e29c057f8f7aebfb0c8b64f562acace5bfc980a9
5
+ SHA512:
6
+ metadata.gz: f924dd8b2762aea63511302906290fda9a597296a918ee30ae01341e68369a11d9766056944b0ba9bd87f98d3fbc755c7ccf1d8b729644a5a432d18ecd8154bb
7
+ data.tar.gz: c7aa80a28a8406bd9b845200a0f367106579ee88778df7896d76ecd3fc7984aa6c995c9be0f836cbf985e46e3d823fbc8d30553af555d89de1852393ea893a7b
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir.glob(
4
+ File.join(__dir__, 'gorilla_patch', '*.rb')
5
+ ).each { |file| require file }
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'deep_dup'
4
+
5
+ module GorillaPatch
6
+ ## Adding blank methods
7
+ module Blank
8
+ refine String do
9
+ def blank?
10
+ strip.empty?
11
+ end
12
+ end
13
+
14
+ refine NilClass do
15
+ def blank?
16
+ true
17
+ end
18
+ end
19
+
20
+ # rubocop:disable Metrics/BlockLength
21
+ REFINED_ENUMERABLES = [Array, Hash].freeze
22
+
23
+ REFINED_ENUMERABLES.each do |klass|
24
+ refine klass do
25
+ def reject_blank_strings!
26
+ replace reject_blank_strings
27
+ end
28
+
29
+ def nilify_blank_strings
30
+ result = map do |element|
31
+ if element.is_a?(String) && element.blank?
32
+ nil
33
+ elsif REFINED_ENUMERABLES.any? { |klass| element.is_a?(klass) }
34
+ element.nilify_blank_strings
35
+ else
36
+ element
37
+ end
38
+ end
39
+ is_a?(Hash) ? result.to_h : result
40
+ end
41
+
42
+ def nilify_blank_strings!
43
+ replace nilify_blank_strings
44
+ end
45
+
46
+ private
47
+
48
+ def deep_value_empty?(value)
49
+ deep_reject_blank_strings_in(value).empty?
50
+ rescue NoMethodError
51
+ false
52
+ end
53
+
54
+ def deep_reject_blank_strings_in(object)
55
+ case object
56
+ when String
57
+ object.strip
58
+ when *REFINED_ENUMERABLES
59
+ object.reject_blank_strings!
60
+ else
61
+ object
62
+ end
63
+ end
64
+ end
65
+ end
66
+ # rubocop:enable Metrics/BlockLength
67
+
68
+ refine Array do
69
+ using GorillaPatch::DeepDup
70
+
71
+ def reject_blank_strings
72
+ deep_dup.reject do |value|
73
+ deep_value_empty? value
74
+ end
75
+ end
76
+ end
77
+
78
+ refine Hash do
79
+ using GorillaPatch::DeepDup
80
+
81
+ def reject_blank_strings
82
+ deep_dup.reject do |_key, value|
83
+ deep_value_empty? value
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding compact methods
5
+ module Compact
6
+ refine Hash do
7
+ def compact
8
+ select { |_key, value| !value.nil? }
9
+ end
10
+
11
+ def compact!
12
+ delete_if { |_key, value| value.nil? }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding include methods
5
+ module Cover
6
+ refine Range do
7
+ def cover?(value)
8
+ return super unless value.is_a? Range
9
+ super(value.first) && super(value.last)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding deep_dup method
5
+ module DeepDup
6
+ refine Object do
7
+ def deep_dup
8
+ dup
9
+ rescue TypeError
10
+ # :nocov:
11
+ # For Ruby < 2.4: https://bugs.ruby-lang.org/issues/12979
12
+ self
13
+ # :nocov:
14
+ end
15
+ end
16
+
17
+ refine Hash do
18
+ def deep_dup
19
+ each_with_object(dup) do |(key, value), hash|
20
+ hash[key.deep_dup] = value.deep_dup
21
+ end
22
+ end
23
+ end
24
+
25
+ refine Array do
26
+ def deep_dup
27
+ map { |el| el.deep_dup }
28
+ end
29
+ end
30
+
31
+ require 'delegate'
32
+
33
+ refine Delegator do
34
+ def deep_dup
35
+ dup
36
+ rescue NoMethodError
37
+ self
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding deep_merge method
5
+ module DeepMerge
6
+ refine Hash do
7
+ def deep_merge!(other_hash, &block)
8
+ other_hash.each do |other_key, other_value|
9
+ self_value = self[other_key]
10
+
11
+ deep_value_merge!(other_key, self_value, other_value, &block)
12
+ end
13
+
14
+ self
15
+ end
16
+
17
+ def deep_merge(other_hash, &block)
18
+ dup.deep_merge!(other_hash, &block)
19
+ end
20
+
21
+ private
22
+
23
+ def deep_value_merge!(other_key, self_value, other_value, &block)
24
+ self[other_key] =
25
+ if self_value.is_a?(Hash) && other_value.is_a?(Hash)
26
+ self_value.deep_merge!(other_value, &block)
27
+ elsif block_given? && key?(other_key)
28
+ yield(other_key, self_value, other_value)
29
+ else
30
+ other_value
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Allows dig with 0 arguments
5
+ module DigEmpty
6
+ refine Hash do
7
+ def dig(*keys)
8
+ keys.any? ? super : self
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding except methods
5
+ module Except
6
+ refine Hash do
7
+ def except(*keys)
8
+ dup.except!(*keys)
9
+ end
10
+
11
+ def except!(*keys)
12
+ keys.each { |key| delete(key) }
13
+ self
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Inflections
5
+ module Inflections
6
+ def self.acronyms
7
+ @acronyms ||= %w[API HTML XML JSON SSL IP HTTP HTTPS]
8
+ end
9
+
10
+ refine String do
11
+ def underscore
12
+ gsub(/::/, '/')
13
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
14
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
15
+ .tr('-', '_')
16
+ .downcase
17
+ end
18
+
19
+ def camelize
20
+ acronyms = GorillaPatch::Inflections.acronyms
21
+ split('/')
22
+ .map do |s|
23
+ s.split(/([[:upper:]][[:lower:]]*)|_|-/).collect do |part|
24
+ upcased_part = part.upcase
25
+ acronyms.include?(upcased_part) ? upcased_part : part.capitalize
26
+ end.join
27
+ end
28
+ .join('::')
29
+ end
30
+ end
31
+
32
+ refine Module do
33
+ def underscore
34
+ to_s.underscore
35
+ end
36
+ end
37
+
38
+ def self.from_sequel
39
+ @from_sequel ||= Module.new do
40
+ require 'sequel'
41
+
42
+ refine String do
43
+ Sequel::Inflections.private_instance_methods.each do |method|
44
+ define_method method do
45
+ Sequel::Inflections.instance_method(method).bind(self).call(self)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def self.from_inflecto
53
+ require 'inflecto-refinements'
54
+
55
+ Inflecto::Refinements
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding keys methods
5
+ module Keys
6
+ refine Hash do
7
+ def keys?(*keys)
8
+ keys.all? { |k| key?(k) }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding namespaces-changing methods
5
+ module Namespace
6
+ refine String do
7
+ def demodulize
8
+ split('::').last.to_s
9
+ end
10
+
11
+ def deconstantize
12
+ self[0, rindex('::') || 0]
13
+ end
14
+ end
15
+
16
+ [Module, Class].each do |klass|
17
+ refine klass do
18
+ def demodulize
19
+ name.to_s.demodulize
20
+ end
21
+
22
+ def deconstantize
23
+ name.to_s.deconstantize
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding slice methods
5
+ module Slice
6
+ refine Hash do
7
+ def slice(*keys, nils: false)
8
+ return super if defined?(super) && !nils
9
+ keys.each_with_object(self.class.new) do |k, hash|
10
+ hash[k] = self[k] if nils || key?(k)
11
+ end
12
+ end
13
+
14
+ def slice!(*keys, nils: false)
15
+ omit = slice(*self.keys - keys, nils: nils)
16
+ hash = slice(*keys, nils: nils)
17
+ replace(hash)
18
+ omit
19
+ end
20
+
21
+ def slice_reverse!(*keys, nils: false)
22
+ omit = slice!(*keys, nils: nils)
23
+ hash = dup
24
+ replace(omit)
25
+ hash
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Adding symbolize methods
5
+ module Symbolize
6
+ refine Hash do
7
+ def symbolize_keys(deep: false)
8
+ each_with_object({}) do |(key, value), hash|
9
+ value = deep_symbolize_keys_in(value) if deep
10
+ key = key.to_sym if key.respond_to?(:to_sym)
11
+ hash[key] = value
12
+ end
13
+ end
14
+
15
+ def symbolize_keys!(deep: false)
16
+ replace symbolize_keys(deep: deep)
17
+ end
18
+
19
+ private
20
+
21
+ def deep_symbolize_keys_in(object)
22
+ case object
23
+ when Hash
24
+ object.symbolize_keys(deep: true)
25
+ when Array
26
+ object.map { |el| deep_symbolize_keys_in(el) }
27
+ else
28
+ object
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Module for transformations methods
5
+ module Transform
6
+ refine Hash do
7
+ def transform_values
8
+ return super if defined? super
9
+ each_with_object(self.class.new) do |(key, value), result|
10
+ result[key] = yield value
11
+ end
12
+ end
13
+
14
+ def transform_values!
15
+ return super if defined? super
16
+ each do |key, value|
17
+ self[key] = yield value
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GorillaPatch
4
+ ## Module for truncating
5
+ module Truncate
6
+ refine String do
7
+ def truncate(position, separator: '', omission: '...')
8
+ return dup if length <= position
9
+ stop = position - omission.length
10
+ if separator.is_a?(Regexp) || !separator.empty?
11
+ stop = rindex(separator, stop)
12
+ end
13
+ "#{self[0, stop]}#{omission}"
14
+ end
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gorilla_patch
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Popov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: codecov
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.52'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.52'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: inflecto-refinements
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sequel
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5'
111
+ description: Refine Ruby Core classes with methods like Active Support.
112
+ email: alex.wayfer@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - lib/gorilla_patch.rb
118
+ - lib/gorilla_patch/blank.rb
119
+ - lib/gorilla_patch/compact.rb
120
+ - lib/gorilla_patch/cover.rb
121
+ - lib/gorilla_patch/deep_dup.rb
122
+ - lib/gorilla_patch/deep_merge.rb
123
+ - lib/gorilla_patch/dig_empty.rb
124
+ - lib/gorilla_patch/except.rb
125
+ - lib/gorilla_patch/inflections.rb
126
+ - lib/gorilla_patch/keys.rb
127
+ - lib/gorilla_patch/namespace.rb
128
+ - lib/gorilla_patch/slice.rb
129
+ - lib/gorilla_patch/symbolize.rb
130
+ - lib/gorilla_patch/transform.rb
131
+ - lib/gorilla_patch/truncate.rb
132
+ homepage: https://github.com/AlexWayfer/gorilla_patch
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 2.1.0
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.7.6
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Refining core classes
156
+ test_files: []