Class: Inquirex::Actions::Action

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, effects:, rule: nil) ⇒ Action

Returns a new instance of Action.

Parameters:

  • id (Symbol)

    action identifier

  • effects (Array<Actions::Base>)

    executed in declaration order

  • rule (Rules::Base, nil) (defaults to: nil)

    gate — action runs only when true



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

#effectsObject (readonly)

Returns the value of attribute effects.



17
18
19
# File 'lib/inquirex/actions/action.rb', line 17

def effects
  @effects
end

#idObject (readonly)

Returns the value of attribute id.



17
18
19
# File 'lib/inquirex/actions/action.rb', line 17

def id
  @id
end

#ruleObject (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

Parameters:

  • hash (Hash)

    string or symbol keys

Returns:



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

Parameters:

  • answers_hash (Hash)

    step_id => value context for rule evaluation

Returns:

  • (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.

Returns:

  • (Boolean)

    whether anything survives JSON serialization



36
37
38
# File 'lib/inquirex/actions/action.rb', line 36

def serializable?
  @effects.any?(&:serializable?)
end

#to_hHash

Returns wire format; run blocks are stripped.

Returns:

  • (Hash)

    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