From 47674428752a1a07381848b3857164249ae3510c Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Mon, 5 Aug 2013 18:18:49 +0200 Subject: [PATCH] Make Snapshot#crop return the snapshot of exact requested height --- app/models/snapshot.rb | 9 +++++++-- spec/models/snapshot_spec.rb | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/models/snapshot.rb b/app/models/snapshot.rb index 03fa99b..2e7a6d0 100644 --- a/app/models/snapshot.rb +++ b/app/models/snapshot.rb @@ -18,8 +18,13 @@ class Snapshot end def crop(width, height) - height = [height, lines.size].min - new_lines = lines.drop(lines.size - height).map { |line| line.crop(width) } + min_height = [height, lines.size].min + new_lines = lines.drop(lines.size - min_height).map { |line| line.crop(width) } + + while new_lines.size < height + new_lines << [] + end + self.class.new(new_lines) end diff --git a/spec/models/snapshot_spec.rb b/spec/models/snapshot_spec.rb index 2dd1b7a..cb61027 100644 --- a/spec/models/snapshot_spec.rb +++ b/spec/models/snapshot_spec.rb @@ -98,9 +98,9 @@ describe Snapshot do context "when height is greater than lines count" do let(:height) { 4 } - it 'returns a new Snapshot with all lines cropped' do + it 'returns a new Snapshot with all lines cropped and missing lines added' do expect(subject).to eq(Snapshot.new([cropped_line_1, cropped_line_2, - cropped_line_3])) + cropped_line_3, []])) end end end