demand 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +45 -0
- data/README.md +73 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/demand.gemspec +32 -0
- data/lib/demand.rb +66 -0
- data/lib/demand/version.rb +3 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 71d2de856e8b5dbde272a052c9469dede2c60e3632330b6e8e7a271879189ac4
|
4
|
+
data.tar.gz: fe01af992413ecf449cf06a85c445f750c947d9a6aa388aa8d5ac6a160726048
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b493b5e0c1a82496d6c4f4b941386d48c25be30c45075f8b0fab7d4b172963d69c9a46626fbc49f5bb53bf39837459f3d8754e8a11a06e11173dbb23c36b37b
|
7
|
+
data.tar.gz: d95a634315032d81f718704890d2260dbbf9ddc89edb07b09a6536a31b06ac8faf8f95db48de7e276cbe68bcbc08bc5a01aa0b7688e9545e990453c18999d595
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
demand (0.1.0)
|
5
|
+
boolean (~> 1.0)
|
6
|
+
facets (~> 3.1)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
boolean (1.0.1)
|
12
|
+
coderay (1.1.2)
|
13
|
+
diff-lcs (1.3)
|
14
|
+
facets (3.1.0)
|
15
|
+
method_source (0.9.2)
|
16
|
+
pry (0.12.2)
|
17
|
+
coderay (~> 1.1.0)
|
18
|
+
method_source (~> 0.9.0)
|
19
|
+
rake (10.5.0)
|
20
|
+
rspec (3.8.0)
|
21
|
+
rspec-core (~> 3.8.0)
|
22
|
+
rspec-expectations (~> 3.8.0)
|
23
|
+
rspec-mocks (~> 3.8.0)
|
24
|
+
rspec-core (3.8.0)
|
25
|
+
rspec-support (~> 3.8.0)
|
26
|
+
rspec-expectations (3.8.2)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.8.0)
|
29
|
+
rspec-mocks (3.8.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.8.0)
|
32
|
+
rspec-support (3.8.0)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
bundler (~> 1.17)
|
39
|
+
demand!
|
40
|
+
pry (~> 0.12)
|
41
|
+
rake (~> 10.0)
|
42
|
+
rspec (~> 3.0)
|
43
|
+
|
44
|
+
BUNDLED WITH
|
45
|
+
1.17.2
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Demand (Ruby Gem)
|
2
|
+
|
3
|
+
**Adds a top level `demand(variable)` method to return a variable if it's present** and optionally of the right type. Otherwise, a default or nil is returned.
|
4
|
+
|
5
|
+
**`demand()` replaces long lines of repetitive code** to check for `nil?`/`present?`/`empty?`, etc., hard-to-read ternary operators (`?:`) and chunky `if` statements. Instead you can make a simple method call.
|
6
|
+
|
7
|
+
| So, you can... | ...instead of stuff like |
|
8
|
+
| --------------------------- | ---------------------------------------------- |
|
9
|
+
| `demand(x)` | `(x.present? ? x : nil)` |
|
10
|
+
| `a = demand(x, [0], Array)` | `a = (x.is_a?(Array) && !x.empty?) ? x : [0]` |
|
11
|
+
| `demand(x) {\|x\| a = x}` | `a = x if !x.nil? && x.strip.length > 0` |
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require 'demand'
|
17
|
+
```
|
18
|
+
|
19
|
+
### If variable present, return it
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
x = false
|
23
|
+
y = ' '
|
24
|
+
|
25
|
+
demand(x) #=> false (that is, x)
|
26
|
+
demand(y) #=> nil
|
27
|
+
demand(y, 'Not present') #=> 'Not present'
|
28
|
+
```
|
29
|
+
|
30
|
+
By *present* here we mean that:
|
31
|
+
|
32
|
+
* The variable is not equal to `nil`
|
33
|
+
* If it is an Array or Hash, it isn't empty
|
34
|
+
* If it is a String, it isn't empty or just whitespace
|
35
|
+
|
36
|
+
(This uses the [Facets](https://github.com/rubyworks/facets) gem's `blank?` method, but overrides it evaluating `false` as blank.)
|
37
|
+
|
38
|
+
If you actually want your variable to be `nil` (i.e. you want the default value when the variable is *not* nil), specify the class you're looking for as `NilClass`):
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
expected_to_be_nil = nil
|
42
|
+
|
43
|
+
demand(expected_to_be_nil, 'Not nil') #=> 'Not nil'
|
44
|
+
demand(expected_to_be_nil, 'Not nil', NilClass) #=> nil (that is, expected_to_be_nil)
|
45
|
+
```
|
46
|
+
|
47
|
+
If you want an Array or Hash and don't mind if an empty one is passed, just specify an empty Array or Hash as the default value.
|
48
|
+
|
49
|
+
### If variable present and of type, return it
|
50
|
+
|
51
|
+
If you specify a Class or Module in the third parameter, the variable must be of this Class or include this Module.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
x = 'Hello world'
|
55
|
+
y = false
|
56
|
+
|
57
|
+
demand(x, 'Not the right type', String) #=> 'Hello world' (that is, x)
|
58
|
+
demand(y, 'Not the right type', String) #=> 'Not the right type'
|
59
|
+
demand(y, 'Not the right type', Boolean) #=> false (that is, y)
|
60
|
+
```
|
61
|
+
|
62
|
+
The type `Boolean` is also made available when using this gem (via the [Boolean](https://github.com/RISCfuture/boolean) gem). This has the effect that `true` and `false` include `Boolean`, so we can check if something `is_a?(Boolean)` which will pass just for `true` and `false` values.
|
63
|
+
|
64
|
+
### If variable, yield and run block
|
65
|
+
|
66
|
+
If a block is specified, this will run only if the variable passes all the conditions. The variable is yielded to the block and also still returned by the method.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
x = 5
|
70
|
+
|
71
|
+
demand(x, nil, Integer) {|x| puts x * 2 } #=> returns: 5; puts: 10
|
72
|
+
demand(x, nil, String) {|x| puts 'Hello' } #=> nil; puts is not run
|
73
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "demand"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/demand.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "demand/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "demand"
|
8
|
+
spec.version = Demand::VERSION
|
9
|
+
spec.authors = ["Convincible"]
|
10
|
+
spec.email = ["development@convincible.media"]
|
11
|
+
|
12
|
+
spec.summary = "Adds a top level demand(variable) method to return a variable if it's present and optionally of the right type. Otherwise, a default or nil is returned."
|
13
|
+
spec.description = "Adds a top level demand(variable) method to return a variable if it's present and optionally of the right type. Otherwise, a default or nil is returned. demand() replaces long lines of repetitive code to check for nil?/present?/empty?, etc., hard-to-read ternary operators (?:) and if statements. A block can also be specified, which only runs (with the variable) if the checks pass."
|
14
|
+
spec.homepage = "https://github.com/ConvincibleMedia/ruby-gem-demand"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "facets", "~> 3.1"
|
26
|
+
spec.add_dependency "boolean", "~> 1.0"
|
27
|
+
|
28
|
+
spec.add_development_dependency "pry", "~> 0.12"
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
end
|
data/lib/demand.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'demand/version'
|
2
|
+
require 'facets/kernel/blank'
|
3
|
+
require 'boolean'
|
4
|
+
# require 'pry'
|
5
|
+
|
6
|
+
module Demand
|
7
|
+
|
8
|
+
# Make Demand::demand() available
|
9
|
+
extend self
|
10
|
+
|
11
|
+
# Checks if a passed variable is present and as expected. If so, returns and optionally yields it. Otherwise, a default is returned. The check will fail for empty arrays, hashes and strings (including whitespace strings).
|
12
|
+
#
|
13
|
+
# @param var The variable you wish to check.
|
14
|
+
# @param default The return value you want if the check fails.
|
15
|
+
# @param type [Class, Module] Variable must be of this class or include this module for the check to pass. The module 'Boolean' can be used, to mean the value must be true or false.
|
16
|
+
#
|
17
|
+
# @return The original variable if the check passes. Otherwise, the default value is returned.
|
18
|
+
# @yield [var] If a block is given and the check passes, the original variable is also yielded to the block.
|
19
|
+
#
|
20
|
+
# @note If you want the check to pass just if the variable is nil, specify type = NilClass
|
21
|
+
#
|
22
|
+
def demand(var, default = nil, type = nil)
|
23
|
+
|
24
|
+
# If type specified, must either be a class or module
|
25
|
+
# Otherwise, get the class of whatever was passed
|
26
|
+
if (type != nil)
|
27
|
+
if (type.is_a?(Class) || type.is_a?(Module))
|
28
|
+
t = type
|
29
|
+
else
|
30
|
+
t = type.class
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Check the var
|
35
|
+
begin
|
36
|
+
# Edge case - you want the variable to be of type NilClass
|
37
|
+
if var == nil
|
38
|
+
unless t == NilClass
|
39
|
+
return default
|
40
|
+
end
|
41
|
+
# Is the variable blank? - not including false
|
42
|
+
elsif !(var.present? || var == false) # Override false == blank
|
43
|
+
return default
|
44
|
+
# Variable is not blank
|
45
|
+
# Do we need to check its class too?
|
46
|
+
elsif (t != nil)
|
47
|
+
unless var.is_a?(t)
|
48
|
+
return default
|
49
|
+
end
|
50
|
+
end
|
51
|
+
rescue
|
52
|
+
return default
|
53
|
+
end
|
54
|
+
|
55
|
+
# All checks have passed by this point
|
56
|
+
yield(var) if block_given?
|
57
|
+
|
58
|
+
# Original variable returned
|
59
|
+
return var
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
# Make demand() available at top level when requiring this gem
|
66
|
+
extend Demand
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: demand
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Convincible
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: facets
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: boolean
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.17'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.17'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description: Adds a top level demand(variable) method to return a variable if it's
|
98
|
+
present and optionally of the right type. Otherwise, a default or nil is returned.
|
99
|
+
demand() replaces long lines of repetitive code to check for nil?/present?/empty?,
|
100
|
+
etc., hard-to-read ternary operators (?:) and if statements. A block can also be
|
101
|
+
specified, which only runs (with the variable) if the checks pass.
|
102
|
+
email:
|
103
|
+
- development@convincible.media
|
104
|
+
executables: []
|
105
|
+
extensions: []
|
106
|
+
extra_rdoc_files: []
|
107
|
+
files:
|
108
|
+
- ".gitignore"
|
109
|
+
- ".rspec"
|
110
|
+
- ".travis.yml"
|
111
|
+
- Gemfile
|
112
|
+
- Gemfile.lock
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- demand.gemspec
|
118
|
+
- lib/demand.rb
|
119
|
+
- lib/demand/version.rb
|
120
|
+
homepage: https://github.com/ConvincibleMedia/ruby-gem-demand
|
121
|
+
licenses: []
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubygems_version: 3.0.0
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Adds a top level demand(variable) method to return a variable if it's present
|
142
|
+
and optionally of the right type. Otherwise, a default or nil is returned.
|
143
|
+
test_files: []
|