Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/chef/validation/ext/object.rb
Overview
Copyright © 2005-2014 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Instance Method Summary collapse
-
#blank? ⇒ true, false
An object is blank if it’s false, empty, or a whitespace string.
-
#presence ⇒ Object
Returns the receiver if it’s present otherwise returns
nil. -
#present? ⇒ true, false
An object is present if it’s not blank.
Instance Method Details
#blank? ⇒ true, false
An object is blank if it’s false, empty, or a whitespace string. For example, ”, ‘ ’, nil, [], and {} are all blank.
This simplifies
address.nil? || address.empty?
to
address.blank?
35 36 37 |
# File 'lib/chef/validation/ext/object.rb', line 35 def blank? respond_to?(:empty?) ? !!empty? : !self end |
#presence ⇒ Object
Returns the receiver if it’s present otherwise returns nil. object.presence is equivalent to
object.present? ? object : nil
For example, something like
state = params[:state] if params[:state].present?
country = params[:country] if params[:country].present?
region = state || country || 'US'
becomes
region = params[:state].presence || params[:country].presence || 'US'
62 63 64 |
# File 'lib/chef/validation/ext/object.rb', line 62 def presence self if present? end |
#present? ⇒ true, false
An object is present if it’s not blank.
42 43 44 |
# File 'lib/chef/validation/ext/object.rb', line 42 def present? !blank? end |