Skip to content

Commit 04dd536

Browse files
committed
Code cleanup and Builder fixes
1 parent 4566609 commit 04dd536

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
### Install the `rmagick` gem ###
88

9-
sprite currently requires the rmagick gem. to install it, use
9+
`sprite` currently requires the rmagick gem. to install it, use
1010

1111
gem install rmagick
1212

@@ -19,7 +19,7 @@ or via installer: http://github.com/maddox/magick-installer/tree/master
1919

2020
### Install the `sprite` gem ###
2121

22-
Install the sprite gem from gemcutter
22+
Install the `sprite` gem from gemcutter
2323

2424
gem sources -a http://gemcutter.org
2525
gem install sprite

lib/sprite/builder.rb

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
module Sprite
22
class Builder
33
DEFAULT_CONFIG_PATH = 'config/sprite.yml'
4-
DEFAULT_IMAGE_PATH = 'public/images/'
5-
DEFAULT_FILE_PATH = 'tmp/sprite.css'
64

75
attr_reader :config
86
attr_reader :images
97
attr_reader :output
108

11-
def self.from_config(path)
12-
results = read_config(path)
9+
def self.from_config(path)
10+
11+
# look up file
12+
results = {}
13+
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
14+
begin
15+
results = File.open(config_path) {|f| YAML::load(f)}
16+
rescue => e
17+
puts "Unable to read sprite config: #{Sprite.root+"/"+config_path}"
18+
puts e.to_s
19+
end
20+
1321
new(results["config"], results["images"])
1422
end
1523

1624
def initialize(config = nil, images = nil)
17-
results = self.class.read_config unless config.is_a?(Hash) and images.is_a?(Array)
18-
19-
# use the override
20-
if config.is_a?(Hash)
21-
@config = config
22-
else
23-
@config = results["config"] || {}
24-
end
25-
26-
# set defaults
25+
@config = config || {}
2726
set_config_defaults
28-
29-
# default image list
30-
if images.is_a?(Array)
31-
@images = images
32-
else
33-
@images = results["images"] || []
34-
end
3527

28+
@images = images || []
3629
expand_image_paths
3730

31+
# freeze hashes
32+
@config = @config.dup.freeze
33+
@images = @images.dup.freeze
34+
3835
# initialize output
3936
@output = {}
4037
end
@@ -95,21 +92,8 @@ def output_file(configuration)
9592
end
9693
end
9794
end
98-
95+
9996
protected
100-
101-
# reads config config from the given path
102-
def self.read_config(path = nil)
103-
config_results = {}
104-
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
105-
begin
106-
config_results = File.open(config_path) {|f| YAML::load(f)}
107-
rescue => e
108-
puts "Unable to read sprite config: #{Sprite.root+"/"+config_path}"
109-
puts e.to_s
110-
end
111-
config_results
112-
end
11397

11498
# sets all the default values on the config
11599
def set_config_defaults

spec/sprite/builder_spec.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
context "configuration parsing" do
66
before(:all) do
77
@sprite = Sprite::Builder.from_config("resources/configs/full_config.yml")
8-
end
8+
end
99

1010
it "should load the settings keys from file" do
1111
@sprite.config.keys.size.should == 6
@@ -34,14 +34,21 @@
3434
@sprite.config['output_path'].should == "public/stylesheets/sprites"
3535
end
3636

37-
it "'image_output_path:' setting should default to 'public/images/sprites/" do
37+
it "'image_output_path:' setting should default to 'public/images/sprites/'" do
3838
@sprite.config['image_output_path'].should == "public/images/sprites/"
3939
end
4040

41-
it "'source_path:' setting should default to 'public/images/" do
41+
it "'source_path:' setting should default to 'public/images/'" do
4242
@sprite.config['source_path'].should == "public/images/"
4343
end
44-
44+
45+
it "'default_format:' setting should default to 'png'" do
46+
@sprite.config['default_format'].should == "png"
47+
end
48+
49+
it "'class_separator:' setting should default to '_'" do
50+
@sprite.config['class_separator'].should == "_"
51+
end
4552

4653
end
4754

0 commit comments

Comments
 (0)