gorilla-patch 0.1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1799b8afd962ff6a0c728d82a7a5c4ed2e3c8d8d
4
- data.tar.gz: 9c81b76b45d0f9654037e5eba32a25bb4d523177
3
+ metadata.gz: 41bbd444208009b4f1b89499a330b887f51f9975
4
+ data.tar.gz: acc4944fd3a50ddd5f7580bb293c66e12519fd39
5
5
  SHA512:
6
- metadata.gz: c84bf4c05d3dc0c416691c77ce43c9f67e113feb7feb3dac6ef07db01582c5b993df117d342a147add1d23ff80db3159126d5b7b0e7572909f1dad765376430c
7
- data.tar.gz: 923fe369a7e965ee26d5b0c9aa9866f5315366f1a09c972e379224966bd0b1be6f9cb4678249da58142d04695d5c86c2804e953c2efd65ec60a11516869042a7
6
+ metadata.gz: 8c7e5ad9bd2f46fa1d6d9478c6c04b5fe478732b95c55187ca698146d450fcaf7dda644e9d954fe6ea00358ccc227c04a2d751ab94a4e8101f433b7fea58b76e
7
+ data.tar.gz: 6e5cb28431fbb419483e257491d72e81759f54fbaa38da7d9558bf1e61f61f30ab4fd9ec98f436c4fc6876b39ac24e34259162b12c0e088726f2ba8847c6c9ce
@@ -0,0 +1,59 @@
1
+ require_relative 'deep_dup'
2
+
3
+ module GorillaPatch
4
+ ## Adding blank methods
5
+ module Blank
6
+ refine String do
7
+ def blank?
8
+ strip.empty?
9
+ end
10
+ end
11
+
12
+ [Array, Hash].each do |klass|
13
+ refine klass do
14
+ def reject_blank_strings!
15
+ replace reject_blank_strings
16
+ end
17
+
18
+ private
19
+
20
+ def deep_value_empty?(value)
21
+ deep_reject_blank_strings_in(value).empty?
22
+ rescue NoMethodError
23
+ false
24
+ end
25
+
26
+ def deep_reject_blank_strings_in(object)
27
+ case object
28
+ when String
29
+ object.strip
30
+ when Hash, Array
31
+ object.reject_blank_strings!
32
+ else
33
+ object
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ refine Array do
40
+ using GorillaPatch::DeepDup
41
+
42
+ def reject_blank_strings
43
+ deep_dup.reject do |value|
44
+ deep_value_empty? value
45
+ end
46
+ end
47
+ end
48
+
49
+ refine Hash do
50
+ using GorillaPatch::DeepDup
51
+
52
+ def reject_blank_strings
53
+ deep_dup.reject do |_key, value|
54
+ deep_value_empty? value
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,14 @@
1
+ module GorillaPatch
2
+ ## Adding compact methods
3
+ module Compact
4
+ refine Hash do
5
+ def compact
6
+ select { |_key, value| !value.nil? }
7
+ end
8
+
9
+ def compact!
10
+ reject! { |_key, value| value.nil? }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ module GorillaPatch
2
+ ## Adding deep_dup method
3
+ module DeepDup
4
+ refine Object do
5
+ def deep_dup
6
+ dup
7
+ rescue TypeError
8
+ self
9
+ end
10
+ end
11
+
12
+ refine Hash do
13
+ def deep_dup
14
+ each_with_object(dup) do |(key, value), hash|
15
+ hash[key.deep_dup] = value.deep_dup
16
+ end
17
+ end
18
+ end
19
+
20
+ refine Array do
21
+ def deep_dup
22
+ map { |el| el.deep_dup }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ module GorillaPatch
2
+ ## Adding except methods
3
+ module Except
4
+ refine Hash do
5
+ def except(*keys)
6
+ dup.except!(*keys)
7
+ end
8
+
9
+ def except!(*keys)
10
+ keys.each { |key| delete(key) }
11
+ self
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,6 +1,6 @@
1
1
  module GorillaPatch
2
- ## Refine Array class
3
- module RangeExt
2
+ ## Adding include methods
3
+ module Include
4
4
  refine Range do
5
5
  def include?(value)
6
6
  return super unless value.is_a? Range
@@ -0,0 +1,10 @@
1
+ module GorillaPatch
2
+ ## Adding keys methods
3
+ module Keys
4
+ refine Hash do
5
+ def keys?(*keys)
6
+ keys.all? { |k| key?(k) }
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ module GorillaPatch
2
+ ## Adding case-changing methods
3
+ module LettersCase
4
+ def self.used(namespace)
5
+ puts "Using in #{namespace}!"
6
+ end
7
+
8
+ refine String do
9
+ def underscore
10
+ gsub(/::/, '/')
11
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
12
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
13
+ .tr('-', '_')
14
+ .downcase
15
+ end
16
+
17
+ def camelize
18
+ split('/')
19
+ .map { |s| s.split('_').collect(&:capitalize).join }
20
+ .join('::')
21
+ end
22
+ end
23
+
24
+ refine Module do
25
+ def underscore
26
+ to_s.underscore
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module GorillaPatch
2
+ ## Adding namespaces-changing methods
3
+ module Namespace
4
+ refine String do
5
+ def demodulize
6
+ split('::').last
7
+ end
8
+ end
9
+
10
+ refine Module do
11
+ def demodulize
12
+ to_s.demodulize
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module GorillaPatch
2
+ ## Refine String class by Sequel Inflections
3
+ module Quantity
4
+ def self.from_sequel
5
+ @from_sequel ||= Module.new do
6
+ require 'sequel'
7
+
8
+ refine String do
9
+ Sequel::Inflections.private_instance_methods.each do |method|
10
+ define_method method do
11
+ Sequel::Inflections.instance_method(method).bind(self).call(self)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ module GorillaPatch
2
+ ## Adding slice methods
3
+ module Slice
4
+ refine Hash do
5
+ def slice(*keys)
6
+ keys.each_with_object({}) { |k, hash| hash[k] = self[k] if key?(k) }
7
+ end
8
+
9
+ def slice!(*keys)
10
+ omit = slice(*self.keys - keys)
11
+ hash = slice(*keys)
12
+ replace(hash)
13
+ omit
14
+ end
15
+
16
+ def slice_reverse!(*keys)
17
+ omit = slice!(*keys)
18
+ hash = dup
19
+ replace(omit)
20
+ hash
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ module GorillaPatch
2
+ ## Adding symbolize methods
3
+ module Symbolize
4
+ refine Hash do
5
+ def symbolize_keys(deep: false)
6
+ each_with_object({}) do |(key, value), hash|
7
+ value = deep_symbolize_keys_in(value) if deep
8
+ key = key.to_sym if key.respond_to?(:to_sym)
9
+ hash[key] = value
10
+ end
11
+ end
12
+
13
+ def symbolize_keys!(deep: false)
14
+ replace symbolize_keys(deep: deep)
15
+ end
16
+
17
+ private
18
+
19
+ def deep_symbolize_keys_in(object)
20
+ case object
21
+ when Hash
22
+ object.symbolize_keys(deep: true)
23
+ when Array
24
+ object.map { |el| deep_symbolize_keys_in(el) }
25
+ else
26
+ object
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gorilla-patch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Popov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-02 00:00:00.000000000 Z
11
+ date: 2016-11-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Refine Ruby Core classes.
14
14
  email: alex.wayfer@gmail.com
@@ -17,11 +17,17 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/gorilla-patch.rb
20
- - lib/gorilla-patch/array.rb
21
- - lib/gorilla-patch/hash.rb
22
- - lib/gorilla-patch/module.rb
23
- - lib/gorilla-patch/range.rb
24
- - lib/gorilla-patch/string.rb
20
+ - lib/gorilla-patch/blank.rb
21
+ - lib/gorilla-patch/compact.rb
22
+ - lib/gorilla-patch/deep_dup.rb
23
+ - lib/gorilla-patch/except.rb
24
+ - lib/gorilla-patch/include.rb
25
+ - lib/gorilla-patch/keys.rb
26
+ - lib/gorilla-patch/letters_case.rb
27
+ - lib/gorilla-patch/namespace.rb
28
+ - lib/gorilla-patch/quantity.rb
29
+ - lib/gorilla-patch/slice.rb
30
+ - lib/gorilla-patch/symbolize.rb
25
31
  homepage: https://github.com/AlexWayfer/gorilla-patch
26
32
  licenses:
27
33
  - MIT
@@ -1,16 +0,0 @@
1
- module GorillaPatch
2
- ## Refine Array class
3
- module ArrayExt
4
- refine Array do
5
- def compare(other)
6
- other = [other] unless other.is_a?(Array)
7
- sort == other.sort
8
- end
9
-
10
- def include_any?(*items)
11
- items = items.first if items.length == 1 && items.first.is_a?(Array)
12
- !(self & items).empty?
13
- end
14
- end
15
- end
16
- end
@@ -1,84 +0,0 @@
1
- module GorillaPatch
2
- ## Refine Hash class
3
- module HashExt
4
- refine Hash do
5
- def keys_to_sym(deep: false)
6
- each_with_object({}) do |(key, val), hash_sym|
7
- val = deep_symbolize_keys_in(val) if deep
8
- key = key.to_sym if key.respond_to?(:to_sym)
9
- hash_sym[key] = val
10
- end
11
- end
12
-
13
- def keys_to_sym!(deep: false)
14
- hash_sym = keys_to_sym(deep: deep)
15
- clear.merge! hash_sym
16
- end
17
-
18
- def keys_to_s(deep: false)
19
- each_with_object({}) do |(key, val), hash_s|
20
- val = val.keys_to_s(deep: deep) if deep && val.is_a?(Hash)
21
- key = key.to_s if key.respond_to?(:to_s)
22
- hash_s[key] = val
23
- end
24
- end
25
-
26
- def keys_to_s!(deep: false)
27
- hash_s = keys_to_s(deep: deep)
28
- clear.merge! hash_s
29
- end
30
-
31
- def except(*keys)
32
- dup.except!(*keys)
33
- end
34
-
35
- def except!(*keys)
36
- keys.each { |key| delete(key) }
37
- self
38
- end
39
-
40
- def compact
41
- select { |_key, value| !value.nil? }
42
- end
43
-
44
- def compact!
45
- reject! { |_key, value| value.nil? }
46
- end
47
-
48
- def slice(*keys)
49
- keys.each_with_object({}) { |k, hash| hash[k] = self[k] if key?(k) }
50
- end
51
-
52
- def slice!(*keys)
53
- omit = slice(*self.keys - keys)
54
- hash = slice(*keys)
55
- replace(hash)
56
- omit
57
- end
58
-
59
- def slice_reverse!(*keys)
60
- omit = slice!(*keys)
61
- hash = dup
62
- replace(omit)
63
- hash
64
- end
65
-
66
- def keys?(*keys)
67
- keys.all? { |k| key?(k) }
68
- end
69
-
70
- private
71
-
72
- def deep_symbolize_keys_in(object)
73
- case object
74
- when Hash
75
- object.keys_to_sym(deep: true)
76
- when Array
77
- object.map { |el| deep_symbolize_keys_in(el) }
78
- else
79
- object
80
- end
81
- end
82
- end
83
- end
84
- end
@@ -1,18 +0,0 @@
1
- require_relative './string'
2
-
3
- module GorillaPatch
4
- ## Refine Module class
5
- module ModuleExt
6
- refine Module do
7
- using StringExt
8
-
9
- def demodulize
10
- to_s.demodulize
11
- end
12
-
13
- def underscore
14
- to_s.underscore
15
- end
16
- end
17
- end
18
- end
@@ -1,38 +0,0 @@
1
- module GorillaPatch
2
- ## Refine String class
3
- module StringExt
4
- refine String do
5
- def underscore
6
- gsub(/::/, '/')
7
- .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
8
- .gsub(/([a-z\d])([A-Z])/, '\1_\2')
9
- .tr('-', '_')
10
- .downcase
11
- end
12
-
13
- def demodulize
14
- split('::').last
15
- end
16
-
17
- def camelize
18
- split('/')
19
- .map { |s| s.split('_').collect(&:capitalize).join }
20
- .join('::')
21
- end
22
- end
23
-
24
- def self.from_sequel
25
- @from_sequel ||= Module.new do
26
- refine String do
27
- require 'sequel'
28
-
29
- Sequel::Inflections.private_instance_methods.each do |method|
30
- define_method method do
31
- Sequel::Inflections.instance_method(method).bind(self).call(self)
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end