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 =
[: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



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
# File 'lib/commander/user_interaction.rb', line 336

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)
    end.select do |const|
      const.class == 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)


363
364
365
# File 'lib/commander/user_interaction.rb', line 363

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