Sha256: 282c7e0a786d1e29742f9d41270c49e39db7f510b40cf5dd338629f10e6252af

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Tocer
  module CLI
    # The main Command Line Interface (CLI) object.
    class Shell
      ACTIONS = {config: Actions::Config.new, insert: Actions::Insert.new}.freeze

      def initialize parser: Parser.new, actions: ACTIONS, container: Container
        @parser = parser
        @actions = actions
        @container = container
      end

      def call arguments = []
        perform parser.call(arguments)
      rescue OptionParser::ParseError => error
        puts error.message
      end

      private

      attr_reader :parser, :actions, :container

      def perform configuration
        case configuration
          in action_config: Symbol => action then process_config action
          in action_insert: true then process_insert configuration
          in action_version: true then logger.info { specification.labeled_version }
          else usage
        end
      end

      def process_config(action) = actions.fetch(:config).call(action)

      def process_insert(configuration) = actions.fetch(:insert).call(configuration)

      def usage = puts(parser.to_s)

      def specification = container[__method__]

      def logger = container[__method__]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tocer-13.3.1 lib/tocer/cli/shell.rb
tocer-13.3.0 lib/tocer/cli/shell.rb
tocer-13.2.0 lib/tocer/cli/shell.rb