ensure_it 0.1.4 → 0.1.5

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: b5f9d6674b5751ec725332d84f6a9527d1824efa
4
- data.tar.gz: b5b0fbe8eec8d2ddd7e35b8a79c5560e7541868f
3
+ metadata.gz: a99f1a9c837f0279fd4e71b337e15e8449618dc0
4
+ data.tar.gz: ae1caca2ca9c85198c5c7a1f149d18ce8ef18741
5
5
  SHA512:
6
- metadata.gz: 77f9808d8d5ede9eea61208535bec4e3b8609b3a9a254cba63203b29fb8fe34ab8f970fca2b341ce9cc3512e4f10d0e0ac7538ac80e2d6189d343d01cf59dd52
7
- data.tar.gz: 34765d4a0fd5e73c2e9bd2b33b2d931ad55983004bd126a083704402a34478f0cf38dfc207adb47dbcf0a9af3814c80af326ba439d81806eaff27509e9047e64
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
- true.ensure_array # => []
194
- true.ensure_array(default: nil) # => nil
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
- opts[:message] ||= '#{subject} should be an Array'
9
- EnsureIt.raise_error(:ensure_array!, **opts)
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
- ENSURES = %i(ensure_symbol ensure_symbol! ensure_string ensure_string!
17
- ensure_integer ensure_integer! ensure_float ensure_float!
18
- ensure_array ensure_array! ensure_class ensure_class!)
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
- OPERATIONS = %i(compact flatten reverse rotate shuffle sort sort_desc uniq)
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
- if ENSURE_IT_REFINED
24
- def ensure_array(*args, values: nil, **opts)
25
- arr = self
26
- args.each do |arg|
27
- if arg.is_a?(Proc)
28
- arr = arr.map(arg)
29
- next
30
- end
31
- arg = arg.ensure_symbol || next
32
- case arg
33
- when *ENSURES
34
- arr = case arg
35
- when :ensure_symbol then map { |x| x.ensure_symbol }
36
- when :ensure_symbol! then map { |x| x.ensure_symbol! }
37
- when :ensure_string then map { |x| x.ensure_string }
38
- when :ensure_string! then map { |x| x.ensure_string! }
39
- when :ensure_integer then map { |x| x.ensure_integer }
40
- when :ensure_integer! then map { |x| x.ensure_integer! }
41
- when :ensure_float then map { |x| x.ensure_float }
42
- when :ensure_float! then map { |x| x.ensure_float! }
43
- when :ensure_array then map { |x| x.ensure_array }
44
- when :ensure_array! then map { |x| x.ensure_array! }
45
- when :ensure_class then map { |x| x.ensure_class }
46
- when :ensure_class! then map { |x| x.ensure_class! }
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
- when *OPERATIONS
49
- op = arg == :sort_desc ? :sort : arg
50
- arr = arr.send(op)
51
- arr = arr.reverse if arg == :sort_desc
52
- else
53
- arr = arr.map { |x| x.respond_to?(arg) ? x.send(arg) : nil }
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
- else
59
- def ensure_array(*args, values: nil, **opts)
60
- arr = self
61
- args.each do |arg|
62
- if arg.is_a?(Proc)
63
- arr = arr.map(arg)
64
- next
65
- end
66
- arg = arg.ensure_symbol || next
67
- case arg
68
- when *ENSURES then arr = arr.map(&arg)
69
- when *OPERATIONS
70
- op = arg == :sort_desc ? :sort : arg
71
- arr = arr.send(op)
72
- arr = arr.reverse if arg == :sort_desc
73
- else
74
- arr = arr.map { |x| x.respond_to?(arg) ? x.send(arg) : nil }
75
- end
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
- alias_method :ensure_array!, :ensure_array
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
@@ -1,3 +1,3 @@
1
1
  module EnsureIt
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
data/lib/ensure_it.rb CHANGED
@@ -1,13 +1,19 @@
1
1
  if RUBY_VERSION < '2.0.0'
2
- fail %q{EnsureIt: library doesn't support ruby < 2.0.0}
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 ['s', 'v']
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ensure_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Ovchinnikov