demand 1.1.0 → 1.1.1

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
  SHA256:
3
- metadata.gz: dd794ffb8ce233fa1db50226078f78fd329ad64e1e49cdedc348a10313d1fbb8
4
- data.tar.gz: 467d92011b71ed5ca40af7682cc73c2bf4d544aafe2632c4325947ef19fd92ec
3
+ metadata.gz: 12153ff2fc033a84316cb6ef6dedf9b03039d4a47cdceaad96328d6592ab6893
4
+ data.tar.gz: a3a92fabd9f97fb31bc990511e1f4fcf9746e752bfa758e97bc87962509ed6d2
5
5
  SHA512:
6
- metadata.gz: ef5d6e480a9b891a3f080786432c650910a0003d670f9572fa207e000c1f8a45a1b06189543fb6e694a615dc5c4d003791aea2036e252406f5619e288cf8a1a6
7
- data.tar.gz: fc98de71b7b57d4b9034f87b56e002a9e4ff0c4b5cfadd7cd2b0f9cab53f0aada0fa8581846b5f6d21f3e9ae51bdbbfdec5a7d6bd648186b58083e14c3d3d3bf
6
+ metadata.gz: 43fb280c392f9c5195439abac97b9246e8ec083d8d52ca3cf645ef672549d4bfb44df3b4f87c27dc3a0da8a085972b2b18cd335a2f83db69799ddde814300af6
7
+ data.tar.gz: 76a6e4779fe782e830bf84c2ad97989d81a4c2ee018bcf143b29ae0556942fa76e901f392e49c0adaa06925ba2482854d6cfd451a51cb5700f84296bc772a4ac
@@ -1,67 +1,83 @@
1
- require 'active_support/core_ext/object/blank'
2
- require 'boolean'
3
- # require 'pry'
4
-
5
- module Demand
6
- YIELD_DEFAULT = false
7
- RETURN_YIELD = false
8
- attr_accessor :YIELD_DEFAULT
9
- attr_accessor :RETURN_YIELD
10
- end
11
-
12
- # 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). If you want the check to pass just if the variable is nil, specify type = NilClass
13
- #
14
- # @param var The variable you wish to check.
15
- # @param default The return value you want if the check fails.
16
- # @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.
17
- #
18
- # @return The original variable if the check passes. Otherwise, the default value is returned.
19
- # @yield [var] If a block is given and the check passes, the original variable is also yielded to the block.
20
- #
21
- # @note This is added as a top level method to the global scope when requiring this gem.
22
- #
23
- def demand(var, default = nil, type = nil)
24
-
25
- # If type specified, must either be a class or module
26
- # Otherwise, get the class of whatever was passed
27
- if (type != nil)
28
- if (type.is_a?(Class) || type.is_a?(Module))
29
- t = type
30
- else
31
- t = type.class
32
- end
33
- end
34
-
35
- # Check the var
36
- result = var; check = true
37
- begin
38
- # Edge case - you want the variable to be of type NilClass
39
- if var == nil
40
- unless t == NilClass
41
- result = default; check = false
42
- end
43
- # Is the variable blank? - not including false
44
- elsif !(var.present? || var == false) # Override false == blank
45
- result = default; check = false
46
- # Variable is not blank
47
- # Do we need to check its class too?
48
- elsif (t != nil)
49
- unless var.is_a?(t)
50
- result = default; check = false
51
- end
52
- end
53
- rescue
54
- result = default; check = false
55
- end
56
-
57
- # All checks have passed by this point
58
- if block_given? && (check || Demand::YIELD_DEFAULT)
59
- if Demand::RETURN_YIELD
60
- return yield(result)
61
- else
62
- yield(result)
63
- end
64
- end
65
- return result
66
-
67
- end
1
+ require 'boolean'
2
+
3
+ module Demand
4
+ OPTIONS = {
5
+ # If true, a passed block will still run if the presence check on your variable fails. The default value will be yielded to the block instead.
6
+ yield_default: false,
7
+ # If true, the return value of the passed block (if run) will be the return value for the main method itself.
8
+ return_yield: false
9
+ }
10
+ attr_accessor :OPTIONS
11
+ end
12
+
13
+ # 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). If you want the check to pass just if the variable is nil, specify type = NilClass
14
+ #
15
+ # @param var The variable you wish to check.
16
+ # @param default The return value you want if the check fails.
17
+ # @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.
18
+ #
19
+ # @return The original variable if the check passes. Otherwise, the default value is returned.
20
+ # @yield [var] If a block is given and the check passes, the original variable is also yielded to the block.
21
+ #
22
+ # @note This is added as a top level method to the global scope when requiring this gem.
23
+ #
24
+ def demand(var, default = nil, type = nil)
25
+
26
+ if (type != nil)
27
+ # If type specified, must either be a class or module
28
+ if (type.is_a?(Class) || type.is_a?(Module))
29
+ t = type
30
+ else
31
+ # Otherwise, get the class of whatever was passed
32
+ t = type.class
33
+ end
34
+
35
+ # Is this an anonymous class (e.g. anonymous struct)? - not much use
36
+ if t.name.to_s == '' && type.superclass != nil
37
+ # Lets use the class it's a type of, instead (e.g. Struct)
38
+ t = type.superclass
39
+ end
40
+ end
41
+
42
+ # Check the var
43
+ result = var; check = true # has the check passed?
44
+ begin
45
+ # Is the variable nil?
46
+ if var == nil
47
+ # Do you want the variable to be nil? (edge case)
48
+ unless t == NilClass
49
+ # No - so the check fails
50
+ result = default; check = false
51
+ end
52
+ # Is the variable blank? - not including false
53
+ elsif (
54
+ ( var.respond_to?(:nil?) && !!var.nil? ) || # responds to nil truthily
55
+ ( var.is_a?(NilClass) ) || # is a kind of nil
56
+ ( var.respond_to?(:empty?) && !!var.empty? ) || # is empty/empty string
57
+ ( var.is_a?(String) && var.strip.empty? ) # is just whitespace
58
+ )
59
+ # Variable is blank
60
+ result = default; check = false
61
+ # Variable is not blank
62
+ # Has a class been specified that the variable must be a type of?
63
+ elsif (t != nil)
64
+ unless var.is_a?(t)
65
+ # Variable is not of correct type
66
+ result = default; check = false
67
+ end
68
+ end
69
+ rescue
70
+ result = default; check = false
71
+ end
72
+
73
+ # All checks have passed by this point
74
+ if block_given? && (check || Demand::OPTIONS[:yield_default])
75
+ if Demand::OPTIONS[:return_yield]
76
+ return yield(result)
77
+ else
78
+ yield(result)
79
+ end
80
+ end
81
+ return result
82
+
83
+ end
@@ -1,3 +1,3 @@
1
- module Demand
2
- VERSION = "1.1.0"
3
- end
1
+ module Demand
2
+ VERSION = "1.1.1"
3
+ end
metadata CHANGED
@@ -1,99 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: demand
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Convincible
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-16 00:00:00.000000000 Z
11
+ date: 2019-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: boolean
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 5.2.2
19
+ version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 5.2.2
26
+ version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: boolean
28
+ name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.0.1
34
- type: :runtime
33
+ version: '2.0'
34
+ type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.0.1
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: pry
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
73
  - - ">="
46
74
  - !ruby/object:Gem::Version
47
- version: '0.12'
75
+ version: '0'
48
76
  type: :development
49
77
  prerelease: false
50
78
  version_requirements: !ruby/object:Gem::Requirement
51
79
  requirements:
52
80
  - - ">="
53
81
  - !ruby/object:Gem::Version
54
- version: '0.12'
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
- name: bundler
84
+ name: ice_nine
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - ">="
60
88
  - !ruby/object:Gem::Version
61
- version: '1.17'
89
+ version: '0'
62
90
  type: :development
63
91
  prerelease: false
64
92
  version_requirements: !ruby/object:Gem::Requirement
65
93
  requirements:
66
94
  - - ">="
67
95
  - !ruby/object:Gem::Version
68
- version: '1.17'
96
+ version: '0'
69
97
  - !ruby/object:Gem::Dependency
70
- name: rake
98
+ name: pry
71
99
  requirement: !ruby/object:Gem::Requirement
72
100
  requirements:
73
101
  - - ">="
74
102
  - !ruby/object:Gem::Version
75
- version: '10.0'
103
+ version: '0'
76
104
  type: :development
77
105
  prerelease: false
78
106
  version_requirements: !ruby/object:Gem::Requirement
79
107
  requirements:
80
108
  - - ">="
81
109
  - !ruby/object:Gem::Version
82
- version: '10.0'
110
+ version: '0'
83
111
  - !ruby/object:Gem::Dependency
84
- name: rspec
112
+ name: pry-nav
85
113
  requirement: !ruby/object:Gem::Requirement
86
114
  requirements:
87
115
  - - ">="
88
116
  - !ruby/object:Gem::Version
89
- version: '3.0'
117
+ version: '0'
90
118
  type: :development
91
119
  prerelease: false
92
120
  version_requirements: !ruby/object:Gem::Requirement
93
121
  requirements:
94
122
  - - ">="
95
123
  - !ruby/object:Gem::Version
96
- version: '3.0'
124
+ version: '0'
97
125
  description: Return a variable if it's present (and optionally of the right type),
98
126
  otherwise a default or nil. Adds a top level demand() method, which replaces long
99
127
  lines of repetitive code to check for nil?/present?/empty?, etc., hard-to-read ternary
@@ -105,16 +133,6 @@ executables: []
105
133
  extensions: []
106
134
  extra_rdoc_files: []
107
135
  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
136
  - lib/demand.rb
119
137
  - lib/demand/version.rb
120
138
  homepage: https://github.com/ConvincibleMedia/ruby-gem-demand
@@ -128,14 +146,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
146
  requirements:
129
147
  - - ">="
130
148
  - !ruby/object:Gem::Version
131
- version: '0'
149
+ version: 2.5.0
132
150
  required_rubygems_version: !ruby/object:Gem::Requirement
133
151
  requirements:
134
152
  - - ">="
135
153
  - !ruby/object:Gem::Version
136
154
  version: '0'
137
155
  requirements: []
138
- rubygems_version: 3.0.0
156
+ rubygems_version: 3.0.6
139
157
  signing_key:
140
158
  specification_version: 4
141
159
  summary: Return a variable if it's present (and optionally of the right type), otherwise
data/.gitignore DELETED
@@ -1,17 +0,0 @@
1
- .git
2
- .env
3
- *.lnk
4
- *.db
5
- *.ini
6
-
7
- /.bundle/
8
- /.yardoc
9
- /_yardoc/
10
- /coverage/
11
- /doc/
12
- /pkg/
13
- /spec/reports/
14
- /tmp/
15
-
16
- # rspec failure tracking
17
- .rspec_status
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.4.3
7
- before_install: gem install bundler -v 1.17.2
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in demand.gemspec
6
- gemspec
@@ -1,56 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- demand (1.1.0)
5
- activesupport (~> 5.2.2)
6
- boolean (~> 1.0.1)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activesupport (5.2.2)
12
- concurrent-ruby (~> 1.0, >= 1.0.2)
13
- i18n (>= 0.7, < 2)
14
- minitest (~> 5.1)
15
- tzinfo (~> 1.1)
16
- boolean (1.0.1)
17
- coderay (1.1.2)
18
- concurrent-ruby (1.1.4)
19
- diff-lcs (1.3)
20
- i18n (1.5.2)
21
- concurrent-ruby (~> 1.0)
22
- method_source (0.9.2)
23
- minitest (5.11.3)
24
- pry (0.12.2)
25
- coderay (~> 1.1.0)
26
- method_source (~> 0.9.0)
27
- rake (12.3.2)
28
- rspec (3.8.0)
29
- rspec-core (~> 3.8.0)
30
- rspec-expectations (~> 3.8.0)
31
- rspec-mocks (~> 3.8.0)
32
- rspec-core (3.8.0)
33
- rspec-support (~> 3.8.0)
34
- rspec-expectations (3.8.2)
35
- diff-lcs (>= 1.2.0, < 2.0)
36
- rspec-support (~> 3.8.0)
37
- rspec-mocks (3.8.0)
38
- diff-lcs (>= 1.2.0, < 2.0)
39
- rspec-support (~> 3.8.0)
40
- rspec-support (3.8.0)
41
- thread_safe (0.3.6)
42
- tzinfo (1.2.5)
43
- thread_safe (~> 0.1)
44
-
45
- PLATFORMS
46
- ruby
47
-
48
- DEPENDENCIES
49
- bundler (>= 1.17)
50
- demand!
51
- pry (>= 0.12)
52
- rake (>= 10.0)
53
- rspec (>= 3.0)
54
-
55
- BUNDLED WITH
56
- 1.17.2
data/README.md DELETED
@@ -1,83 +0,0 @@
1
- # Demand (Ruby Gem)
2
-
3
- **Return a variable if it's present** (and optionally of the right type), otherwise, a default or nil. Adds a top level `demand()` method, which 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:
4
-
5
- | So, you can... | ...instead of stuff like |
6
- | --------------------------- | ---------------------------------------------- |
7
- | `demand(x)` | `(x.present? ? x : nil)` |
8
- | `demand(x, y)` | `(x.present? ? x : y)` |
9
- | `a = demand(x, [0], Array)` | `a = (x.is_a?(Array) && !x.empty?) ? x : [0]` |
10
- | `demand(x) {\|x\| a = x}` | `a = x if !x.nil? && x.strip.length > 0` |
11
-
12
- ## Usage
13
-
14
- ```ruby
15
- require 'demand'
16
- ```
17
-
18
- ### If variable present, return it
19
-
20
- ```ruby
21
- x = false
22
- y = ' '
23
-
24
- demand(x) #=> false (that is, x)
25
- demand(y) #=> nil
26
- demand(y, 'Not present') #=> 'Not present'
27
- ```
28
-
29
- By *present* here we mean that:
30
-
31
- * The variable is not equal to `nil`
32
- * If it is an Array or Hash, it isn't empty
33
- * If it is a String, it isn't empty or just whitespace
34
-
35
- (This uses ActiveSupport's `blank?` method, but overrides it evaluating `false` as blank.)
36
-
37
- 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`):
38
-
39
- ```ruby
40
- expected_to_be_nil = nil
41
-
42
- demand(expected_to_be_nil, 'Not nil') #=> 'Not nil'
43
- demand(expected_to_be_nil, 'Not nil', NilClass) #=> nil (that is, expected_to_be_nil)
44
- ```
45
-
46
- 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.
47
-
48
- ### If variable present and of type, return it
49
-
50
- If you specify a Class or Module in the third parameter, the variable must be of this Class or include this Module.
51
-
52
- ```ruby
53
- x = 'Hello world'
54
- y = false
55
-
56
- demand(x, 'Not the right type', String) #=> 'Hello world' (that is, x)
57
- demand(y, 'Not the right type', String) #=> 'Not the right type'
58
- demand(y, 'Not the right type', Boolean) #=> false (that is, y)
59
- ```
60
-
61
- 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.
62
-
63
- ### If variable, yield and run block
64
-
65
- 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.
66
-
67
- ```ruby
68
- x = 5
69
-
70
- demand(x, nil, Integer) {|x| puts x * 2 } #=> returns: 5; puts: 10
71
- demand(x, nil, String) {|x| puts 'Hello' } #=> nil; puts is not run
72
- ```
73
-
74
- ## Options
75
-
76
- Not really recommended, but you can adjust how the `demand()` method works by setting `Demand::YIELD_DEFAULT` and `Demand::RETURN_YIELD`.
77
-
78
- | Option | Default | Explanation |
79
- | ------------- | ------- | ----------- |
80
- | YIELD_DEFAULT | `false` | If `true`, a passed block will still run if the presence check on your variable fails. The default value will be yielded to the block instead. |
81
- | RETURN_YIELD | `false` | If `true`, the return value of the passed block (if run) will be the return value for the main method itself. |
82
-
83
- Once set, these switches change how all further calls to `demand()` behave. The switches are included for flexibility to developer preferences, but use with caution: things could get confusing quickly. Probably if you feel like you need these, `demand()` may not be the right tool.
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
@@ -1,14 +0,0 @@
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 DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,32 +0,0 @@
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 = "Return a variable if it's present (and optionally of the right type), otherwise a default or nil. Adds a top level demand() method."
13
- spec.description = "Return a variable if it's present (and optionally of the right type), otherwise a default or nil. Adds a top level demand() method, which 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 'activesupport', '~>5.2.2'
26
- spec.add_dependency 'boolean', '~>1.0.1'
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