Sha256: cf9bfef014b010e363c5c56912cf0af9ce6330bd74b87d9fb5fd841ad69688fe

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacCli
  module Runner
    class Context
      attr_reader :argv, :parent, :program_name, :runner

      def initialize(runner, *context_args)
        options = context_args.extract_options!
        @argv = (context_args[0] || options.delete(:argv) || ARGV).dup.freeze
        @parent = context_args[1] || options.delete(:parent)
        @program_name = options.delete(:program_name)
        @runner = runner
      end

      # Call a method in the runner or in one of it ancestors.
      def call(method_name, *args, &block)
        return runner.send(method_name, *args, &block) if runner.respond_to?(method_name)
        return parent_call(method_name, *args, &block) if parent_respond_to?(method_name)

        raise ::NameError, "No method \"#{method_name}\" found in #{runner} or in its ancestors"
      end

      # @param method_name [Symbol]
      # @return [Boolean]
      def parent_respond_to?(method_name)
        parent.if_present(false) do |v|
          next true if v.respond_to?(method_name)

          v.if_respond(:runner_context, false) { |w| w.parent_respond_to?(method_name) }
        end
      end

      def parent_call(method_name, *args, &block)
        if parent.respond_to?(:runner_context)
          return parent.runner_context.call(method_name, *args, &block)
        end

        raise "Parent #{parent} do not respond to .context or .runner_context (Runner: #{runner})"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eac_cli-0.34.1 lib/eac_cli/runner/context.rb