more_core_extensions 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/more_core_extensions/core_ext/array/deletes.rb +1 -2
- data/lib/more_core_extensions/core_ext/hash/deletes.rb +1 -2
- data/lib/more_core_extensions/core_ext/object/blank.rb +149 -0
- data/lib/more_core_extensions/core_ext/object.rb +1 -0
- data/lib/more_core_extensions/version.rb +1 -1
- data/spec/core_ext/hash/nested_spec.rb +38 -34
- data/spec/core_ext/object/blank_spec.rb +46 -0
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8d64c7762ab50f5ccf50df6ab0617196c5033ff
|
4
|
+
data.tar.gz: 6eddc491ea72be38afd73e62e1f81440c52622fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e31fdff37332985b3849f2047b5826988060bfcf53ee2fd34c934e3b2b0f3174a8a99321719eb2af6376b14dddd730d887352cbe5d20a4dbdbab80784a26c5eb
|
7
|
+
data.tar.gz: 4b54bf02dc41e38429d4b819981444e1a868708214dff14c14b9f0583b0aac533b90b26f6a46b35f92f75a2cd39e1c75ee9ca6d89fe6e57eda6590d740cbd783
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# Copied from https://github.com/rails/rails/blob/v5.0.0/activesupport/lib/active_support/core_ext/object/blank.rb
|
2
|
+
# With the release of ActiveSupport v5.0.0 a minimum required_ruby_version of '>= 2.2.2' was added.
|
3
|
+
# This code has been copied here so that we can continue to support ruby versions that don't meet that requirement.
|
4
|
+
|
5
|
+
unless Object.respond_to?(:blank?)
|
6
|
+
class Object
|
7
|
+
# An object is blank if it's false, empty, or a whitespace string.
|
8
|
+
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
|
9
|
+
#
|
10
|
+
# This simplifies
|
11
|
+
#
|
12
|
+
# !address || address.empty?
|
13
|
+
#
|
14
|
+
# to
|
15
|
+
#
|
16
|
+
# address.blank?
|
17
|
+
#
|
18
|
+
# @return [true, false]
|
19
|
+
def blank?
|
20
|
+
respond_to?(:empty?) ? !!empty? : !self
|
21
|
+
end
|
22
|
+
|
23
|
+
# An object is present if it's not blank.
|
24
|
+
#
|
25
|
+
# @return [true, false]
|
26
|
+
def present?
|
27
|
+
!blank?
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns the receiver if it's present otherwise returns +nil+.
|
31
|
+
# <tt>object.presence</tt> is equivalent to
|
32
|
+
#
|
33
|
+
# object.present? ? object : nil
|
34
|
+
#
|
35
|
+
# For example, something like
|
36
|
+
#
|
37
|
+
# state = params[:state] if params[:state].present?
|
38
|
+
# country = params[:country] if params[:country].present?
|
39
|
+
# region = state || country || 'US'
|
40
|
+
#
|
41
|
+
# becomes
|
42
|
+
#
|
43
|
+
# region = params[:state].presence || params[:country].presence || 'US'
|
44
|
+
#
|
45
|
+
# @return [Object]
|
46
|
+
def presence
|
47
|
+
self if present?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class NilClass
|
52
|
+
# +nil+ is blank:
|
53
|
+
#
|
54
|
+
# nil.blank? # => true
|
55
|
+
#
|
56
|
+
# @return [true]
|
57
|
+
def blank?
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class FalseClass
|
63
|
+
# +false+ is blank:
|
64
|
+
#
|
65
|
+
# false.blank? # => true
|
66
|
+
#
|
67
|
+
# @return [true]
|
68
|
+
def blank?
|
69
|
+
true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class TrueClass
|
74
|
+
# +true+ is not blank:
|
75
|
+
#
|
76
|
+
# true.blank? # => false
|
77
|
+
#
|
78
|
+
# @return [false]
|
79
|
+
def blank?
|
80
|
+
false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class Array
|
85
|
+
# An array is blank if it's empty:
|
86
|
+
#
|
87
|
+
# [].blank? # => true
|
88
|
+
# [1,2,3].blank? # => false
|
89
|
+
#
|
90
|
+
# @return [true, false]
|
91
|
+
alias_method :blank?, :empty?
|
92
|
+
end
|
93
|
+
|
94
|
+
class Hash
|
95
|
+
# A hash is blank if it's empty:
|
96
|
+
#
|
97
|
+
# {}.blank? # => true
|
98
|
+
# { key: 'value' }.blank? # => false
|
99
|
+
#
|
100
|
+
# @return [true, false]
|
101
|
+
alias_method :blank?, :empty?
|
102
|
+
end
|
103
|
+
|
104
|
+
class String
|
105
|
+
BLANK_RE = /\A[[:space:]]*\z/
|
106
|
+
|
107
|
+
# A string is blank if it's empty or contains whitespaces only:
|
108
|
+
#
|
109
|
+
# ''.blank? # => true
|
110
|
+
# ' '.blank? # => true
|
111
|
+
# "\t\n\r".blank? # => true
|
112
|
+
# ' blah '.blank? # => false
|
113
|
+
#
|
114
|
+
# Unicode whitespace is supported:
|
115
|
+
#
|
116
|
+
# "\u00a0".blank? # => true
|
117
|
+
#
|
118
|
+
# @return [true, false]
|
119
|
+
def blank?
|
120
|
+
# The regexp that matches blank strings is expensive. For the case of empty
|
121
|
+
# strings we can speed up this method (~3.5x) with an empty? call. The
|
122
|
+
# penalty for the rest of strings is marginal.
|
123
|
+
empty? || BLANK_RE === self
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
class Numeric #:nodoc:
|
128
|
+
# No number is blank:
|
129
|
+
#
|
130
|
+
# 1.blank? # => false
|
131
|
+
# 0.blank? # => false
|
132
|
+
#
|
133
|
+
# @return [false]
|
134
|
+
def blank?
|
135
|
+
false
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
class Time #:nodoc:
|
140
|
+
# No Time is blank:
|
141
|
+
#
|
142
|
+
# Time.now.blank? # => false
|
143
|
+
#
|
144
|
+
# @return [false]
|
145
|
+
def blank?
|
146
|
+
false
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -190,39 +190,43 @@ describe Hash do
|
|
190
190
|
include_examples "core_ext/hash/nested"
|
191
191
|
end
|
192
192
|
|
193
|
-
|
194
|
-
require 'active_support
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
193
|
+
begin
|
194
|
+
require 'active_support'
|
195
|
+
require 'active_support/core_ext/hash'
|
196
|
+
describe HashWithIndifferentAccess do
|
197
|
+
let(:hash) do
|
198
|
+
described_class.new.merge(
|
199
|
+
"a" => 1,
|
200
|
+
"b" => {},
|
201
|
+
"c" => {"c1" => 2},
|
202
|
+
"d" => {"d1" => {"d2" => {"d3" => 3}}},
|
203
|
+
"e" => Hash.new(4),
|
204
|
+
"f" => described_class.new { |h, k| h[k] = described_class.new },
|
205
|
+
nil => {nil => 7},
|
206
|
+
["h", "i"] => 8
|
207
|
+
)
|
208
|
+
|
209
|
+
# NOTE: "f" has to be initialized in that way due to a bug in
|
210
|
+
# HashWithIndifferentAccess and assigning a Hash with a default proc.
|
211
|
+
#
|
212
|
+
# 1.9.3 :001 > h1 = Hash.new
|
213
|
+
# 1.9.3 :002 > h1[:a] = Hash.new { |h, k| h[k] = Hash.new }
|
214
|
+
# 1.9.3 :003 > h1[:a].class
|
215
|
+
# => Hash
|
216
|
+
# 1.9.3 :004 > h1[:a][:b].class
|
217
|
+
# => Hash
|
218
|
+
#
|
219
|
+
# 1.9.3 :005 > require 'active_support/all'
|
220
|
+
# 1.9.3 :006 > h2 = HashWithIndifferentAccess.new
|
221
|
+
# 1.9.3 :007 > h2[:a] = Hash.new { |h, k| h[k] = Hash.new }
|
222
|
+
# 1.9.3 :008 > h2[:a].class
|
223
|
+
# => ActiveSupport::HashWithIndifferentAccess
|
224
|
+
# 1.9.3 :009 > h2[:a][:b].class
|
225
|
+
# => NilClass
|
226
|
+
end
|
227
|
+
|
228
|
+
include_examples "core_ext/hash/nested"
|
225
229
|
end
|
226
|
-
|
227
|
-
|
230
|
+
rescue LoadError
|
231
|
+
# ActiveSupport v5.0.0 requires ruby '>=2.2.2', skip these tests on older rubies.
|
228
232
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
describe "blank" do
|
2
|
+
class EmptyTrue
|
3
|
+
def empty?
|
4
|
+
0
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class EmptyFalse
|
9
|
+
def empty?
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
BLANK = [EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", ' ', "\u00a0", [], {}].freeze
|
15
|
+
PRESENT = [EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], {nil => 0}].freeze
|
16
|
+
|
17
|
+
describe "#blank?" do
|
18
|
+
context "true" do
|
19
|
+
BLANK.each { |v| it(v.inspect) { expect(v).to be_blank } }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "false" do
|
23
|
+
PRESENT.each { |v| it(v.inspect) { expect(v).to_not be_blank } }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#present?" do
|
28
|
+
context "true" do
|
29
|
+
PRESENT.each { |v| it(v.inspect) { expect(v).to be_present } }
|
30
|
+
end
|
31
|
+
|
32
|
+
context "false" do
|
33
|
+
BLANK.each { |v| it(v.inspect) { expect(v).to_not be_present } }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#presence" do
|
38
|
+
context "object" do
|
39
|
+
PRESENT.each { |v| it(v.inspect) { expect(v.presence).to eq(v) } }
|
40
|
+
end
|
41
|
+
|
42
|
+
context "nil" do
|
43
|
+
BLANK.each { |v| it(v.inspect) { expect(v.presence).to be_nil } }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: more_core_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Frey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,16 +70,16 @@ dependencies:
|
|
70
70
|
name: activesupport
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
76
|
-
type: :
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
description: MoreCoreExtensions are a set of core extensions beyond those provided
|
84
84
|
by ActiveSupport.
|
85
85
|
email:
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- lib/more_core_extensions/core_ext/module.rb
|
109
109
|
- lib/more_core_extensions/core_ext/module/namespace.rb
|
110
110
|
- lib/more_core_extensions/core_ext/object.rb
|
111
|
+
- lib/more_core_extensions/core_ext/object/blank.rb
|
111
112
|
- lib/more_core_extensions/core_ext/object/namespace.rb
|
112
113
|
- lib/more_core_extensions/core_ext/shared/nested.rb
|
113
114
|
- lib/more_core_extensions/core_ext/string.rb
|
@@ -124,6 +125,7 @@ files:
|
|
124
125
|
- spec/core_ext/array/tableize_spec.rb
|
125
126
|
- spec/core_ext/hash/deletes_spec.rb
|
126
127
|
- spec/core_ext/hash/nested_spec.rb
|
128
|
+
- spec/core_ext/object/blank_spec.rb
|
127
129
|
- spec/core_ext/object/namespace_spec.rb
|
128
130
|
- spec/core_ext/string/formats_spec.rb
|
129
131
|
- spec/core_ext/string/hex_dump_spec.rb
|
@@ -140,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
140
142
|
requirements:
|
141
143
|
- - ">="
|
142
144
|
- !ruby/object:Gem::Version
|
143
|
-
version:
|
145
|
+
version: 2.0.0
|
144
146
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
147
|
requirements:
|
146
148
|
- - ">="
|
@@ -148,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
150
|
version: '0'
|
149
151
|
requirements: []
|
150
152
|
rubyforge_project:
|
151
|
-
rubygems_version: 2.
|
153
|
+
rubygems_version: 2.6.3
|
152
154
|
signing_key:
|
153
155
|
specification_version: 4
|
154
156
|
summary: MoreCoreExtensions are a set of core extensions beyond those provided by
|
@@ -164,6 +166,7 @@ test_files:
|
|
164
166
|
- spec/core_ext/array/tableize_spec.rb
|
165
167
|
- spec/core_ext/hash/deletes_spec.rb
|
166
168
|
- spec/core_ext/hash/nested_spec.rb
|
169
|
+
- spec/core_ext/object/blank_spec.rb
|
167
170
|
- spec/core_ext/object/namespace_spec.rb
|
168
171
|
- spec/core_ext/string/formats_spec.rb
|
169
172
|
- spec/core_ext/string/hex_dump_spec.rb
|