From 8bdcf9c7aa2e20b341f1a9630c55099db10f5333 Mon Sep 17 00:00:00 2001 From: dtan4 Date: Fri, 30 May 2014 02:13:05 +0900 Subject: [PATCH] Move Client#resize_to_pixel_size to Image#resize_images --- lib/photomosaic/client.rb | 10 ++-------- lib/photomosaic/image.rb | 6 ++++++ spec/photomosaic/client_spec.rb | 6 +++--- spec/photomosaic/image_spec.rb | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/photomosaic/client.rb b/lib/photomosaic/client.rb index 4955239..1af331f 100644 --- a/lib/photomosaic/client.rb +++ b/lib/photomosaic/client.rb @@ -12,8 +12,8 @@ def execute @image_downloader = Photomosaic::ImageDownloader.new begin - resize_to_pixel_size(pixel_images) - Photomosaic::Image.create_mosaic_image(pixel_images, @options.output_path) + resized_images = Photomosaic::Image.resize_images(pixel_images, 40, 20) + Photomosaic::Image.create_mosaic_image(resized_images, @options.output_path) ensure @image_downloader.remove_save_dir end @@ -54,12 +54,6 @@ def pixel_images base_image.dispatch_images(image_list, 1, 2, @options.color_model) end - def resize_to_pixel_size(images) - images.map! do |row| - row.map { |image| image.resize!(40, 20, false) } - end - end - def search_engine @options.search_engine.new(@options.api_key, @options.results) end diff --git a/lib/photomosaic/image.rb b/lib/photomosaic/image.rb index b536528..89b59f8 100644 --- a/lib/photomosaic/image.rb +++ b/lib/photomosaic/image.rb @@ -21,6 +21,12 @@ def self.preprocess_image(image_path, width, height, level, colors) image end + def self.resize_images(images, width, height) + images.map do |row| + row.map { |image| image.resize!(width, height, false) } + end + end + def initialize(image_path) @image = Magick::Image.read(image_path).first end diff --git a/spec/photomosaic/client_spec.rb b/spec/photomosaic/client_spec.rb index bef51c8..61d9c73 100644 --- a/spec/photomosaic/client_spec.rb +++ b/spec/photomosaic/client_spec.rb @@ -40,9 +40,9 @@ module Photomosaic describe "#execute" do before do - allow_any_instance_of(described_class).to receive(:pixel_images) - allow_any_instance_of(described_class).to receive(:resize_to_pixel_size).and_return(true) - allow(Photomosaic::Image).to receive(:create_mosaic_image).and_return(true) + allow_any_instance_of(described_class).to receive(:pixel_images).and_return([[]]) + allow(Photomosaic::Image).to receive(:resize_to_pixel_size) + allow(Photomosaic::Image).to receive(:create_mosaic_image) end it "should execute the program" do diff --git a/spec/photomosaic/image_spec.rb b/spec/photomosaic/image_spec.rb index 2453f51..591d754 100644 --- a/spec/photomosaic/image_spec.rb +++ b/spec/photomosaic/image_spec.rb @@ -55,6 +55,24 @@ module Photomosaic described_class.preprocess_image(image_path, 100, 100, 4, 8) end end + + describe "#resize_images" do + let(:image) do + double(:resize!) + end + + let(:image_list) do + 5.times.inject([]) do |image_list, _| + image_list << [image] + image_list + end + end + + it "should resize images" do + expect(image).to receive(:resize!).exactly(5).times + described_class.resize_images(image_list, 40, 20) + end + end end context "instance methods" do