Class: Plutonium::Interaction::Base
- Inherits:
-
Object
- Object
- Plutonium::Interaction::Base
- Includes:
- ActiveModel::Attributes, ActiveModel::Model, Definition::ConfigAttr, Definition::DefineableProps, Definition::Presentable, Definition::StructuredInputs
- Defined in:
- lib/plutonium/interaction/base.rb
Overview
Subclasses must implement the #execute method.
Base class for all interactions. Provides core functionality for validations, execution, and result handling.
Direct Known Subclasses
Defined Under Namespace
Classes: Form
Instance Attribute Summary collapse
-
#view_context ⇒ Object
readonly
Returns the value of attribute view_context.
Class Method Summary collapse
- .build_form(instance) ⇒ Object
- .call ⇒ Object
-
.structured_input(name, **options, &block) ⇒ Object
On interactions, declaring a structured input also declares the backing ActiveModel attribute so the value survives ‘attributes=` and appears in `attribute_names` (which drives the interaction form’s field list).
Instance Method Summary collapse
- #build_form ⇒ Object
-
#call ⇒ Plutonium::Interaction::Outcome
Executes the interaction.
-
#initialize(view_context:, **attributes) ⇒ Base
constructor
A new instance of Base.
Methods included from Definition::StructuredInputs
Methods included from Definition::Presentable
Constructor Details
#initialize(view_context:, **attributes) ⇒ Base
Returns a new instance of Base.
60 61 62 63 |
# File 'lib/plutonium/interaction/base.rb', line 60 def initialize(view_context:, **attributes) super(attributes) @view_context = view_context end |
Instance Attribute Details
#view_context ⇒ Object (readonly)
Returns the value of attribute view_context.
58 59 60 |
# File 'lib/plutonium/interaction/base.rb', line 58 def view_context @view_context end |
Class Method Details
.build_form(instance) ⇒ Object
48 49 50 51 52 |
# File 'lib/plutonium/interaction/base.rb', line 48 def build_form(instance) raise ArgumentError, "instance is required" unless instance self::Form.new(instance) end |
.call ⇒ Object
44 45 46 |
# File 'lib/plutonium/interaction/base.rb', line 44 def call(...) new(...).call end |
.structured_input(name, **options, &block) ⇒ Object
On interactions, declaring a structured input also declares the backing ActiveModel attribute so the value survives ‘attributes=` and appears in `attribute_names` (which drives the interaction form’s field list).
33 34 35 36 37 |
# File 'lib/plutonium/interaction/base.rb', line 33 def self.structured_input(name, **, &block) super default = [:repeat] ? -> { [] } : -> { {} } attribute name, default: default end |
Instance Method Details
#build_form ⇒ Object
65 66 67 |
# File 'lib/plutonium/interaction/base.rb', line 65 def build_form self.class.build_form(self) end |
#call ⇒ Plutonium::Interaction::Outcome
Executes the interaction.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/plutonium/interaction/base.rb', line 72 def call if valid? outcome = execute unless outcome.is_a?(Plutonium::Interaction::Outcome) raise "#{self.class}#execute must return an instance of Plutonium::Interaction::Outcome.\n" \ "#{outcome.inspect} received instead" end outcome else failure.("An error occurred") end end |