Sha256: 58ac4150183b5541cf7735e143d1e037e7a021018560d5ae4f323535a401d9aa

Contents?: true

Size: 1.35 KB

Versions: 28

Compression:

Stored size: 1.35 KB

Contents

class Proc
  
  # Get the code, based on the inspect's filename and line number start
  def code(filename=source_file)
    return @code if @code
    current_block_count = 1
    line_count = 0
    
    source(filename).each do |line|
      if line =~ /do(\W*?)$/
        current_block_count+=1
      elsif line =~ /end$/
        current_block_count-=1
        if current_block_count == 0
          return @code = source(filename)[0..(line_count-1)].join("\n")
        end        
      end
      line_count+=1
    end    
  end
  
  # Source
  def source(filename = source_file, start_line = source_line_number)
    @source ||= if File.exist?(filename)
      begin
        open(filename).read.split("\n")[start_line .. -1]
      rescue
        nil
      end
    else
      raise StandardError.new("Cannot find the source file for #{self.inspect}")
    end
  end
  
  # Grab the location of the proc by filename
  def source_file
    @source_file ||= proc_info[0]
  end
  
  # Grab the line number in the source_file
  def source_line_number
    @source_line_number ||= proc_info[1]
  end
  
  # Grab the source location from the inspect method
  def proc_info
    return @proc_info if @proc_info
    if md = /^#<Proc:0x[0-9A-Fa-f]+@(.+):(\d+)>$/.match(inspect)
      filename, line = md.captures
      @proc_info = File.expand_path(filename), line.to_i
    end
    @proc_info
  end
  
end

Version data entries

28 entries across 28 versions & 3 rubygems

Version Path
auser-poolparty-1.3.0 lib/core/proc.rb
auser-poolparty-1.3.1 lib/core/proc.rb
auser-poolparty-1.3.10 lib/core/proc.rb
auser-poolparty-1.3.11 lib/core/proc.rb
auser-poolparty-1.3.12 lib/core/proc.rb
auser-poolparty-1.3.13 lib/core/proc.rb
auser-poolparty-1.3.14 lib/core/proc.rb
auser-poolparty-1.3.15 lib/core/proc.rb
auser-poolparty-1.3.16 lib/core/proc.rb
auser-poolparty-1.3.17 lib/core/proc.rb
auser-poolparty-1.3.2 lib/core/proc.rb
auser-poolparty-1.3.3 lib/core/proc.rb
auser-poolparty-1.3.4 lib/core/proc.rb
auser-poolparty-1.3.5 lib/core/proc.rb
auser-poolparty-1.3.6 lib/core/proc.rb
auser-poolparty-1.3.7 lib/core/proc.rb
auser-poolparty-1.3.8 lib/core/proc.rb
fairchild-poolparty-1.3.17 lib/core/proc.rb
fairchild-poolparty-1.3.5 lib/core/proc.rb
poolparty-1.3.15 lib/core/proc.rb