class RubyListComprehension::ListComprehension

Attributes

cache[RW]
filterable[RW]
flattener[RW]
line[RW]
mappable[RW]

Public Class Methods

new() click to toggle source
# File lib/ruby_list_comprehension.rb, line 197
def initialize
  @cache = {}
end

Public Instance Methods

[](*iterable) click to toggle source
# File lib/ruby_list_comprehension.rb, line 177
def [](*iterable)
  return [] if iterable.empty? || iterable.nil?

  @flattener = iterable.length == 1
  $iterable = iterable[0] if iterable.length == 1

  @filename = $PROGRAM_NAME
  if @filename == "pry" || @filename == "irb"
    @line = locate_list_repl
  else
    @line = locate_list_file
  end
  return @line if @line == :TOO_MANY_COMPS_ON_ONE_LINE
  return @cache[@line] if @cache.has_key?(@line)

  @cache[@line] = one_shot(@line)
end

Private Instance Methods

locate_list_file() click to toggle source
# File lib/ruby_list_comprehension.rb, line 215
def locate_list_file
  @line = retrieve_file_data[caller_locations.last.to_s.scan(/\d+/).last.to_i - 1]
            .strip.match(/\$l(?<line>.+)/)[:line][0...-1]
            .sub(";", " do ")
  @line = :TOO_MANY_COMPS_ON_ONE_LINE if @line.include?("$l")
  @line
end
locate_list_repl() click to toggle source
# File lib/ruby_list_comprehension.rb, line 201
def locate_list_repl
  @line = Readline::HISTORY.to_a.reverse.uniq.reverse[-1]
  start = @line.index('l[') + 2
  ending = @line[start..-1].index('end') + 6
  @line = @line[start...ending]
end
retrieve_file_data() click to toggle source
# File lib/ruby_list_comprehension.rb, line 208
def retrieve_file_data
  @file = File.open($PROGRAM_NAME)
  file_data = @file.readlines.map(&:chomp)
  @file.close
  file_data
end