Sha256: 380807574082cfc1e491109febe03325a3783ff4737dd31e640da3cb22f9f4ea

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require_relative 'validation'
require_relative '../../model'

module Lino
  module Builders
    module Mixins
      module Subcommands
        include Validation

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

        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

        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

        private

        def state
          super.merge(subcommands: @subcommands)
        end

        def build_subcommands(
          option_separator, option_quoting, option_placement
        )
          @subcommands.map do |s|
            s.build(option_separator, option_quoting, option_placement)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lino-4.2.0.pre.2 lib/lino/builders/mixins/subcommands.rb
lino-4.2.0.pre.1 lib/lino/builders/mixins/subcommands.rb
lino-4.1.0 lib/lino/builders/mixins/subcommands.rb
lino-4.1.0.pre.2 lib/lino/builders/mixins/subcommands.rb
lino-4.1.0.pre.1 lib/lino/builders/mixins/subcommands.rb