Skip to content

Commit 5cee84d

Browse files
committed
Refactoring for Easier Testing
1 parent ddcad0e commit 5cee84d

File tree

12 files changed

+207
-130
lines changed

12 files changed

+207
-130
lines changed

lib/sprite.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
require 'sprite/sprite'
1+
# set up Sprite module
2+
module Sprite
3+
def self.root
4+
@root ||= nil
5+
6+
# set the root to the framework setting (if not already set)
7+
@root ||= begin
8+
if defined?(Rails)
9+
Rails.root
10+
elsif defined?(Merb)
11+
Merb.root
12+
else
13+
"."
14+
end
15+
end
16+
@root
17+
end
18+
end
19+
20+
require 'sprite/builder'
21+
require 'sprite/image_combiner'

lib/sprite/builder.rb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
module Sprite
2+
class Builder
3+
DEFAULT_CONFIG_PATH = 'config/sprite.yml'
4+
DEFAULT_IMAGE_PATH = 'public/images/'
5+
DEFAULT_FILE_PATH = 'tmp/sprite.css'
6+
7+
def initialize(settings = nil)
8+
@image_path = DEFAULT_IMAGE_PATH
9+
10+
if settings.is_a?(Hash)
11+
@sprite_config = settings
12+
else
13+
@sprite_config = read_sprite_config(settings)
14+
end
15+
end
16+
17+
def build
18+
@output = {}
19+
20+
# build up settings
21+
@image_path = sprite_config['config']['base_image_path'] ? Sprite.root+"/"+sprite_config['config']['base_image_path']+"/" : DEFAULT_IMAGE_PATH
22+
@file_path = sprite_config['config']['output_file'] ? Sprite.root+"/"+sprite_config['config']['output_file'] : DEFAULT_FILE_PATH
23+
24+
# create images
25+
sprite_config['images'].each do |configuration|
26+
output_image(configuration)
27+
end
28+
29+
# write css
30+
output_css(sprite_config)
31+
end
32+
33+
def output_image(configuration)
34+
results = []
35+
sources = configuration['sources'].collect {|source| Dir.glob(@image_path+source)}.flatten
36+
dest = configuration['target'] || sources[0].gsub(/\./,"_sprite.")
37+
spaced_by = configuration['spaced_by'] || 0
38+
dest_image = ImageUtil.get_image(sources.shift)
39+
results << ImageUtil.image_properties(dest_image).merge(:x => 0, :y => 0)
40+
sources.each do |source|
41+
source_image = ImageUtil.get_image(source)
42+
if configuration['align'] == 'horizontal'
43+
gravity = Magick::EastGravity
44+
x = dest_image.columns + spaced_by
45+
y = 0
46+
else
47+
gravity = Magick::SouthGravity
48+
x = 0
49+
y = dest_image.rows + spaced_by
50+
end
51+
results << ImageUtil.image_properties(source_image).merge(:x => x, :y => y)
52+
dest_image = ImageUtil.composite_images(dest_image, source_image, x, y)
53+
end
54+
@output[dest] = results
55+
dest_image.write(@image_path + dest)
56+
end
57+
58+
def output_file(configuration)
59+
File.open(@file_path, 'w') do |f|
60+
@output.each do |dest, results|
61+
results.each do |result|
62+
f.puts ".#{result[:name]}"
63+
f.puts " background: url(http://github.com/'/images/%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E#{%3C/span%3E%3Cspan%20class=pl-s1%3Edest%3C/span%3E%3Cspan%20class=pl-kos%3E}%3C/span%3E%3C/span%3E') no-repeat #{result[:x]}px #{result[:y]}px;"
64+
f.puts " width: #{result[:width]}px;"
65+
f.puts " height: #{result[:height]}px;"
66+
f.puts ""
67+
end
68+
end
69+
end
70+
end
71+
72+
protected
73+
74+
# reads config settings from the given path
75+
def read_sprite_config(path = nil)
76+
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
77+
begin
78+
config = File.open(config_path) {|f| YAML::load(f)}
79+
rescue => e
80+
puts "Unable to read sprite config: #{Sprite.root+"/"+config_path}"
81+
puts e.to_s
82+
end
83+
config
84+
rescue
85+
end
86+
end
87+
end

lib/sprite/image_combiner.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Sprite
2+
class ImageCombiner
3+
def initialize
4+
# avoid loading rmagick till the last possible moment
5+
require 'rmagick'
6+
end
7+
8+
def composite_images(dest_image, src_image, x, y)
9+
width = [src_image.columns + x, dest_image.columns].max
10+
height = [src_image.rows + y, dest_image.rows].max
11+
image = Magick::Image.new(width, height)
12+
image.opacity = Magick::MaxRGB
13+
14+
image.composite!(dest_image, 0, 0, Magick::OverCompositeOp)
15+
image.composite!(src_image, x, y, Magick::OverCompositeOp)
16+
image
17+
end
18+
19+
20+
# Image Utility Methods
21+
def get_image(image_filename)
22+
image = Magick::Image::read(image_filename).first
23+
end
24+
25+
def image_properties(image)
26+
{:name => File.basename(image.filename).split('.')[0], :width => image.columns, :height => image.rows}
27+
end
28+
29+
end
30+
end

lib/sprite/sprite.rb

Lines changed: 0 additions & 90 deletions
This file was deleted.

spec/resources/configs/config1.yml

Whitespace-only changes.

spec/spec_helper.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
require 'spec/autorun'
33
require 'date'
44

5-
RAILS_ROOT = '.'
6-
require File.join(File.dirname(__FILE__), '/../lib/sprite.rb')
5+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "/../lib"))
6+
7+
require 'sprite'
8+
9+
# set Sprite.root to be this spec/ folder
10+
Sprite.module_eval{ @root = File.dirname(__FILE__) }

spec/sprite/image_combiner_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require File.dirname(__FILE__) + '/../spec_helper'
2+
3+
describe Sprite do
4+
before(:all) do
5+
# build a sprite object with empty config
6+
@combiner = Sprite::ImageCombiner.new
7+
8+
@image_paths = {
9+
:good_topic => "#{Sprite.root}/resources/images/topics/good_topic.gif",
10+
:mid_topic => "#{Sprite.root}/resources/images/topics/mid_topic.gif"
11+
}
12+
end
13+
14+
context "image handling" do
15+
context "get_image" do
16+
it "should get a image" do
17+
@combiner.get_image(@image_paths[:good_topic]).class.should == Magick::Image
18+
end
19+
end
20+
21+
context "image_properties" do
22+
it "should get image properties" do
23+
image = @combiner.get_image(@image_paths[:good_topic])
24+
@combiner.image_properties(image).should == {:name => 'good_topic', :width => 20, :height => 19}
25+
end
26+
end
27+
28+
context "composite_images" do
29+
it "should composite two images into one horizontally" do
30+
image1 = @combiner.get_image(@image_paths[:good_topic])
31+
image2 = @combiner.get_image(@image_paths[:mid_topic])
32+
image = @combiner.composite_images(image1, image2, image1.columns, 0)
33+
@combiner.image_properties(image).should == {:name => nil, :width => 40, :height => 19}
34+
end
35+
36+
it "should composite two images into one verically" do
37+
image1 = @combiner.get_image(@image_paths[:good_topic])
38+
image2 = @combiner.get_image(@image_paths[:mid_topic])
39+
image = @combiner.composite_images(image1, image2, 0, image1.rows)
40+
@combiner.image_properties(image).should == {:name => nil, :width => 20, :height => 38}
41+
end
42+
end
43+
end
44+
end

spec/sprite/sprite_config_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require File.dirname(__FILE__) + '/../spec_helper.rb'
2+
3+
describe Sprite do
4+
5+
context "Verify Settings" do
6+
before(:all) do
7+
@sprite = Sprite.new
8+
end
9+
end
10+
11+
12+
end

spec/sprite_spec.rb

Lines changed: 0 additions & 36 deletions
This file was deleted.

tasks/sprite_tasks.rake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
require File.join(File.dirname(__FILE__), '../lib/sprite/sprite.rb')
2+
13
namespace :sprite do
24
desc "build sprite images based on config/sprite.yml"
35
task :build do
4-
require File.join(File.dirname(__FILE__), '../lib/sprite/sprite.rb')
56
Sprite.new.build
67
end
8+
9+
task :list => [:build] do
10+
# TODO
11+
# list all the currently configured sprites
12+
end
713
end

0 commit comments

Comments
 (0)