Skip to content

Commit 5c7e5bc

Browse files
committed
Updating Builder to handle proper configuration
1 parent 0f92b54 commit 5c7e5bc

File tree

6 files changed

+121
-36
lines changed

6 files changed

+121
-36
lines changed

lib/sprite.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# set up Sprite module
21
module Sprite
2+
3+
# provides the root directory to use when reading and writing files
34
def self.root
45
@root ||= nil
56

lib/sprite/builder.rb

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,43 @@ class Builder
44
DEFAULT_IMAGE_PATH = 'public/images/'
55
DEFAULT_FILE_PATH = 'tmp/sprite.css'
66

7-
def initialize(settings = nil)
8-
@image_path = DEFAULT_IMAGE_PATH
9-
10-
if settings.is_a?(Hash)
11-
@sprite_config = settings
7+
attr_reader :config
8+
attr_reader :images
9+
10+
def initialize(config = nil, images = nil)
11+
results = config_results
12+
13+
# use the override
14+
if config.is_a?(Hash)
15+
@config = config
16+
else
17+
@config = config["config"] || {}
18+
end
19+
20+
# set defaults
21+
set_config_defaults
22+
23+
# default image list
24+
if images.is_a?(Array)
25+
@images = images
1226
else
13-
@sprite_config = read_sprite_config(settings)
27+
@images = config["images"] || []
1428
end
29+
30+
# process images
31+
process_image_settings
1532
end
1633

1734
def build
1835
@output = {}
1936

20-
# build up settings
37+
# build up config
2138
@image_path = sprite_config['config']['base_image_path'] ? Sprite.root+"/"+sprite_config['config']['base_image_path']+"/" : DEFAULT_IMAGE_PATH
2239
@file_path = sprite_config['config']['output_file'] ? Sprite.root+"/"+sprite_config['config']['output_file'] : DEFAULT_FILE_PATH
2340

2441
# create images
2542
sprite_config['images'].each do |configuration|
26-
output_image(configuration)
43+
output_image(configuration)
2744
end
2845

2946
# write css
@@ -32,24 +49,26 @@ def build
3249

3350
def output_image(configuration)
3451
results = []
35-
sources = configuration['sources'].collect {|source| Dir.glob(@image_path+source)}.flatten
52+
sources = configuration['sources'].to_a
53+
3654
dest = configuration['target'] || sources[0].gsub(/\./,"_sprite.")
3755
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)
56+
57+
combiner = ImageCombiner.new
58+
59+
dest_image = combiner.get_image(sources.shift)
60+
results << combiner.image_properties(dest_image).merge(:x => 0, :y => 0)
4061
sources.each do |source|
41-
source_image = ImageUtil.get_image(source)
42-
if configuration['align'] == 'horizontal'
43-
gravity = Magick::EastGravity
62+
source_image = combiner.get_image(source)
63+
if configuration['align'].to_s == 'horizontal'
4464
x = dest_image.columns + spaced_by
4565
y = 0
4666
else
47-
gravity = Magick::SouthGravity
4867
x = 0
4968
y = dest_image.rows + spaced_by
5069
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)
70+
results << combiner.image_properties(source_image).merge(:x => x, :y => y)
71+
dest_image = combiner.composite_images(dest_image, source_image, x, y)
5372
end
5473
@output[dest] = results
5574
dest_image.write(@image_path + dest)
@@ -71,17 +90,50 @@ def output_file(configuration)
7190

7291
protected
7392

74-
# reads config settings from the given path
75-
def read_sprite_config(path = nil)
93+
# reads config config from the given path
94+
def read_config(path = nil)
95+
config_results = {}
7696
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
7797
begin
78-
config = File.open(config_path) {|f| YAML::load(f)}
98+
config_results = File.open(config_path) {|f| YAML::load(f)}
7999
rescue => e
80100
puts "Unable to read sprite config: #{Sprite.root+"/"+config_path}"
81101
puts e.to_s
82102
end
83-
config
84-
rescue
103+
config_results
85104
end
105+
106+
# sets all the default values on the config
107+
def set_config_defaults
108+
@config['style'] ||= ''
109+
@config['output_path'] ||= 'public/sass/mixins/sprites'
110+
@config['image_output_path'] ||= 'public/images/sprite/'
111+
@config['source_path'] ||= ''
112+
@config['default_format'] ||= 'png'
113+
@config['class_separator'] ||= '_'
114+
end
115+
116+
# expands out sources
117+
def process_image_config
118+
# cycle through image sources and expand out globs
119+
@images.each do |image|
120+
# find all the files
121+
image['sources'] = image['sources'].to_a.map do |source|
122+
Dir.glob(File.join(Sprite.root, @config['source_path'], source))
123+
end
124+
125+
# remove the prefix on them
126+
new_sources = new_sources.flatten.map do |source|
127+
source.gsub!(Sprite.root, "")
128+
end
129+
130+
image_config['sources'] = new_sources
131+
end
132+
133+
rescue => e
134+
puts "Invalid sprite configuration syntax:"
135+
puts e.to_s
136+
end
137+
86138
end
87139
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
config:
2+
style: css
3+
output_path: output/stylesheets/sprites.css
4+
image_output_path: output/images/sprites/
5+
source_path: resources/images/
6+
class_separator: '_'
7+
default_format: png
8+
9+
# defines what sprite collections get created
10+
images:
11+
- name: android
12+
format: png
13+
align: vertical
14+
spaced_by: 50
15+
sources:
16+
- android/*.png
17+
18+
- name: topics
19+
format: gif
20+
align: vertical
21+
spaced_by: 50
22+
sources:
23+
- topics/good_topic.gif
24+
- topics/mid_topic.gif

spec/sprite/builder_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require File.dirname(__FILE__) + '/../spec_helper.rb'
2+
3+
describe Sprite::Builder do
4+
5+
context "Configuration Parsing" do
6+
before(:all) do
7+
@sprite = Sprite::Builder.new("resources/configs/full_config.yml")
8+
end
9+
10+
it "should load the settings keys from file" do
11+
@sprite.config.keys.size.should == 2
12+
end
13+
14+
it "should glob the files together" do
15+
@sprite.config["images"].first["sources"].size.should == 30
16+
end
17+
18+
end
19+
20+
end

spec/sprite/image_combiner_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require File.dirname(__FILE__) + '/../spec_helper'
22

3-
describe Sprite do
3+
describe Sprite::ImageCombiner do
44
before(:all) do
55
# build a sprite object with empty config
66
@combiner = Sprite::ImageCombiner.new

spec/sprite/sprite_config_spec.rb

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

0 commit comments

Comments
 (0)