Use less spans for thumbnail rendering

This commit is contained in:
Marcin Kulik 2013-08-26 10:22:21 +02:00
parent 31a880b7aa
commit c775bc8b06
4 changed files with 74 additions and 2 deletions

View File

@ -9,7 +9,9 @@ class SnapshotDecorator < ApplicationDecorator
private
def line(line_no)
(0...width).map { |column_no| model.cell(column_no, line_no) }
line = (0...width).map { |column_no| model.cell(column_no, line_no) }
LineOptimizer.new(line).optimize
end
end

View File

@ -0,0 +1,33 @@
class LineOptimizer
def initialize(line)
@line = line
end
def optimize
return [] if line.empty?
text = [line[0].text]
brush = line[0].brush
cells = []
line[1..-1].each do |cell|
if cell.brush == brush
text << cell.text
else
cells << Cell.new(text.join, brush)
text, brush = [cell.text], cell.brush
end
end
cells << Cell.new(text.join, brush)
cells
end
private
attr_reader :line
end

View File

@ -14,9 +14,17 @@ describe SnapshotDecorator do
before do
allow(snapshot).to receive(:cell) { |x, y| cells[y][x] }
allow(LineOptimizer).to receive(:new).with([:a, :b]) {
double('optimizer', :optimize => [:ab])
}
allow(LineOptimizer).to receive(:new).with([:c, :d]) {
double('optimizer', :optimize => [:c, :d])
}
end
it { should eq(cells) }
it { should eq([ [:ab], [:c, :d] ]) }
end
end

View File

@ -0,0 +1,29 @@
require 'spec_helper'
describe LineOptimizer do
let(:line_optimizer) { described_class.new(line) }
def brush(attrs)
Brush.new(attrs)
end
describe '#optimize' do
let(:line) { [
Cell.new('a', brush(fg: 1)),
Cell.new('b', brush(fg: 1)),
Cell.new('c', brush(fg: 2)),
Cell.new('d', brush(fg: 3)),
Cell.new('e', brush(fg: 3))
] }
subject { line_optimizer.optimize }
it { should eq([
Cell.new('ab', brush(fg: 1)),
Cell.new('c', brush(fg: 2)),
Cell.new('de', brush(fg: 3))
]) }
end
end