Module: Hubbado::Sequence::Sequencer
- Defined in:
- lib/hubbado/sequence/sequencer.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .build_default_substitute_module ⇒ Object
- .included(cls) ⇒ Object
-
.install_default_substitute(cls) ⇒ Object
Each sequencer gets a default ‘Substitute` module so it can be used as a dependency without bespoke test scaffolding.
Instance Method Summary collapse
- #failure(ctx, **error_attrs) ⇒ Object
- #i18n_scope ⇒ Object
-
#pipeline(ctx, &block) ⇒ Object
Builds a Pipeline that auto-dispatches blockless ‘step(:foo)` calls to `self.foo(ctx)`.
Class Method Details
.build_default_substitute_module ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hubbado/sequence/sequencer.rb', line 22 def self.build_default_substitute_module Module.new do include ::RecordInvocation def succeed_with(**ctx_writes) @configured_writes = ctx_writes self end def fail_with(**error_attrs) @configured_error = error_attrs self end record def call(ctx) return ::Hubbado::Sequence::Result.fail(ctx, error: @configured_error) if @configured_error if @configured_writes @configured_writes.each { |k, v| ctx[k] = v } end ::Hubbado::Sequence::Result.ok(ctx) end def called?(**kwargs) invoked?(:call, **kwargs) end end end |
.included(cls) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/hubbado/sequence/sequencer.rb', line 4 def self.included(cls) cls.send(:include, ::Dependency) cls.send(:include, ::Configure) cls.extend(ClassMethods) install_default_substitute(cls) end |
.install_default_substitute(cls) ⇒ Object
Each sequencer gets a default ‘Substitute` module so it can be used as a dependency without bespoke test scaffolding. The user can reopen the module in their class body to add per-sequencer assertions; the defaults below are always available.
16 17 18 19 20 |
# File 'lib/hubbado/sequence/sequencer.rb', line 16 def self.install_default_substitute(cls) return if cls.const_defined?(:Substitute, false) cls.const_set(:Substitute, build_default_substitute_module) end |
Instance Method Details
#failure(ctx, **error_attrs) ⇒ Object
85 86 87 |
# File 'lib/hubbado/sequence/sequencer.rb', line 85 def failure(ctx, **error_attrs) Result.fail(ctx, error: error_attrs, i18n_scope: i18n_scope) end |
#i18n_scope ⇒ Object
81 82 83 |
# File 'lib/hubbado/sequence/sequencer.rb', line 81 def i18n_scope self.class.i18n_scope end |
#pipeline(ctx, &block) ⇒ Object
Builds a Pipeline that auto-dispatches blockless ‘step(:foo)` calls to `self.foo(ctx)`. Use this inside a sequencer’s ‘call` body in place of `Pipeline.(ctx)` whenever steps are local methods.
Block form (‘pipeline(ctx) { |p| … }`) yields the pipeline, runs the block, and returns the final Result — no trailing `.result` needed. The non-block form returns the Pipeline so chained `.step(…)…result` calls still work.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/hubbado/sequence/sequencer.rb', line 97 def pipeline(ctx, &block) pipe = Pipeline.new(ctx, dispatcher: self) if block block.call(pipe) pipe.result else pipe end end |