Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/errgonomic/result.rb,
lib/errgonomic/type.rb,
lib/errgonomic/presence.rb,
lib/errgonomic/core_ext/blank.rb
Overview
Introduce certain helper methods into the Object class.
Instance Method Summary collapse
-
#assert_result! ⇒ Object
Lacking static typing, we are going to want to make it easy to enforce at runtime that a given object is a Result.
-
#blank? ⇒ true, false
An object is blank if it’s false, empty, or a whitespace string.
-
#blank_or(value) ⇒ Object
Returns the receiver if it is blank, otherwise returns the given value.
-
#blank_or_else(&block) ⇒ Object
Returns the receiver if it is blank, otherwise returns the result of the block.
-
#blank_or_raise!(message) ⇒ Object
(also: #blank_or_raise)
Returns the receiver if it is blank, otherwise raises a NotPresentError.
-
#not_type_or_raise!(type, message = nil) ⇒ Object
Returns the receiver if it does not match the expected type, otherwise raises a TypeMismatchError.
-
#presence ⇒ Object
Returns the receiver if it’s present otherwise returns
nil
. -
#present? ⇒ true, false
An object is present if it’s not blank.
-
#present_or(value) ⇒ Object
Returns the receiver if it is present, otherwise returns the given value.
-
#present_or_else(&block) ⇒ Object
Returns the receiver if it is present, otherwise returns the result of the block.
-
#present_or_raise!(message) ⇒ Object
(also: #present_or_raise)
Returns the receiver if it is present, otherwise raises a NotPresentError.
-
#result? ⇒ Boolean
Convenience method to indicate whether we are working with a result.
-
#type_or(type, default) ⇒ Object
Returns the receiver if it matches the expected type, otherwise returns the default value.
-
#type_or_else(type, &block) ⇒ Object
Returns the receiver if it matches the expected type, otherwise returns the result of the block.
-
#type_or_raise!(type, message = nil) ⇒ Object
(also: #type_or_raise)
Returns the receiver if it matches the expected type, otherwise raises a TypeMismatchError.
Instance Method Details
#assert_result! ⇒ Object
Lacking static typing, we are going to want to make it easy to enforce at runtime that a given object is a Result.
301 302 303 304 305 |
# File 'lib/errgonomic/result.rb', line 301 def assert_result! return true if result? raise Errgonomic::ResultRequiredError end |
#blank? ⇒ true, false
An object is blank if it’s false, empty, or a whitespace string. For example, nil
, ”, ‘ ’, [], {}, and false
are all blank.
This simplifies
!address || address.empty?
to
address.blank?
18 19 20 |
# File 'lib/errgonomic/core_ext/blank.rb', line 18 def blank? respond_to?(:empty?) ? !!empty? : false end |
#blank_or(value) ⇒ Object
Returns the receiver if it is blank, otherwise returns the given value.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/errgonomic/presence.rb', line 70 def blank_or(value) # TBD whether this is *too* strict if value.class != self.class && self.class != NilClass raise Errgonomic::TypeMismatchError, "Type mismatch: default value is a #{value.class} but original was a #{self.class}" end return self if blank? value end |
#blank_or_else(&block) ⇒ Object
Returns the receiver if it is blank, otherwise returns the result of the block.
87 88 89 90 91 |
# File 'lib/errgonomic/presence.rb', line 87 def blank_or_else(&block) return block.call unless blank? self end |
#blank_or_raise!(message) ⇒ Object Also known as: blank_or_raise
Returns the receiver if it is blank, otherwise raises a NotPresentError. This method is helpful to enforce expectations where blank objects are required.
58 59 60 61 62 |
# File 'lib/errgonomic/presence.rb', line 58 def blank_or_raise!() raise Errgonomic::NotPresentError, unless blank? self end |
#not_type_or_raise!(type, message = nil) ⇒ Object
Returns the receiver if it does not match the expected type, otherwise raises a TypeMismatchError.
61 62 63 64 65 66 |
# File 'lib/errgonomic/type.rb', line 61 def not_type_or_raise!(type, = nil) ||= "Expected anything but #{type} but got #{self.class}" raise Errgonomic::TypeMismatchError, if is_a?(type) 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'
45 46 47 |
# File 'lib/errgonomic/core_ext/blank.rb', line 45 def presence self if present? end |
#present? ⇒ true, false
An object is present if it’s not blank.
25 26 27 |
# File 'lib/errgonomic/core_ext/blank.rb', line 25 def present? !blank? end |
#present_or(value) ⇒ Object
Returns the receiver if it is present, otherwise returns the given value. If constructing the default value is expensive, consider using present_or_else
.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/errgonomic/presence.rb', line 29 def present_or(value) # TBD whether this is *too* strict if value.class != self.class && self.class != NilClass raise Errgonomic::TypeMismatchError, "Type mismatch: default value is a #{value.class} but original was a #{self.class}" end return self if present? value end |
#present_or_else(&block) ⇒ Object
Returns the receiver if it is present, otherwise returns the result of the block. Invoking a block may be preferable to returning a default value with present_or
, if constructing the default value is expensive.
47 48 49 50 51 |
# File 'lib/errgonomic/presence.rb', line 47 def present_or_else(&block) return block.call if blank? self end |
#present_or_raise!(message) ⇒ Object Also known as: present_or_raise
Returns the receiver if it is present, otherwise raises a NotPresentError. This method is useful to enforce strong expectations, where it is preferable to fail early rather than risk causing an ambiguous error somewhere else.
15 16 17 18 19 |
# File 'lib/errgonomic/presence.rb', line 15 def present_or_raise!() raise Errgonomic::NotPresentError, if blank? self end |
#result? ⇒ Boolean
Convenience method to indicate whether we are working with a result. TBD whether we implement some stubs for the rest of the Result API; I want to think about how effectively these map to truthiness or presence.
291 292 293 |
# File 'lib/errgonomic/result.rb', line 291 def result? false end |
#type_or(type, default) ⇒ Object
Returns the receiver if it matches the expected type, otherwise returns the default value.
31 32 33 34 35 |
# File 'lib/errgonomic/type.rb', line 31 def type_or(type, default) return self if is_a?(type) default end |
#type_or_else(type, &block) ⇒ Object
Returns the receiver if it matches the expected type, otherwise returns the result of the block. Useful when constructing the default value is expensive.
46 47 48 49 50 |
# File 'lib/errgonomic/type.rb', line 46 def type_or_else(type, &block) return self if is_a?(type) block.call end |
#type_or_raise!(type, message = nil) ⇒ Object Also known as: type_or_raise
Returns the receiver if it matches the expected type, otherwise raises a TypeMismatchError. This is useful for enforcing type expectations in method arguments.
14 15 16 17 18 19 |
# File 'lib/errgonomic/type.rb', line 14 def type_or_raise!(type, = nil) ||= "Expected #{type} but got #{self.class}" raise Errgonomic::TypeMismatchError, unless is_a?(type) self end |