Class: Clack::Group
- Inherits:
-
Object
- Object
- Clack::Group
- Defined in:
- lib/clack/group.rb
Overview
Collects results from multiple prompts and handles cancellation gracefully.
Instance Attribute Summary collapse
-
#results ⇒ Hash
readonly
The collected results.
Instance Method Summary collapse
-
#initialize(on_cancel: nil) ⇒ Group
constructor
A new instance of Group.
-
#prompt(name) {|results| ... } ⇒ void
Define a prompt in the group.
-
#run ⇒ Hash, Clack::CANCEL
Run all prompts and collect results.
Constructor Details
#initialize(on_cancel: nil) ⇒ Group
Returns a new instance of Group.
23 24 25 26 27 |
# File 'lib/clack/group.rb', line 23 def initialize(on_cancel: nil) @results = {} @prompts = [] @on_cancel = on_cancel end |
Instance Attribute Details
#results ⇒ Hash (readonly)
Returns The collected results.
21 22 23 |
# File 'lib/clack/group.rb', line 21 def results @results end |
Instance Method Details
#prompt(name) {|results| ... } ⇒ void
This method returns an undefined value.
Define a prompt in the group.
35 36 37 38 39 |
# File 'lib/clack/group.rb', line 35 def prompt(name, &block) raise ArgumentError, "Block required for prompt :#{name}" unless block_given? @prompts << {name: name.to_sym, block: block} end |
#run ⇒ Hash, Clack::CANCEL
Run all prompts and collect results.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/clack/group.rb', line 44 def run @prompts.each do |prompt_def| name = prompt_def[:name] block = prompt_def[:block] # Pass previous results to the block if it accepts an argument result = if block.arity.zero? block.call else block.call(@results.dup.freeze) end if Clack.cancel?(result) @results[name] = :cancelled @on_cancel&.call(@results.dup.freeze) return CANCEL end @results[name] = result end @results end |