Class: Inquirex::DSL::ActionBuilder
- Inherits:
-
Object
- Object
- Inquirex::DSL::ActionBuilder
- Defined in:
- lib/inquirex/dsl/action_builder.rb
Overview
Builds an Actions::Action from an action DSL block. Every effect verb
registered in Inquirex::Actions (send_email, plus anything host gems
register) is available as a method automatically; run wraps arbitrary
Ruby in an Actions::Custom effect.
action :admin_alert do
send_email to: "admin@example.com", subject: "New lead: {{name}}",
html: "{{answers_summary}}"
run { |answers, outbox| Metrics.count(:lead, answers.to_flat_h) }
end
Instance Method Summary collapse
- #build(id, rule: nil) ⇒ Actions::Action
-
#initialize ⇒ ActionBuilder
constructor
A new instance of ActionBuilder.
-
#method_missing(name, *args, **params) ⇒ Object
Registered effect verbs (send_email, ...) resolve dynamically so that newly registered effect types become DSL words without core changes.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#run {|answers, outbox| ... } ⇒ Object
Escape hatch: arbitrary server-side Ruby.
Constructor Details
#initialize ⇒ ActionBuilder
Returns a new instance of ActionBuilder.
16 17 18 |
# File 'lib/inquirex/dsl/action_builder.rb', line 16 def initialize @effects = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **params) ⇒ Object
Registered effect verbs (send_email, ...) resolve dynamically so that newly registered effect types become DSL words without core changes.
31 32 33 34 35 36 37 |
# File 'lib/inquirex/dsl/action_builder.rb', line 31 def method_missing(name, *args, **params, &) return super unless Actions.registered?(name) raise Errors::DefinitionError, "#{name} takes keyword arguments only" unless args.empty? @effects << Actions.lookup(name).new(**params) end |
Instance Method Details
#build(id, rule: nil) ⇒ Actions::Action
46 47 48 49 50 |
# File 'lib/inquirex/dsl/action_builder.rb', line 46 def build(id, rule: nil) raise Errors::DefinitionError, "action #{id.inspect} declares no effects" if @effects.empty? Actions::Action.new(id:, effects: @effects, rule:) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
39 40 41 |
# File 'lib/inquirex/dsl/action_builder.rb', line 39 def respond_to_missing?(name, include_private = false) Actions.registered?(name) || super end |
#run {|answers, outbox| ... } ⇒ Object
Escape hatch: arbitrary server-side Ruby. Stripped from JSON.
23 24 25 26 27 |
# File 'lib/inquirex/dsl/action_builder.rb', line 23 def run(&block) raise Errors::DefinitionError, "run requires a block" unless block @effects << Actions::Custom.new(block) end |