class GemLookup::RubyGems

Public Class Methods

new(gems) click to toggle source

Creates a new instance of RubyGems. @param gems [Array] an array of gems and/or flags.

# File lib/gem_lookup/ruby_gems.rb, line 9
def initialize(gems)
  @gem_list = gems
  @flags = []
  @display_mode = :emoji
  @serializer = nil
  @continue = true
end

Public Instance Methods

find_all() click to toggle source

Handles the preparation and processing of the gems and/or flags.

# File lib/gem_lookup/ruby_gems.rb, line 18
def find_all
  format_list
  process_flags if valid?

  return unless continue?

  process_gems if valid?
  display_help! unless valid?
end

Private Instance Methods

check_flags() click to toggle source

Looks through the flags and calls GemLookup::Flags to assist, and either calls GemLookup::Help or sets the display mode, where appropriate. rubocop:disable Metrics/MethodLength

# File lib/gem_lookup/ruby_gems.rb, line 68
def check_flags
  if Flags.supported?(:help, flags: @flags)
    Help.display exit_code: 0
    @continue = false
  elsif Flags.supported?(:version, flags: @flags)
    Help.version exit_code: 0
    @continue = false
  elsif Flags.supported?(:wordy, flags: @flags)
    @display_mode = :wordy
  elsif Flags.supported?(:json, flags: @flags)
    @display_mode = :json
  else
    Flags.unsupported(flags: @flags)
  end
end
continue?() click to toggle source

If the lookup process should continue. @return [Boolean] whether to continue the lookup process or not.

# File lib/gem_lookup/ruby_gems.rb, line 109
def continue?
  @continue
end
detect_flags() click to toggle source

Looks for strings that start with a dash and marks them as flags, and removes them from the gem list.

# File lib/gem_lookup/ruby_gems.rb, line 39
def detect_flags
  @flags = @gem_list.select {|g| g[0] == '-' }
  @gem_list -= @flags
end
detect_serializer() click to toggle source

Utilizes the display mode to identify which serializer to utilize.

# File lib/gem_lookup/ruby_gems.rb, line 86
def detect_serializer
  @serializer = case @display_mode
                when :wordy
                  Serializers::Wordy
                when :json
                  Serializers::Json
                when :emoji
                  Serializers::Emoji
                end

  invalid_display_mode! if @serializer.nil?
end
display_help!() click to toggle source

Calls the GemLookup::Help.display method and exits with an exit code of 1.

# File lib/gem_lookup/ruby_gems.rb, line 120
def display_help!
  Help.display exit_code: 1
end
error(message:) click to toggle source

Outputs the passed in message in a standard error format, in red, and exits with an exit code of 1. @param message [String] the error message to present.

# File lib/gem_lookup/ruby_gems.rb, line 127
def error(message:)
  puts "=> Error: #{message}".red
  exit 1
end
format_list() click to toggle source

Lower-cases the entire gem list, and then removes duplicate entries.

# File lib/gem_lookup/ruby_gems.rb, line 45
def format_list
  @gem_list.map!(&:downcase).uniq!
end
handle_flags() click to toggle source

Calls the check_flags method if there are any flag entries.

# File lib/gem_lookup/ruby_gems.rb, line 55
def handle_flags
  return unless @flags.any?

  check_flags
rescue GemLookup::Errors::UnsupportedFlag => e
  error message: "Unsupported flag [#{e.message}]"
rescue GemLookup::Errors::UnsupportedFlags => e
  error message: "Unsupported flags [#{e.message}]"
end
invalid_display_mode!() click to toggle source

Raises the Invalid display mode error.

# File lib/gem_lookup/ruby_gems.rb, line 100
def invalid_display_mode!
  @continue = false
  raise GemLookup::Errors::InvalidDisplayMode, @display_mode
rescue GemLookup::Errors::InvalidDisplayMode => e
  error message: "Invalid display mode [#{e.message}]"
end
process_flags() click to toggle source

Processes the detection and handling of flags.

# File lib/gem_lookup/ruby_gems.rb, line 31
def process_flags
  detect_flags
  handle_flags
  detect_serializer
end
process_gems() click to toggle source

Creates a new GemLookup::Gems instance and calls process.

# File lib/gem_lookup/ruby_gems.rb, line 50
def process_gems
  Gems.new(@gem_list, serializer: @serializer).process
end
valid?() click to toggle source

If the gem list is valid. @return [Boolean] whether there are any entries in the gem list.

# File lib/gem_lookup/ruby_gems.rb, line 115
def valid?
  @gem_list.any?
end