Sha256: 7b7b806331c5698c0f6f0bda8365aad5c5f43da9074b056e6f3ab23821d267f3

Contents?: true

Size: 957 Bytes

Versions: 7

Compression:

Stored size: 957 Bytes

Contents

require "ripper"
require "sorcerer"

class ExampleBlock
  attr_reader :block
  attr_reader :code
  attr_reader :sexp
  attr_reader :assertion
  attr_reader :assertion_sexp

  def initialize(block)
    @block = block

    @code = generate_code

    @sexp = Ripper::SexpBuilder.new(code).parse.last
    @assertion_sexp = @sexp.last
    @assertion = Sorcerer.source(assertion_sexp)
  end

  def call
    block.call
  end

  def source_location
    block.source_location
  end

  private

  def generate_code
    lineno = block.source_location.last

    current_line = lineno-1
    valid_lines = [lines[current_line]]

    until Ripper::SexpBuilder.new(valid_lines.join("\n")).parse
      current_line += 1
      valid_lines << lines[current_line]
    end

    valid_lines[1..-2].join
  end

  def lines
    @lines ||= get_lines
  end

  def get_lines
    file = File.open(block.source_location.first)
    source = file.read
    source.each_line.to_a
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
matest-1.5.6 lib/matest/example_block.rb
matest-1.5.5 lib/matest/example_block.rb
matest-1.5.4 lib/matest/example_block.rb
matest-1.5.3 lib/matest/example_block.rb
matest-1.5.2 lib/matest/example_block.rb
matest-1.5.1 lib/matest/example_block.rb
matest-1.5.0 lib/matest/example_block.rb