Skip to content

Commit 4566609

Browse files
committed
Updating Default Settings for Sprite::Builder
1 parent 090d126 commit 4566609

File tree

3 files changed

+68
-37
lines changed

3 files changed

+68
-37
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ Configuration of `sprite` is done via `config/sprite.yml`. It allows you to set
8888

8989
* `config:` section defines all the global properties for sprite generation. Such as how it generates the styles, where it looks for images, where it writes it output file to, and what image file format it uses by default
9090
- `style:` defines how the style rules are outputted. built in options are `css`, `sass`, and `sass_mixin`. (defaults to `css`)
91-
- `output_path:` defines the file path where your style settings get written. the file extension not needed as it will be determined by the style setting above (defaults to `public/stylesheets/sprite`)
92-
- `image_output_path:` defines the folder path where the combined sprite images files are written (defaults to `public/images/sprite/`)
91+
- `output_path:` defines the file path where your style settings get written (defaults to `public/stylesheets/sprites`). the file extension not needed as it will be set based on the `style:` setting
92+
- `image_output_path:` defines the folder path where the combined sprite images files are written (defaults to `public/images/sprites/`)
9393
- `source_path:` defines the folder where source image files are read from (defaults to `public/images/`)
9494
- `default_format:` defines the default file image format of the generated files. (defaults to `png`)
9595
- `class_separator:` used within the generated class names between the image name and sprite name (defaults to `_`)

lib/sprite/builder.rb

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ class Builder
66

77
attr_reader :config
88
attr_reader :images
9+
attr_reader :output
10+
11+
def self.from_config(path)
12+
results = read_config(path)
13+
new(results["config"], results["images"])
14+
end
915

1016
def initialize(config = nil, images = nil)
11-
results = config_results
17+
results = self.class.read_config unless config.is_a?(Hash) and images.is_a?(Array)
1218

1319
# use the override
1420
if config.is_a?(Hash)
1521
@config = config
1622
else
17-
@config = config["config"] || {}
23+
@config = results["config"] || {}
1824
end
1925

2026
# set defaults
@@ -24,30 +30,32 @@ def initialize(config = nil, images = nil)
2430
if images.is_a?(Array)
2531
@images = images
2632
else
27-
@images = config["images"] || []
33+
@images = results["images"] || []
2834
end
2935

30-
# process images
31-
process_image_settings
36+
expand_image_paths
37+
38+
# initialize output
39+
@output = {}
3240
end
3341

34-
def build
42+
def build
3543
@output = {}
36-
44+
3745
# build up config
38-
@image_path = sprite_config['config']['base_image_path'] ? Sprite.root+"/"+sprite_config['config']['base_image_path']+"/" : DEFAULT_IMAGE_PATH
39-
@file_path = sprite_config['config']['output_file'] ? Sprite.root+"/"+sprite_config['config']['output_file'] : DEFAULT_FILE_PATH
46+
# @image_path = sprite_config['base_image_path'] ? Sprite.root+"/"+sprite_config['config']['base_image_path']+"/" : DEFAULT_IMAGE_PATH
47+
# @file_path = sprite_config['config']['output_file'] ? Sprite.root+"/"+sprite_config['config']['output_file'] : DEFAULT_FILE_PATH
4048

4149
# create images
42-
sprite_config['images'].each do |configuration|
43-
output_image(configuration)
50+
images.each do |image|
51+
output_image(image)
4452
end
4553

4654
# write css
47-
output_css(sprite_config)
55+
output_file
4856
end
4957

50-
def output_image(configuration)
58+
def output_image(image)
5159
results = []
5260
sources = configuration['sources'].to_a
5361

@@ -91,7 +99,7 @@ def output_file(configuration)
9199
protected
92100

93101
# reads config config from the given path
94-
def read_config(path = nil)
102+
def self.read_config(path = nil)
95103
config_results = {}
96104
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
97105
begin
@@ -105,34 +113,29 @@ def read_config(path = nil)
105113

106114
# sets all the default values on the config
107115
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'] ||= ''
116+
@config['style'] ||= 'css'
117+
@config['output_path'] ||= 'public/stylesheets/sprites'
118+
@config['image_output_path'] ||= 'public/images/sprites/'
119+
@config['source_path'] ||= 'public/images/'
112120
@config['default_format'] ||= 'png'
113121
@config['class_separator'] ||= '_'
114122
end
115123

116124
# expands out sources
117-
def process_image_config
125+
def expand_image_paths
118126
# cycle through image sources and expand out globs
119127
@images.each do |image|
120-
# find all the files
121-
image['sources'] = image['sources'].to_a.map do |source|
128+
129+
# expand out all the globs
130+
image['sources'] = image['sources'].to_a.map{ |source|
122131
Dir.glob(File.join(Sprite.root, @config['source_path'], source))
123-
end
132+
}.flatten.compact
124133

125-
# remove the prefix on them
126-
new_sources = new_sources.flatten.map do |source|
134+
# remove the prefixes on them
135+
image['sources'].each do |source|
127136
source.gsub!(Sprite.root, "")
128137
end
129-
130-
image_config['sources'] = new_sources
131138
end
132-
133-
rescue => e
134-
puts "Invalid sprite configuration syntax:"
135-
puts e.to_s
136139
end
137140

138141
end

spec/sprite/builder_spec.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,46 @@
22

33
describe Sprite::Builder do
44

5-
context "Configuration Parsing" do
5+
context "configuration parsing" do
66
before(:all) do
7-
@sprite = Sprite::Builder.new("resources/configs/full_config.yml")
7+
@sprite = Sprite::Builder.from_config("resources/configs/full_config.yml")
88
end
99

1010
it "should load the settings keys from file" do
11-
@sprite.config.keys.size.should == 2
11+
@sprite.config.keys.size.should == 6
12+
end
13+
14+
it "should load the image keys from file" do
15+
@sprite.images.size.should == 2
16+
end
17+
18+
it "should expand any globs within the source paths" do
19+
@sprite.images.first["sources"].size.should == 30
1220
end
1321

14-
it "should glob the files together" do
15-
@sprite.config["images"].first["sources"].size.should == 30
22+
end
23+
24+
context "default settings" do
25+
before(:all) do
26+
@sprite = Sprite::Builder.new
27+
end
28+
29+
it "'style:' setting should default to 'css'" do
30+
@sprite.config['style'].should == "css"
31+
end
32+
33+
it "'output_path:' setting should default to 'public/stylesheets/sprites'" do
34+
@sprite.config['output_path'].should == "public/stylesheets/sprites"
35+
end
36+
37+
it "'image_output_path:' setting should default to 'public/images/sprites/" do
38+
@sprite.config['image_output_path'].should == "public/images/sprites/"
39+
end
40+
41+
it "'source_path:' setting should default to 'public/images/" do
42+
@sprite.config['source_path'].should == "public/images/"
1643
end
44+
1745

1846
end
1947

0 commit comments

Comments
 (0)