Module: Commander::UI::AskForClass

Included in:
Methods
Defined in:
lib/commander/user_interaction.rb

Overview

Implements ask_for_CLASS methods.

Constant Summary collapse

DEPRECATED_CONSTANTS =
%i[Config TimeoutError MissingSourceFile NIL TRUE FALSE Fixnum Bignum Data].freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/commander/user_interaction.rb', line 335

def method_missing(method_name, *arguments, &block)
  if method_name.to_s =~ /^ask_for_(.*)/
    if arguments.count != 1
      fail ArgumentError, "wrong number of arguments (given #{arguments.count}, expected 1)"
    end

    prompt = arguments.first
    requested_class = Regexp.last_match[1]

    # All Classes that respond to #parse
    # Ignore constants that trigger deprecation warnings
    available_classes = (Object.constants - DEPRECATED_CONSTANTS).map do |const|
      Object.const_get(const)
    rescue RuntimeError
      # Rescue errors in Ruby 3 for SortedSet:
      # The `SortedSet` class has been extracted from the `set` library.
    end.compact.select do |const|
      const.instance_of?(Class) && const.respond_to?(:parse)
    end

    klass = available_classes.find { |k| k.to_s.downcase == requested_class }
    if klass
      HighLine.default_instance.ask(prompt, klass)
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


366
367
368
# File 'lib/commander/user_interaction.rb', line 366

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?('ask_for_') || super
end