Class: PryTest::Template

Inherits:
Object
  • Object
show all
Includes:
Color
Defined in:
lib/pry-test/formatters/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Color

#default

Constructor Details

#initialize(context) ⇒ Template

Returns a new instance of Template.



15
16
17
# File 'lib/pry-test/formatters/template.rb', line 15

def initialize(context)
  @context = context
end

Class Method Details

.view(name) ⇒ Object



10
11
12
13
# File 'lib/pry-test/formatters/template.rb', line 10

def self.view(name)
  @views ||= {}
  @views[name] ||= File.read(File.expand_path("../views/#{name}.txt.erb", __FILE__))
end

Instance Method Details

#assert_lines(assert) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pry-test/formatters/template.rb', line 37

def assert_lines(assert)
  index = assert[:line_num] - 1
  start = index - 2
  start = 0 if start <= 0
  finish = index + 2
  finish = assert[:lines].length - 1 if finish >= assert[:lines].length
  (start..finish).map do |i|
    {
      line_num: (i + 1),
      line: assert[:lines][i],
      color: (i == index ? :red : :gray)
    }
  end
end

#duration_color(duration) ⇒ Object



32
33
34
35
# File 'lib/pry-test/formatters/template.rb', line 32

def duration_color(duration)
  return :yellow if @context.duration <= 0.01
  :red
end

#partial(name, *collection) ⇒ Object



25
26
27
28
29
30
# File 'lib/pry-test/formatters/template.rb', line 25

def partial(name, *collection)
  return render_to_string(name) if collection.empty?
  collection.map { |item|
    Template.new(item).render_to_string(name)
  }.join("\n")
end

#render_to_string(name) ⇒ Object



19
20
21
22
23
# File 'lib/pry-test/formatters/template.rb', line 19

def render_to_string(name)
  instance_eval do
    ERB.new(self.class.view(name), trim_mode: "-").result(binding)
  end
end