ensure_it 0.1.4 → 0.1.5
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/README.md +9 -3
- data/lib/ensure_it/ensure_array.rb +85 -61
- data/lib/ensure_it/version.rb +1 -1
- data/lib/ensure_it.rb +7 -1
- data/spec/lib/ensure_array_spec.rb +6 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a99f1a9c837f0279fd4e71b337e15e8449618dc0
|
4
|
+
data.tar.gz: ae1caca2ca9c85198c5c7a1f149d18ce8ef18741
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a037de6ea3e3260557b445ca8bfa051410e9a67a92c3ac35d61befc6ad51eaf0d84082cb85920f549fccd8e633837d3bb21cab1d1065dd463ec63580b5f7bef3
|
7
|
+
data.tar.gz: 30c3e01829b7099823ee817ac69030d53eeb788531f5d1d106127e6fe782b58f549771fc402d7a574a7ff7e05361a2db0c4ac40b6d7a9371e3ef31713b277e5c
|
data/README.md
CHANGED
@@ -186,12 +186,14 @@ true.ensure_float # => nil
|
|
186
186
|
|
187
187
|
### ensure_array, ensure_array!
|
188
188
|
|
189
|
-
By default, returns Array only for Array itself and **empty** array (not nil) for others. You can specify any number of arguments. Each argument can be a Proc or a symbol. If Proc given, it will be used as argument for `map` method of array, if symbol specified and it is one of `compact`, `flatten`, `reverse`, `rotate`, `shuffle`, `sort`, `sort_desc`, `uniq` then respective method wiill be called for array (for `sort_desc`, `sort` and then `reverse` will be called). In other cases specified method will be called for each array element inside `map` function. All arguments are processed in specified order. Examples:
|
189
|
+
By default, returns Array only for Array itself and **empty** array (not nil) for others. You can specify any number of arguments. Each argument can be a Proc or a symbol. If Proc given, it will be used as argument for `map` method of array, if symbol specified and it is one of `compact`, `flatten`, `reverse`, `rotate`, `shuffle`, `sort`, `sort_desc`, `uniq` then respective method wiill be called for array (for `sort_desc`, `sort` and then `reverse` will be called). In other cases specified method will be called for each array element inside `map` function. All arguments are processed in specified order. Also you can use `make: true` option to make array with object as single element if object is not an array (for `nil` empty array created). Examples:
|
190
190
|
|
191
191
|
```ruby
|
192
192
|
[1, nil, 2].ensure_array # => [1, nil, 2]
|
193
|
-
|
194
|
-
|
193
|
+
10.ensure_array # => []
|
194
|
+
10.ensure_array(default: nil) # => nil
|
195
|
+
10.ensure_array(make: true) # => [10]
|
196
|
+
nil.ensure_array(make: true) # => []
|
195
197
|
[1, nil, 2].ensure_array(:compact) # => [1, 2]
|
196
198
|
[1, [2, 3], 4].ensure_array(:flatten) # => [1, 2, 3, 4]
|
197
199
|
[1, [5, 6], 4].ensure_array(:flatten, :sort) # => [1, 4, 5, 6]
|
@@ -372,6 +374,10 @@ thor ensure_it:benchmark:all -n 1000 -s
|
|
372
374
|
|
373
375
|
## Changelog
|
374
376
|
|
377
|
+
`0.1.5`
|
378
|
+
* added `EnsureIt.refined?`
|
379
|
+
* `ensure_array` `make` option added
|
380
|
+
|
375
381
|
`0.1.4`
|
376
382
|
* name_of option added to `ensure_symbol` and `ensure_string`
|
377
383
|
* string options added to `ensure_class`
|
@@ -1,82 +1,106 @@
|
|
1
|
+
#
|
1
2
|
module EnsureIt
|
2
3
|
patch Object do
|
3
|
-
def ensure_array(*args, default: [], **opts)
|
4
|
-
default
|
4
|
+
def ensure_array(*args, default: [], make: false, **opts)
|
5
|
+
return default if make != true
|
6
|
+
EnsureIt.ensure_array([self], *args, **opts)
|
5
7
|
end
|
6
8
|
|
7
|
-
def ensure_array!(*args, **opts)
|
8
|
-
|
9
|
-
EnsureIt.raise_error(:ensure_array!,
|
9
|
+
def ensure_array!(*args, make: false, **opts)
|
10
|
+
return EnsureIt.ensure_array([self], *args, **opts) if make == true
|
11
|
+
EnsureIt.raise_error(:ensure_array!,
|
12
|
+
**EnsureIt.ensure_array_error(**opts))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
patch NilClass do
|
17
|
+
def ensure_array(*args, default: [], make: false, **opts)
|
18
|
+
make == true ? [] : default
|
19
|
+
end
|
20
|
+
|
21
|
+
def ensure_array!(*args, make: false, **opts)
|
22
|
+
return [] if make == true
|
23
|
+
EnsureIt.raise_error(:ensure_array!,
|
24
|
+
**EnsureIt.ensure_array_error(**opts))
|
10
25
|
end
|
11
26
|
end
|
12
27
|
|
13
28
|
patch Array do
|
14
29
|
using EnsureIt if ENSURE_IT_REFINED
|
15
30
|
|
16
|
-
|
17
|
-
|
18
|
-
|
31
|
+
def ensure_array(*args, **opts)
|
32
|
+
EnsureIt.ensure_array(self, *args, **opts)
|
33
|
+
end
|
34
|
+
alias_method :ensure_array!, :ensure_array
|
35
|
+
end
|
19
36
|
|
20
|
-
|
37
|
+
ENSURES = %i(ensure_symbol ensure_symbol! ensure_string ensure_string!
|
38
|
+
ensure_integer ensure_integer! ensure_float ensure_float!
|
39
|
+
ensure_array ensure_array! ensure_class ensure_class!)
|
21
40
|
|
41
|
+
OPERATIONS = %i(compact flatten reverse rotate shuffle sort sort_desc uniq)
|
22
42
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
when :
|
38
|
-
when :
|
39
|
-
when :
|
40
|
-
when :
|
41
|
-
when :
|
42
|
-
when :
|
43
|
-
when :
|
44
|
-
when :
|
45
|
-
when :
|
46
|
-
when :
|
43
|
+
if ENSURE_IT_REFINED
|
44
|
+
using EnsureIt
|
45
|
+
|
46
|
+
def self.ensure_array(arr, *args, values: nil, **opts)
|
47
|
+
args.each do |arg|
|
48
|
+
if arg.is_a?(Proc)
|
49
|
+
arr = arr.map(arg)
|
50
|
+
next
|
51
|
+
end
|
52
|
+
arg = arg.ensure_symbol || next
|
53
|
+
case arg
|
54
|
+
when *ENSURES
|
55
|
+
arr =
|
56
|
+
case arg
|
57
|
+
when :ensure_symbol then arr.map { |x| x.ensure_symbol }
|
58
|
+
when :ensure_symbol! then arr.map { |x| x.ensure_symbol! }
|
59
|
+
when :ensure_string then arr.map { |x| x.ensure_string }
|
60
|
+
when :ensure_string! then arr.map { |x| x.ensure_string! }
|
61
|
+
when :ensure_integer then arr.map { |x| x.ensure_integer }
|
62
|
+
when :ensure_integer! then arr.map { |x| x.ensure_integer! }
|
63
|
+
when :ensure_float then arr.map { |x| x.ensure_float }
|
64
|
+
when :ensure_float! then arr.map { |x| x.ensure_float! }
|
65
|
+
when :ensure_array then arr.map { |x| x.ensure_array }
|
66
|
+
when :ensure_array! then arr.map { |x| x.ensure_array! }
|
67
|
+
when :ensure_class then arr.map { |x| x.ensure_class }
|
68
|
+
when :ensure_class! then arr.map { |x| x.ensure_class! }
|
47
69
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
70
|
+
when *OPERATIONS
|
71
|
+
op = arg == :sort_desc ? :sort : arg
|
72
|
+
arr = arr.send(op)
|
73
|
+
arr = arr.reverse if arg == :sort_desc
|
74
|
+
else
|
75
|
+
arr = arr.map { |x| x.respond_to?(arg) ? x.send(arg) : nil }
|
55
76
|
end
|
56
|
-
values.is_a?(Array) ? arr & values : arr
|
57
77
|
end
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
78
|
+
values.is_a?(Array) ? arr & values : arr
|
79
|
+
end
|
80
|
+
else
|
81
|
+
def self.ensure_array(arr, *args, values: nil, **opts)
|
82
|
+
args.each do |arg|
|
83
|
+
if arg.is_a?(Proc)
|
84
|
+
arr = arr.map(arg)
|
85
|
+
next
|
86
|
+
end
|
87
|
+
arg = arg.ensure_symbol || next
|
88
|
+
case arg
|
89
|
+
when *ENSURES then arr = arr.map(&arg)
|
90
|
+
when *OPERATIONS
|
91
|
+
op = arg == :sort_desc ? :sort : arg
|
92
|
+
arr = arr.send(op)
|
93
|
+
arr = arr.reverse if arg == :sort_desc
|
94
|
+
else
|
95
|
+
arr = arr.map { |x| x.respond_to?(arg) ? x.send(arg) : nil }
|
76
96
|
end
|
77
|
-
values.is_a?(Array) ? arr & values : arr
|
78
97
|
end
|
98
|
+
values.is_a?(Array) ? arr & values : arr
|
79
99
|
end
|
80
|
-
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.ensure_array_error(**opts)
|
103
|
+
opts[:message] ||= '#{subject} should be an Array'
|
104
|
+
opts
|
81
105
|
end
|
82
106
|
end
|
data/lib/ensure_it/version.rb
CHANGED
data/lib/ensure_it.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
if RUBY_VERSION < '2.0.0'
|
2
|
-
fail %q
|
2
|
+
fail %q(EnsureIt: library doesn't support ruby < 2.0.0)
|
3
3
|
end
|
4
4
|
|
5
5
|
defined?(ENSURE_IT_REFINED) || ENSURE_IT_REFINED = false
|
6
6
|
|
7
|
+
#
|
7
8
|
module EnsureIt
|
9
|
+
# dummy module for eager usage
|
8
10
|
module StringUtils
|
9
11
|
def self.ensure_name(*args); end
|
10
12
|
end
|
13
|
+
|
14
|
+
def self.refined?
|
15
|
+
ENSURE_IT_REFINED == true
|
16
|
+
end
|
11
17
|
end
|
12
18
|
|
13
19
|
require File.join %w(ensure_it version)
|
@@ -44,7 +44,7 @@ describe EnsureIt do
|
|
44
44
|
|
45
45
|
it 'calls standard method for each element' do
|
46
46
|
arr = ['s', :v]
|
47
|
-
expect(call_for(arr, :to_s)).to eq
|
47
|
+
expect(call_for(arr, :to_s)).to eq %w(s v)
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'chains methods for each element' do
|
@@ -57,6 +57,11 @@ describe EnsureIt do
|
|
57
57
|
call_for([1, 5, 6, 4], values: [1, 6, 8])
|
58
58
|
).to eq [1, 6]
|
59
59
|
end
|
60
|
+
|
61
|
+
it 'creates array with make option' do
|
62
|
+
expect(call_for(1, make: true)).to eq [1]
|
63
|
+
expect(call_for(nil, make: true)).to eq []
|
64
|
+
end
|
60
65
|
end
|
61
66
|
|
62
67
|
describe '#ensure_array' do
|