Module: Lino::Builders::Mixins::Subcommands

Includes:
Validation
Included in:
CommandLine
Defined in:
lib/lino/builders/mixins/subcommands.rb

Instance Method Summary collapse

Methods included from Validation

#empty?, #nil_or_empty?

Instance Method Details

#initialize(state) ⇒ Object



12
13
14
15
# File 'lib/lino/builders/mixins/subcommands.rb', line 12

def initialize(state)
  @subcommands = Hamster::Vector.new(state[:subcommands] || [])
  super
end

#with_subcommand(subcommand, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lino/builders/mixins/subcommands.rb', line 17

def with_subcommand(subcommand, &block)
  return self if nil_or_empty?(subcommand)

  with(
    subcommands: @subcommands.add(
      (block || ->(sub) { sub }).call(
        Builders::Subcommand.for_subcommand(subcommand)
      )
    )
  )
end

#with_subcommands(subcommands) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/lino/builders/mixins/subcommands.rb', line 29

def with_subcommands(subcommands, &)
  return self if nil_or_empty?(subcommands)

  without_block = subcommands[0...-1]
  with_block = subcommands.last

  without_block
    .inject(self) { |s, sc| s.with_subcommand(sc) }
    .with_subcommand(with_block, &)
end