-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to concrete Success/Failure result classes
Within an operation data is now gathered on a separate Data object that’s passed to the result on completion.
- Loading branch information
1 parent
4f036f9
commit 67c4605
Showing
8 changed files
with
402 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module Operatic | ||
class Data | ||
# Generate a subclass of {Data} with named +attrs+ accessors. This wouldn't | ||
# normally be called directly, see {ClassMethods#data_attr} for example | ||
# usage. | ||
# | ||
# @param attrs [Array<Symbol>] a list of convenience data accessors. | ||
def self.define(*attrs) | ||
Class.new(self) do | ||
attrs.each do |name| | ||
define_method name do | ||
self[name] | ||
end | ||
|
||
define_method "#{name}=" do |value| | ||
self[name] = value | ||
end | ||
end | ||
end | ||
end | ||
|
||
# @param kwargs [Hash<Symbol, anything>] | ||
def initialize(**kwargs) | ||
@data = kwargs | ||
end | ||
|
||
# Return the value for +key+. | ||
# | ||
# @param key [Symbol] | ||
# | ||
# @return [anything] | ||
def [](key) | ||
@data[key] | ||
end | ||
|
||
# Set data on the result. | ||
# | ||
# @param key [Symbol] | ||
# @param value [anything] | ||
def []=(key, value) | ||
@data[key] = value | ||
end | ||
|
||
# @return [self] | ||
def freeze | ||
@data.freeze | ||
super | ||
end | ||
|
||
# @param hash [Hash<Symbol, anything>] | ||
# | ||
# @return [Data] | ||
def merge(hash) | ||
self.class.new.tap { |other| | ||
other.set_data(@data) | ||
other.set_data(hash) | ||
} | ||
end | ||
|
||
# @return [Hash<Symbol, anything>] | ||
def to_h | ||
@data | ||
end | ||
|
||
protected | ||
def set_data(data) | ||
data.each do |key, value| | ||
@data[key] = value | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.