class Redcarpet::Render::SeeingIsBelieving::Enricher

Attributes

options[R]

Public Class Methods

new(options = Options.new) click to toggle source
# File lib/redcarpet/render/seeing_is_believing/enricher.rb, line 8
def initialize(options = Options.new)
  @options = options
end

Public Instance Methods

process(code) click to toggle source
# File lib/redcarpet/render/seeing_is_believing/enricher.rb, line 12
def process(code)
  code.split("\n").
    zip(evaluate_code(code)).
    map(&combine_code_with_result).
    join("\n")
end

Private Instance Methods

combine_code_with_result() click to toggle source
# File lib/redcarpet/render/seeing_is_believing/enricher.rb, line 47
def combine_code_with_result
  proc do |code_line, eval_line|
    if eval_line
      "#{code_line} # => #{eval_line}"
    else
      code_line
    end
  end
end
combine_eval_lines_and_exceptions(result) click to toggle source
# File lib/redcarpet/render/seeing_is_believing/enricher.rb, line 28
def combine_eval_lines_and_exceptions(result)
  (1..result.num_lines).map do |line_number|
    eval_lines = result[line_number]
    exception = exception_for_line(result.exceptions, line_number)

    if eval_lines.any?
      eval_lines.join(" ")
    elsif exception
      "#{exception.class_name}: #{exception.message}"
    end
  end
end
evaluate_code(code) click to toggle source
# File lib/redcarpet/render/seeing_is_believing/enricher.rb, line 23
def evaluate_code(code)
  eval_result = ::SeeingIsBelieving.call(code).result
  combine_eval_lines_and_exceptions(eval_result)
end
exception_for_line(exceptions, line_number) click to toggle source
# File lib/redcarpet/render/seeing_is_believing/enricher.rb, line 41
def exception_for_line(exceptions, line_number)
  if options.show_exceptions
    exceptions.detect { |ex| ex.line_number == line_number }
  end
end