Class: PryTest::Template
- Inherits:
-
Object
- Object
- PryTest::Template
- Includes:
- Color
- Defined in:
- lib/pry-test/formatters/template.rb
Class Method Summary collapse
Instance Method Summary collapse
- #assert_lines(assert) ⇒ Object
- #duration_color(duration) ⇒ Object
-
#initialize(context) ⇒ Template
constructor
A new instance of Template.
- #partial(name, *collection) ⇒ Object
- #render_to_string(name) ⇒ Object
Methods included from Color
Constructor Details
#initialize(context) ⇒ Template
Returns a new instance of Template.
13 14 15 |
# File 'lib/pry-test/formatters/template.rb', line 13 def initialize(context) @context = context end |
Class Method Details
.view(name) ⇒ Object
8 9 10 11 |
# File 'lib/pry-test/formatters/template.rb', line 8 def self.view(name) @views ||= {} @views[name] ||= File.read(File.("../views/#{name}.txt.erb", __FILE__)) end |
Instance Method Details
#assert_lines(assert) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pry-test/formatters/template.rb', line 35 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
30 31 32 33 |
# File 'lib/pry-test/formatters/template.rb', line 30 def duration_color(duration) return :yellow if @context.duration <= 0.01 :red end |
#partial(name, *collection) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/pry-test/formatters/template.rb', line 23 def partial(name, *collection) return render_to_string(name) if collection.empty? collection.map do |item| Template.new(item).render_to_string(name) end.join("\n") end |
#render_to_string(name) ⇒ Object
17 18 19 20 21 |
# File 'lib/pry-test/formatters/template.rb', line 17 def render_to_string(name) instance_eval do ERB.new(self.class.view(name), nil, "%<>-").result(binding) end end |