Class: Inquirex::Actions::Action
- Inherits:
-
Object
- Object
- Inquirex::Actions::Action
- Defined in:
- lib/inquirex/actions/action.rb
Overview
A named post-completion unit: an optional rule gating execution and an
ordered list of effects. Declared with the DSL word action:
action :client_receipt, if: not_empty(:email) do
send_email to: "{{email}}", subject: "Thanks {{name}}!", text: "..."
end
Rules reuse the same serializable AST as transitions, so conditions survive the JSON round-trip. Non-serializable effects (run blocks) are stripped on serialization; an action left with no serializable effects is omitted from JSON entirely.
Instance Attribute Summary collapse
-
#effects ⇒ Object
readonly
Returns the value of attribute effects.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
Class Method Summary collapse
Instance Method Summary collapse
- #applicable?(answers_hash) ⇒ Boolean
-
#initialize(id:, effects:, rule: nil) ⇒ Action
constructor
A new instance of Action.
-
#serializable? ⇒ Boolean
Whether anything survives JSON serialization.
-
#to_h ⇒ Hash
Wire format; run blocks are stripped.
Constructor Details
#initialize(id:, effects:, rule: nil) ⇒ Action
Returns a new instance of Action.
22 23 24 25 26 27 |
# File 'lib/inquirex/actions/action.rb', line 22 def initialize(id:, effects:, rule: nil) @id = id.to_sym @effects = effects.freeze @rule = rule freeze end |
Instance Attribute Details
#effects ⇒ Object (readonly)
Returns the value of attribute effects.
17 18 19 |
# File 'lib/inquirex/actions/action.rb', line 17 def effects @effects end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/inquirex/actions/action.rb', line 17 def id @id end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
17 18 19 |
# File 'lib/inquirex/actions/action.rb', line 17 def rule @rule end |
Class Method Details
.from_h(hash) ⇒ Action
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/inquirex/actions/action.rb', line 50 def self.from_h(hash) id = hash["id"] || hash[:id] rule_data = hash["if"] || hash[:if] effects_data = hash["effects"] || hash[:effects] || [] effects = effects_data.map do |effect_hash| type = effect_hash["type"] || effect_hash[:type] Actions.lookup(type).from_h(effect_hash) end new( id: id.to_sym, effects: effects, rule: rule_data ? Rules::Base.from_h(rule_data) : nil ) end |
Instance Method Details
#applicable?(answers_hash) ⇒ Boolean
31 32 33 |
# File 'lib/inquirex/actions/action.rb', line 31 def applicable?(answers_hash) @rule.nil? || @rule.evaluate(answers_hash) end |
#serializable? ⇒ Boolean
Returns whether anything survives JSON serialization.
36 37 38 |
# File 'lib/inquirex/actions/action.rb', line 36 def serializable? @effects.any?(&:serializable?) end |
#to_h ⇒ Hash
Returns wire format; run blocks are stripped.
41 42 43 44 45 46 |
# File 'lib/inquirex/actions/action.rb', line 41 def to_h hash = { "id" => @id.to_s } hash["if"] = @rule.to_h if @rule hash["effects"] = @effects.select(&:serializable?).map(&:to_h) hash end |