Class: Operatic::Result
- Inherits:
-
Object
- Object
- Operatic::Result
- Defined in:
- lib/operatic/result.rb
Class Method Summary collapse
-
.generate(*attrs) ⇒ Object
Generate a subclass of Result with named
attrsaccessors.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Read data that's attached to the result.
-
#[]=(key, value) ⇒ Object
Set data on the result.
-
#deconstruct ⇒ Array(Boolean, Hash<Symbol, anything>)
Returns an array of success and data.
-
#failure!(**data) ⇒ Object
Mark the result as a failure, optionally attach
datavia kwargs, and freeze the object so it cannot be modified further. - #failure? ⇒ Boolean
- #freeze ⇒ Object
-
#initialize ⇒ Result
constructor
A new instance of Result.
-
#success!(**data) ⇒ Object
Mark the result as a success, optionally attach
datavia kwargs, and freeze the object so it cannot be modified further. - #success? ⇒ Boolean
-
#to_h ⇒ Hash<Symbol, anything>
Returns the full (frozen) hash of data attached to the result via #success!, #failure!, or convenience accessors added with Result.generate.
Constructor Details
#initialize ⇒ Result
Returns a new instance of Result.
22 23 24 25 |
# File 'lib/operatic/result.rb', line 22 def initialize @data = {} @success = true end |
Class Method Details
.generate(*attrs) ⇒ Object
Generate a subclass of Operatic::Result with named attrs accessors. This
wouldn't normally be called directly, see ClassMethods#result_attr for
example usage.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/operatic/result.rb', line 8 def self.generate(*attrs) Class.new(self) do attrs.each do |name| define_method name do @data[name] end define_method "#{name}=" do |value| @data[name] = value end end end end |
Instance Method Details
#[](key) ⇒ Object
Read data that's attached to the result.
28 29 30 |
# File 'lib/operatic/result.rb', line 28 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
Set data on the result.
33 34 35 |
# File 'lib/operatic/result.rb', line 33 def []=(key, value) @data[key] = value end |
#deconstruct ⇒ Array(Boolean, Hash<Symbol, anything>)
Returns an array of success and data.
50 51 52 |
# File 'lib/operatic/result.rb', line 50 def deconstruct [@success, @data] end |
#failure!(**data) ⇒ Object
59 60 61 62 63 |
# File 'lib/operatic/result.rb', line 59 def failure!(**data) @success = false set_data(data) freeze end |
#failure? ⇒ Boolean
65 66 67 |
# File 'lib/operatic/result.rb', line 65 def failure? !@success end |
#freeze ⇒ Object
69 70 71 72 |
# File 'lib/operatic/result.rb', line 69 def freeze @data.freeze super end |
#success!(**data) ⇒ Object
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
success, but it's a convenient means of attaching data and of indicating
intent in the consuming code.
Note: Calling #success! or #failure! more than once will raise a
FrozenError.
83 84 85 86 87 |
# File 'lib/operatic/result.rb', line 83 def success!(**data) @success = true set_data(data) freeze end |
#success? ⇒ Boolean
89 90 91 |
# File 'lib/operatic/result.rb', line 89 def success? @success end |