Class: Statefully::State Abstract
- Inherits:
-
Object
- Object
- Statefully::State
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/statefully/state.rb
Overview
State is an immutable collection of fields with some convenience methods.
Defined Under Namespace
Classes: Failure, Finished, None, Success
Instance Attribute Summary collapse
-
#previous ⇒ State
readonly
Return the previous State.
Class Method Summary collapse
-
.create(**values) ⇒ type
Create an instance of State object.
Instance Method Summary collapse
- #diff ⇒ Diff
- #each ⇒ Enumerator
-
#failed? ⇒ Boolean
Check if the current State is failed.
- #fetch ⇒ Object
-
#finished? ⇒ Boolean
Check if the current State is finished.
-
#history ⇒ Array<Diff>
Return all historical changes to this State.
-
#inspect ⇒ String
Show the current State in a human-readable form.
- #key? ⇒ Boolean
- #keys ⇒ Array<Symbol>
- #none? ⇒ Boolean
-
#resolve ⇒ State
Resolve the current State.
-
#successful? ⇒ Boolean
Check if the current State is successful.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Dynamically pass unknown messages to the underlying state storage
State fields become accessible through readers, like in an OpenStruct. A single state field can be questioned for existence by having its name followed by a question mark - eg. bacon?. A single state field can be force-accessed by having its name followed by an exclamation mark - eg. bacon!.
This method reeks of :reek:TooManyStatements.
260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/statefully/state.rb', line 260 def method_missing(name, *args, &block) sym_name = name.to_sym return fetch(sym_name) if key?(sym_name) str_name = name.to_s modifier = str_name[-1] return super unless %w[? !].include?(modifier) base = str_name[0...-1].to_sym known = key?(base) return known if modifier == '?' return fetch(base) if known raise Errors::StateMissing, base end |
Instance Attribute Details
#previous ⇒ State (readonly)
Return the previous Statefully::State
22 23 24 |
# File 'lib/statefully/state.rb', line 22 def previous @previous end |
Class Method Details
.create(**values) ⇒ type
Create an instance of Statefully::State object
This is meant as the only valid way of creating Statefully::State objects.
68 69 70 |
# File 'lib/statefully/state.rb', line 68 def self.create(**values) Success.send(:new, values, previous: None.instance).freeze end |
Instance Method Details
#diff ⇒ Diff
Return a Diff between current and previous Statefully::State
79 80 81 |
# File 'lib/statefully/state.rb', line 79 def diff Diff.create(current: self, previous: previous) end |
#each ⇒ Enumerator
55 |
# File 'lib/statefully/state.rb', line 55 def_delegators :@_members, :each, :fetch, :key?, :keys |
#failed? ⇒ Boolean
Check if the current Statefully::State is failed
120 121 122 |
# File 'lib/statefully/state.rb', line 120 def failed? !successful? end |
#fetch ⇒ Object
55 |
# File 'lib/statefully/state.rb', line 55 def_delegators :@_members, :each, :fetch, :key?, :keys |
#finished? ⇒ Boolean
Check if the current Statefully::State is finished
135 136 137 |
# File 'lib/statefully/state.rb', line 135 def finished? false end |
#history ⇒ Array<Diff>
Return all historical changes to this Statefully::State
90 91 92 |
# File 'lib/statefully/state.rb', line 90 def history ([diff] + previous.history).freeze end |
#inspect ⇒ String
Show the current Statefully::State in a human-readable form
181 182 183 |
# File 'lib/statefully/state.rb', line 181 def inspect _inspect_details({}) end |
#key? ⇒ Boolean
55 |
# File 'lib/statefully/state.rb', line 55 def_delegators :@_members, :each, :fetch, :key?, :keys |
#keys ⇒ Array<Symbol>
55 |
# File 'lib/statefully/state.rb', line 55 def_delegators :@_members, :each, :fetch, :key?, :keys |
#none? ⇒ Boolean
Check if the current Statefully::State is none (a null-object of Statefully::State)
150 151 152 |
# File 'lib/statefully/state.rb', line 150 def none? false end |
#resolve ⇒ State
Resolve the current Statefully::State
Resolving will return the current Statefully::State if successful, but raise an error wrapped in a Failure. This is a convenience method inspired by monadic composition from functional languages.
170 171 172 |
# File 'lib/statefully/state.rb', line 170 def resolve self end |
#successful? ⇒ Boolean
Check if the current Statefully::State is successful
105 106 107 |
# File 'lib/statefully/state.rb', line 105 def successful? true end |