ensure_it 0.1.5 → 1.0.0

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: a99f1a9c837f0279fd4e71b337e15e8449618dc0
4
- data.tar.gz: ae1caca2ca9c85198c5c7a1f149d18ce8ef18741
3
+ metadata.gz: 7c19bb7f7a340fbb0721719e3bdaa50a255e290b
4
+ data.tar.gz: 1bd4a40db51f2f18db0800f06f0f0e88739d5538
5
5
  SHA512:
6
- metadata.gz: a037de6ea3e3260557b445ca8bfa051410e9a67a92c3ac35d61befc6ad51eaf0d84082cb85920f549fccd8e633837d3bb21cab1d1065dd463ec63580b5f7bef3
7
- data.tar.gz: 30c3e01829b7099823ee817ac69030d53eeb788531f5d1d106127e6fe782b58f549771fc402d7a574a7ff7e05361a2db0c4ac40b6d7a9371e3ef31713b277e5c
6
+ metadata.gz: 1877f38a3bb65753fda9210c8f2526b33f5526e491e1877ca9af81f266c3376d10ff45162f57e37b8fc4921bd4ccedcc3dc24632f377116fa5fecc6dbd1810f0
7
+ data.tar.gz: 3e2483590c0cafbb7150eff7e960d74dd63b38ef694f0d38394972fe97142e6a7456febb8fa7db448d323e09b3a17ba1e4e15d8c5a3e383984d1c975ca8dc6aa
data/README.md CHANGED
@@ -87,7 +87,7 @@ EnsureIt does monkey-patching or provides refines (see [Refinements section](#re
87
87
 
88
88
  For example `ensure_symbol` method returns symbol itself for Symbols, converted to symbol value for Strings and nil for all other. Same way `ensure_symbol!` returns symbol for String and Symbol, and raises exception for all other.
89
89
 
90
- The special thing, that EnsureIt can do (and do it by default) is smart error messages in bang methods. In most cases, EnsureIt guesses right context in wich `ensure_*` method called and froms more informative message. It recognizes name of local variable if method called for variable like `my_var.ensure_symbol`, argument name, if variable is argument of method and method calls itself like `'some_string'.to_sym.ensure_symbol` - so `ensure_symbol` called on result of `to_sym` method. You can disable this functionality at all (see [Configuration section](#configuration)) or override globally configuration for any method call by `smart` option like this `:symbol.ensure_symbol(smart: true)` or `:symbol.ensure_symbol(smart: false)`. In any way, this `:smart` errors doesn't affect execution speed because the analyzing block of code executed only on exception - not on every `ensure_*` call.
90
+ The special thing, that EnsureIt can do (and do it by default) is smart error messages in bang methods. In most cases, EnsureIt guesses right context in wich `ensure_*` method called and forms more informative message. It recognizes name of local variable if method called for variable like `my_var.ensure_symbol`, argument name, if variable is argument of method and method calls itself like `'some_string'.to_sym.ensure_symbol` - so `ensure_symbol` called on result of `to_sym` method. You can disable this functionality at all (see [Configuration section](#configuration)) or override global configuration for any method call by `smart` option like this `:symbol.ensure_symbol(smart: true)` or `:symbol.ensure_symbol(smart: false)`. In any way, this `:smart` errors doesn't affect execution speed because the analyzing block of code executed only on exception - not on every `ensure_*` call.
91
91
 
92
92
  For example, following code
93
93
 
@@ -186,7 +186,7 @@ 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. 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:
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 will 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]
@@ -206,21 +206,21 @@ arr.ensure_array(:to_s) # => ['some', 'value'] standard methods can be used
206
206
  arr.ensure_array(:ensure_string, :to_sym) # => [:some, :value] you can chain methods
207
207
  ```
208
208
 
209
- Simple usage example:
209
+ Simple usage example (`examples/array.rb`):
210
210
 
211
211
  ```ruby
212
212
  require 'ensure_it'
213
213
 
214
214
  class Awesome
215
215
  def self.define_getters(*args)
216
- args.ensure_array(:ensure_symbol, compact: true).each do |n|
216
+ args.ensure_array(:ensure_symbol, :compact).each do |n|
217
217
  define_method(n) { instance_variable_get("@#{n}") }
218
218
  end
219
219
  end
220
220
  end
221
221
 
222
222
  Awesome.define_getters(:one, 'two', nil, false, Object, :three)
223
- Awesome.methods(false) #=> [:one, :two, :three]
223
+ puts Awesome.instance_methods(false).inspect #=> [:one, :two, :three]
224
224
  ```
225
225
 
226
226
  ### ensure_hash, ensure_hash!
@@ -230,29 +230,30 @@ Returns Hash only for Hash itself and **empty** hash (not nil) for others. Symbo
230
230
  ```ruby
231
231
  {some: 0, 'key' => 1}.ensure_hash # => {some: 0, 'key' => 1}
232
232
  0.ensure_hash # => {}
233
- 0.ensure_hash(wrong: nil) # => nil
233
+ 0.ensure_hash(default: nil) # => nil
234
234
  {some: 0, 'key' => 1}.ensure_hash(symbolize_keys: true) # => {some: 0, key: 0}
235
235
  ```
236
236
 
237
237
  ### ensure_instance_of, ensure_instance_of!
238
238
 
239
- Returns self only if it instance of specified class or nil (or raise) elsewhere:
239
+ Returns self only if it is an instance of specified class or nil (or raise) elsewhere:
240
240
 
241
241
  ```ruby
242
242
  10.ensure_instance_of(Fixnum) # => 10
243
243
  10.0.ensure_instance_of(Fixnum) # => nil
244
- 10.0.ensure_instance_of(Fixnum, wrong: -1) # => -1
244
+ 10.0.ensure_instance_of(Fixnum, default: -1) # => -1
245
245
  ```
246
246
 
247
247
  ### ensure_class, ensure_class!
248
248
 
249
- Returns self only if it is a class and optionally have specified ancestors or nil (or raise) elsewhere:
249
+ Returns self only if it is a class and optionally have specified ancestors or nil (or raise) elsewhere. With `strings: true` option, returns class, specified in string:
250
250
 
251
251
  ```ruby
252
252
  10.ensure_class # => nil
253
253
  String.ensure_class # => String
254
254
  Fixnum.ensure_class(Integer) # => Fixnum
255
255
  Float.ensure_class(Integer) # => nil
256
+ 'Array'.ensure_class(strings: true) # => Array
256
257
 
257
258
  module CustomModule; end
258
259
  class CustomArray < Array;
@@ -263,11 +264,30 @@ Array.ensure_class(Enumerable, CustomModule) # => nil
263
264
  Array.ensure_class(Enumerable) # => Array
264
265
  ```
265
266
 
267
+ ### ensure_boolean, ensure_boolean!
268
+
269
+ Returns true or false for booleans itself and for numbers (0 - false, other - true). With `strings: true` option, returns `true` for String and Symbols with values `'true'`, `'yes'`, `'y'`, `'1'` and `false` for others. For numbers with `positive: true` option returns `true` only for positive numbers.
270
+
271
+ ```ruby
272
+ :true.ensure_boolean # => true
273
+ :false.ensure_boolean # => false
274
+ 1.ensure_boolean # => true
275
+ 0.ensure_boolean # => false
276
+ -1.ensure_boolean # => true
277
+ -1.ensure_boolean(positive: true) # => false
278
+ 1.ensure_boolean(numbers: false) # => nil
279
+ 'true'.ensure_boolean # => nil
280
+ 'true'.ensure_boolean(strings: true) # => true
281
+ 'yes'.ensure_boolean(strings: true) # => true
282
+ :true.ensure_boolean(strings: true) # => true
283
+ 'false'.ensure_boolean(strings: true) # => false
284
+ ```
285
+
266
286
  ### Common options for all methods
267
287
 
268
288
  |option|possible values|meaning|
269
289
  |------|---------------|-------|
270
- |`:values`|Array|(not used in `ensure_instance_of` and `ensure_hash`) an array of possible values. If value doesn't included in this array, default value returned or exception raised for bang methods. Note that library doesn't check types of this array elements, so be sure to specify array with right elements here.
290
+ |`:values`|Array|(not used in `ensure_instance_of`, `ensure_hash` and `ensure_boolean`) an array of possible values. If value doesn't contained in this array, default value returned or exception raised for bang methods. Note that library doesn't check types of this array elements, so be sure to specify array with right elements here.
271
291
 
272
292
  ### Common options for all non-bang methods
273
293
 
@@ -300,7 +320,7 @@ Or without bundler:
300
320
  require 'ensure_it_refined'
301
321
  ```
302
322
 
303
- Then activate EnsureIt refines by `using EnsureIt` in needed scope:
323
+ Then activate EnsureIt refines by `using EnsureIt` in appropriated scope:
304
324
 
305
325
  ```ruby
306
326
  require 'ensure_it_refined'
@@ -320,6 +340,15 @@ AwesomeClass.new.awesome_method(0) # => raises EnsureIt::Error with message
320
340
 
321
341
  Please read carefully [refinements](http://www.ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html) documentation before using refined EnsureIt. Don't forget to call `using EnsureIt` in every file (not class or method if your class or method placed in many files) you need it.
322
342
 
343
+ If you using refined library, but want to support mode without refinements, activate refines with condition like this:
344
+
345
+ ```ruby
346
+ module SomeModule
347
+ using EnsureIt if EnsureIt.refined?
348
+ ...
349
+ end
350
+ ```
351
+
323
352
  ## Benchmarking
324
353
 
325
354
  In development mode a set of thor tasks under 'ensure_it:benchmark' namespace provided for benchmarking and profiling any library method. Also tasks `:non_bang`, `:bang` and `:all` provided for benchmarking all non-bang methods, all bang methods and allmost all methods respectively. To benchmark refined version of library, use `USE_REFINES=true` environment variable.
@@ -374,6 +403,10 @@ thor ensure_it:benchmark:all -n 1000 -s
374
403
 
375
404
  ## Changelog
376
405
 
406
+ `1.0.0`
407
+ * first release version
408
+ * added `ensure_boolean`
409
+
377
410
  `0.1.5`
378
411
  * added `EnsureIt.refined?`
379
412
  * `ensure_array` `make` option added
@@ -416,9 +449,7 @@ thor ensure_it:benchmark:all -n 1000 -s
416
449
 
417
450
  ## Todo
418
451
 
419
- * class from string converting for ensure_class
420
- * ensure_file_name
421
- * ensure_var_name
452
+ * ensure file name
422
453
  * enlarge number of options for arrays and hashes
423
454
  * block processing for arrays and hashes
424
455
  * rspec matchers
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'ensure_it'
4
+
5
+ class Awesome
6
+ def self.define_getters(*args)
7
+ args.ensure_array(:ensure_symbol, :compact).each do |n|
8
+ define_method(n) { instance_variable_get("@#{n}") }
9
+ end
10
+ end
11
+ end
12
+
13
+ Awesome.define_getters(:one, 'two', nil, false, Object, :three)
14
+ puts Awesome.instance_methods(false).inspect #=> [:one, :two, :three]
@@ -26,6 +26,7 @@ require File.join %w(ensure_it ensure_integer)
26
26
  require File.join %w(ensure_it ensure_float)
27
27
  require File.join %w(ensure_it ensure_array)
28
28
  require File.join %w(ensure_it ensure_hash)
29
+ require File.join %w(ensure_it ensure_boolean)
29
30
  require File.join %w(ensure_it ensure_instance_of)
30
31
  require File.join %w(ensure_it ensure_class)
31
32
  require File.join %w(ensure_it string_utils)
@@ -0,0 +1,75 @@
1
+ module EnsureIt
2
+ TRUE_NAMES = %w(true y yes 1)
3
+
4
+ patch Object do
5
+ def ensure_boolean(default: nil, **opts)
6
+ default
7
+ end
8
+
9
+ def ensure_boolean!(**opts)
10
+ EnsureIt.raise_error(:ensure_boolean!,
11
+ **EnsureIt::ensure_boolean_error(**opts))
12
+ end
13
+ end
14
+
15
+ patch String do
16
+ def ensure_boolean(default: nil, strings: false, **opts)
17
+ return TRUE_NAMES.include?(downcase) if strings == true
18
+ default
19
+ end
20
+
21
+ def ensure_boolean!(strings: false, **opts)
22
+ return TRUE_NAMES.include?(downcase) if strings == true
23
+ EnsureIt.raise_error(:ensure_boolean!,
24
+ **EnsureIt::ensure_boolean_error(**opts))
25
+ end
26
+ end
27
+
28
+ patch Symbol do
29
+ def ensure_boolean(default: nil, strings: false, **opts)
30
+ return TRUE_NAMES.include?(to_s.downcase) if strings == true
31
+ default
32
+ end
33
+
34
+ def ensure_boolean!(strings: false, **opts)
35
+ return TRUE_NAMES.include?(to_s.downcase) if strings == true
36
+ EnsureIt.raise_error(:ensure_boolean!,
37
+ **EnsureIt::ensure_boolean_error(**opts))
38
+ end
39
+ end
40
+
41
+ patch Numeric do
42
+ def ensure_boolean(default: nil, numbers: true, positive: false, **opts)
43
+ return positive == true ? self > 0 : self != 0 if numbers == true
44
+ default
45
+ end
46
+
47
+ def ensure_boolean!(numbers: true, positive: false, **opts)
48
+ return positive == true ? self > 0 : self != 0 if numbers == true
49
+ EnsureIt.raise_error(:ensure_boolean!,
50
+ **EnsureIt::ensure_boolean_error(**opts))
51
+ end
52
+ end
53
+
54
+ patch TrueClass do
55
+ def ensure_boolean(**opts)
56
+ self
57
+ end
58
+ alias_method :ensure_boolean!, :ensure_boolean
59
+ end
60
+
61
+ patch FalseClass do
62
+ def ensure_boolean(**opts)
63
+ self
64
+ end
65
+ alias_method :ensure_boolean!, :ensure_boolean
66
+ end
67
+
68
+ def self.ensure_boolean_error(**opts)
69
+ unless opts.key?(:message)
70
+ opts[:message] = '#{subject} should be a boolean or be able' \
71
+ ' to convert to it'
72
+ end
73
+ opts
74
+ end
75
+ end
@@ -30,16 +30,16 @@ module EnsureIt
30
30
  patch String do
31
31
  using EnsureIt if ENSURE_IT_REFINED
32
32
 
33
- def ensure_class(*args, default: nil, string: nil, **opts)
34
- return default if string != true
33
+ def ensure_class(*args, default: nil, strings: nil, **opts)
34
+ return default if strings != true
35
35
  catch :wrong do
36
36
  return EnsureIt.ensure_class_string(self, *args, **opts)
37
37
  end
38
38
  default
39
39
  end
40
40
 
41
- def ensure_class!(*args, string: nil, **opts)
42
- if string == true
41
+ def ensure_class!(*args, strings: nil, **opts)
42
+ if strings == true
43
43
  catch :wrong do
44
44
  return EnsureIt.ensure_class_string(self, *args, **opts)
45
45
  end
@@ -1,7 +1,7 @@
1
1
  module EnsureIt
2
2
  module StringUtils
3
3
  NAME_TYPES = %i(local instance_variable class_variable setter getter
4
- checker bang method class)
4
+ checker bang method class file)
5
5
  NAME_REGEXP = /\A
6
6
  (?<class_access>@{1,2})?
7
7
  (?<name>[a-z_][a-zA-Z_0-9]*)
@@ -13,6 +13,7 @@ module EnsureIt
13
13
  CLASS_NAME_DOWNCASE_REGEXP = /\A
14
14
  [a-z][a-zA-Z_0-9]*(?:\/[a-z][a-zA-Z_0-9]*)*
15
15
  \z/x
16
+ FILE_EXTENSION_REGEXP = /\A(?<name>.*)(?<ext>\.[^.]+)\z/
16
17
 
17
18
  using EnsureIt if ENSURE_IT_REFINED
18
19
 
@@ -42,6 +43,26 @@ module EnsureIt
42
43
  end
43
44
  end
44
45
  str
46
+ elsif name_of == :file
47
+ if extension = opts[:extension].ensure_string
48
+ extension.gsub!(/\A\.+/, '')
49
+ str.gsub!(/\A\.+/, '')
50
+ if str =~ FILE_EXTENSION_REGEXP
51
+ str.gsub!(FILE_EXTENSION_REGEXP, '\1.' + extension)
52
+ else
53
+ str = [str, extension].join('.')
54
+ end
55
+ end
56
+ if opts[:exist] == true
57
+ if File.exist?(File.expand_path(str, Dir.pwd))
58
+ str = File.expand_path(str, Dir.pwd)
59
+ else
60
+ return nil
61
+ end
62
+ else
63
+ str = File.expand_path(str, Dir.pwd)
64
+ end
65
+ str
45
66
  else
46
67
  m = NAME_REGEXP.match(str)
47
68
  return nil if m.nil?
@@ -1,3 +1,3 @@
1
1
  module EnsureIt
2
- VERSION = '0.1.5'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ class Tester
4
+ using EnsureIt if ENSURE_IT_REFINED
5
+
6
+ def ensure_boolean(*args)
7
+ obj.ensure_boolean(*args)
8
+ end
9
+
10
+ def ensure_boolean!(*args)
11
+ obj.ensure_boolean!(*args)
12
+ end
13
+ end
14
+
15
+ describe EnsureIt do
16
+ shared_examples 'polygraph' do
17
+ it 'and returns self for TrueClass' do
18
+ obj = true
19
+ expect(call_for(obj)).to eq obj
20
+ end
21
+
22
+ it 'and returns self for FalseClass' do
23
+ obj = false
24
+ expect(call_for(obj)).to eq obj
25
+ end
26
+
27
+ it 'and converts numbers' do
28
+ expect(call_for(0)).to be_false
29
+ expect(call_for(1)).to be_true
30
+ expect(call_for(-1.0)).to be_true
31
+ end
32
+
33
+ it 'and converts numbers with positive: true' do
34
+ expect(call_for(1, positive: true)).to be_true
35
+ expect(call_for(-1, positive: true)).to be_false
36
+ end
37
+
38
+ it 'and converts strings with strings: true' do
39
+ expect(call_for('true', strings: true)).to be_true
40
+ expect(call_for('yes', strings: true)).to be_true
41
+ expect(call_for('y', strings: true)).to be_true
42
+ expect(call_for('1', strings: true)).to be_true
43
+ expect(call_for('false', strings: true)).to be_false
44
+ end
45
+
46
+ it 'and converts symbols with strings: true' do
47
+ expect(call_for(:true, strings: true)).to be_true
48
+ expect(call_for(:yes, strings: true)).to be_true
49
+ expect(call_for(:y, strings: true)).to be_true
50
+ expect(call_for(:'1', strings: true)).to be_true
51
+ expect(call_for(:false, strings: true)).to be_false
52
+ end
53
+ end
54
+
55
+ describe '#ensure_boolean' do
56
+ it_behaves_like 'polygraph'
57
+ it_behaves_like(
58
+ 'niller for unmet objects',
59
+ '123test', :test123, :'100',
60
+ except: [Numeric, TrueClass, FalseClass]
61
+ )
62
+ end
63
+
64
+ describe '#ensure_boolean!' do
65
+ it_behaves_like 'polygraph'
66
+ it_behaves_like(
67
+ 'banger for unmet objects',
68
+ '123test', :test123, :'100',
69
+ except: [Numeric, TrueClass, FalseClass],
70
+ message: /should be a boolean or be able to convert to it/
71
+ )
72
+ end
73
+ end
@@ -22,8 +22,8 @@ describe EnsureIt do
22
22
  expect(call_for(Array, Enumerable, Array)).to eq Array
23
23
  end
24
24
 
25
- it 'and converts string to class with string option' do
26
- expect(call_for('Array', string: true)).to eq Array
25
+ it 'and converts string to class with strings option' do
26
+ expect(call_for('Array', strings: true)).to eq Array
27
27
  end
28
28
  end
29
29
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ensure_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Ovchinnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
11
+ date: 2014-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -142,10 +142,12 @@ files:
142
142
  - README.md
143
143
  - Rakefile
144
144
  - ensure_it.gemspec
145
+ - examples/array.rb
145
146
  - examples/symbol.rb
146
147
  - lib/ensure_it.rb
147
148
  - lib/ensure_it/config.rb
148
149
  - lib/ensure_it/ensure_array.rb
150
+ - lib/ensure_it/ensure_boolean.rb
149
151
  - lib/ensure_it/ensure_class.rb
150
152
  - lib/ensure_it/ensure_float.rb
151
153
  - lib/ensure_it/ensure_hash.rb
@@ -162,6 +164,7 @@ files:
162
164
  - spec/integration/refines_spec.rb
163
165
  - spec/lib/config_spec.rb
164
166
  - spec/lib/ensure_array_spec.rb
167
+ - spec/lib/ensure_boolean_spec.rb
165
168
  - spec/lib/ensure_class_spec.rb
166
169
  - spec/lib/ensure_float_spec.rb
167
170
  - spec/lib/ensure_hash_spec.rb
@@ -197,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
200
  version: '0'
198
201
  requirements: []
199
202
  rubyforge_project:
200
- rubygems_version: 2.2.2
203
+ rubygems_version: 2.2.0
201
204
  signing_key:
202
205
  specification_version: 4
203
206
  summary: This library provides way to check and converts local variables for every-method
@@ -207,6 +210,7 @@ test_files:
207
210
  - spec/integration/refines_spec.rb
208
211
  - spec/lib/config_spec.rb
209
212
  - spec/lib/ensure_array_spec.rb
213
+ - spec/lib/ensure_boolean_spec.rb
210
214
  - spec/lib/ensure_class_spec.rb
211
215
  - spec/lib/ensure_float_spec.rb
212
216
  - spec/lib/ensure_hash_spec.rb