lib/operatic/result.rb in operatic-0.3.1 vs lib/operatic/result.rb in operatic-0.4.0
- old
+ new
@@ -2,11 +2,11 @@
class Result
# Generate a subclass of {Result} with named +attrs+ accessors. This
# wouldn't normally be called directly, see {ClassMethods#result} for
# example usage.
#
- # @param attrs [Array<Symbol>] a list of accessors to the result's data.
+ # @param attrs [Array<Symbol>] a list of convenience data accessors.
def self.generate(*attrs)
Class.new(self) do
attrs.each do |name|
define_method name do
@data[name]
@@ -22,20 +22,17 @@
def initialize
@data = {}
@success = true
end
- # Mark the result as a failure, optionally attach data, and freeze the
- # object so it cannot be modified further.
+ # Mark the result as a failure, optionally attach +data+ via kwargs, and
+ # freeze the object so it cannot be modified further.
#
- # *Note*: After calling this method calling {#success!} or {#failure!}
- # again will raise a +FrozenError+.
- #
- # @param data [Hash<Symbol, anything>] an optional hash of data to attach
- # to the result.
- def failure!(data = nil)
- set_data(data) if data
+ # *Note*: Calling {#success!} or {#failure!} more than once will raise a
+ # +FrozenError+.
+ def failure!(**data)
+ set_data(**data)
@success = false
freeze
end
def failure?
@@ -45,24 +42,21 @@
def freeze
@data.freeze
super
end
- # Mark the result as a success, optionally attach data, and freeze the
- # object so it cannot be modified further.
+ # Mark the result as a success, optionally attach +data+ via kwargs, and
+ # freeze the object so it cannot be modified further.
#
- # Calling this is not strictly necessary as a result defaults to being a
+ # Calling this is not strictly necessary as a +Result+ defaults to being a
# success, but it's a convenient means of attaching data and of indicating
# intent in the consuming code.
#
- # *Note*: After calling this method calling {#success!} or {#failure!}
- # again will raise a +FrozenError+.
- #
- # @param data [Hash<Symbol, anything>] an optional hash of data to attach
- # to the result.
- def success!(data = nil)
- set_data(data) if data
+ # *Note*: Calling {#success!} or {#failure!} more than once will raise a
+ # +FrozenError+.
+ def success!(**data)
+ set_data(**data)
@success = true
freeze
end
def success?
@@ -70,16 +64,16 @@
end
# Returns the full (frozen) hash of data attached to the result via
# {#success!}, {#failure!}, or convenience accessors added with {.generate}.
#
- # @return [Hash]
+ # @return [Hash<Symbol, anything>]
def to_h
@data
end
private
- def set_data(data)
+ def set_data(**data)
data.each do |key, value|
@data[key] = value
end
end
end