inquisitive 3.0.0 → 3.1.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: 80d236d855c6c33b3ec287fcd2c3a302388608f3
4
- data.tar.gz: bd301adffafcbc757db422e5e0adeb780a06b5e4
3
+ metadata.gz: e22f450e21c1139aff9b5587f6da20f9964ed3f5
4
+ data.tar.gz: 26d9737845ee0028316664d7715ec0b2457619b1
5
5
  SHA512:
6
- metadata.gz: e19cf79e18d77f23a58ee9c6dfc6d87af7262e4fd828f271e9cfe3694c67b38d11ea6c10764a14b6483ce037accac1491348a9a25ef6351c5d7332b6f0cd1d6a
7
- data.tar.gz: 5e287d046ac747a2f27391bb6c38046b33c3123c93e953546948a06d16500cacde4629fcb77b66c39cfee4941eebb8e1d8df6ba7e4ca41ae6313d7c5989cadd3
6
+ metadata.gz: 213264cbb822921b64d7393fcdeee3c135dbee7e4ae4ed93d9150b01354fe6f0803d4028cc2ad64147ccf47239de5c2ec49121a60338911beec8dc4d697dc561
7
+ data.tar.gz: eea7a7c74d2a3288e6902b1012bccefbaec395e7847007d1dd87fa1cfaa109bdea7d9bcf00ea41af33f36115c504e28059bf6be4ceac90f2b7750aaa0c877e65
data/README.md CHANGED
@@ -301,10 +301,94 @@ MyApp.env
301
301
  #=> "development"
302
302
  MyApp.env.development?
303
303
  #=> true
304
+
304
305
  MyApp.env.production?
305
306
  #=> false
306
307
  ```
307
308
 
309
+ #### Presence
310
+
311
+ Environment inquirers can have explicit presence checks, circumventing a common pitfall when reasoning about environment variables. Borrowing from the example above:
312
+
313
+ ```ruby
314
+ ENV['STUB__AUTHENTICATION'] = 'false'
315
+ class MyApp
316
+ extend Inquisitive::Environment
317
+ inquires_about 'STUB'
318
+ end
319
+
320
+ MyApp.stub.authentication
321
+ #=> "false"
322
+ MyApp.stub.authentication?
323
+ #=> true
324
+ MyApp.stub.authentication.true?
325
+ #=> false
326
+ ```
327
+
328
+ It's common to use the presence of environment variables as runtime booleans. This is frequently done by setting the environment variable to the string `"true"` when you want it to be true, and not at all otherwise. As demonstrated, this pattern can lead to ambiguity when the string is other values.
329
+
330
+ By default such variables will be parsed as an `Inquisitive::String`, so predicate methods will return true whatever their contents, as long as they exist. You can bind the predicate method tighter to an explicit value if you prefer:
331
+
332
+ ```ruby
333
+ ENV['STUB_AUTHENTICATION'] = 'false'
334
+ ENV['STUB_REGISTRATION'] = 'true'
335
+ class MyApp
336
+ extend Inquisitive::Environment
337
+ inquires_about 'STUB_AUTHENTICATION', present_if: 'true'
338
+ inquires_about 'STUB_REGISTRATION', present_if: 'true'
339
+ end
340
+
341
+ MyApp.stub_authentication
342
+ #=> "false"
343
+ MyApp.stub_authentication?
344
+ #=> false
345
+
346
+ MyApp.stub_registration
347
+ #=> "true"
348
+ MyApp.stub_registration?
349
+ #=> true
350
+ ```
351
+
352
+ This only works on top-level inquirers, so there's no way to get our nested `MyApp.stubbed.authentication?` to behave as expected. Prefer `MyApp.stubbed.authentication.true?` instead.
353
+
354
+ The `present_if` check uses `===` under the covers for maximum expressiveness, so you can also use it to match against regexs, classes, and other constructs.
355
+
356
+ ##### Truthy Booleans
357
+
358
+ `Inquisitive::Environment.truthy` contains a regex useful for reading booleans from environment variables.
359
+
360
+ ```ruby
361
+ ENV['NO'] = 'no'
362
+ ENV['YES'] = 'yes'
363
+ ENV['TRUTHY'] = 'TrUe'
364
+ ENV['FALSEY'] = 'FaLsE'
365
+ ENV['BOOLEAN'] = '1'
366
+ ENV['BOOLENOPE'] = '0'
367
+ class MyCli
368
+ extend Inquisitive::Environment
369
+ inquires_about 'NO', present_if: truthy
370
+ inquires_about 'YES', present_if: truthy
371
+ inquires_about 'TRUTHY', present_if: truthy
372
+ inquires_about 'FALSEY', present_if: truthy
373
+ inquires_about 'BOOLEAN', present_if: truthy
374
+ inquires_about 'BOOLENOPE', present_if: truthy
375
+ end
376
+
377
+ MyApp.no?
378
+ #=> false
379
+ MyApp.yes?
380
+ #=> true
381
+
382
+ MyApp.truthy?
383
+ #=> true
384
+ MyApp.falsey?
385
+ #=> false
386
+
387
+ MyApp.boolean?
388
+ #=> true
389
+ MyApp.boolenope?
390
+ ```
391
+
308
392
  #### Inquiry mode
309
393
 
310
394
  Environment inquirers have three configurable modes, defaulting to `:static`.
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "inquisitive"
7
- spec.version = "3.0.0"
7
+ spec.version = "3.1.0"
8
8
  spec.authors = ["Chris Keele"]
9
9
  spec.email = ["dev@chriskeele.com"]
10
10
  spec.summary = "Predicate methods for those curious about their datastructures."
@@ -2,6 +2,10 @@ module Inquisitive
2
2
  module Environment
3
3
  include Inquisitive
4
4
 
5
+ def truthy
6
+ /true|yes|1/i
7
+ end
8
+
5
9
  def inquires_about(env_var, opts={})
6
10
 
7
11
  env_accessor = opts.fetch(:with, env_var.downcase[/(.*?)(?=(?:_$|$))/])
@@ -31,6 +35,19 @@ module Inquisitive
31
35
 
32
36
  end
33
37
 
38
+ present_if = opts.fetch(:present_if, nil)
39
+
40
+ @__env_presence__ ||= HashWithIndifferentAccess.new
41
+ @__env_presence__["#{env_accessor}?"] = present_if if present_if
42
+
43
+ define_singleton_method :"#{env_accessor}?" do
44
+ if @__env_presence__.has_key? __method__
45
+ @__env_presence__[__method__] === send(predication(__method__))
46
+ else
47
+ Inquisitive.present? send(predication(__method__))
48
+ end
49
+ end
50
+
34
51
  end
35
52
 
36
53
  private
@@ -2,6 +2,11 @@ require 'test_helper'
2
2
 
3
3
  class InquisitiveEnvironmentTest < EnvironmentTest
4
4
 
5
+ def test_missing_variable_predicates
6
+ App.inquires_about 'DOES_NOT_EXIST', with: :exists
7
+ refute App.exists?
8
+ end
9
+
5
10
  def test_autonaming_of_inquirers
6
11
  App.inquires_about 'NAME_NOT_SPECIFIED'
7
12
  assert App.respond_to? :name_not_specified
@@ -16,4 +21,30 @@ class InquisitiveEnvironmentTest < EnvironmentTest
16
21
  assert App.defaults_to.static?
17
22
  end
18
23
 
24
+ def test_custom_string_presence
25
+ ENV['AUTHORIZABLE'] = 'false'
26
+ App.inquires_about 'AUTHORIZABLE', present_if: 'true'
27
+ refute App.authorizable?
28
+ end
29
+
30
+ def test_custom_regex_presence
31
+ ENV['AUTHORIZABLE'] = 'not at all'
32
+ App.inquires_about 'AUTHORIZABLE', present_if: /yes/
33
+ refute App.authorizable?
34
+ end
35
+
36
+ def test_custom_class_presence
37
+ ENV['AUTHORIZABLE'] = 'not at all'
38
+ App.inquires_about 'AUTHORIZABLE', present_if: Array
39
+ refute App.authorizable?
40
+ end
41
+
42
+ %w[true True TrUe TRUE yes Yes YeS YES 1].each do |truthy_var|
43
+ define_method :"test_truthy_var_#{truthy_var}" do
44
+ ENV['TRUTHY'] = truthy_var
45
+ App.inquires_about 'TRUTHY', present_if: App.truthy
46
+ assert App.truthy?
47
+ end
48
+ end
49
+
19
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inquisitive
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Keele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-22 00:00:00.000000000 Z
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler