css_sprite 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.2
1
+ 1.3.3
data/css_sprite.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{css_sprite}
8
- s.version = "1.3.2"
8
+ s.version = "1.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
data/lib/automatic.rb CHANGED
@@ -8,8 +8,9 @@ class Rails
8
8
  end
9
9
  end
10
10
 
11
+ sprite = Sprite.new
11
12
  loop do
12
13
  sleep 1
13
14
 
14
- Sprite.new.check
15
+ sprite.check
15
16
  end
@@ -6,8 +6,8 @@ require 'enumerator'
6
6
  class Sprite
7
7
 
8
8
  def initialize(options={})
9
- @image_path = File.join(Rails.root, 'public/images')
10
- @stylesheet_path = File.join(Rails.root, 'public/stylesheets')
9
+ @image_path = File.expand_path(File.join(Rails.root, 'public/images'))
10
+ @stylesheet_path = File.expand_path(File.join(Rails.root, 'public/stylesheets'))
11
11
  @todo = {}
12
12
 
13
13
  if File.exist?(File.join(Rails.root, 'config/css_sprite.yml'))
@@ -67,13 +67,13 @@ class Sprite
67
67
  span = 5
68
68
  unless sources.empty?
69
69
  dest_image = get_image(sources.shift)
70
- results << image_properties(dest_image).merge(:x => 0, :y => 0)
70
+ results << image_properties(dest_image, directory).merge(:x => 0, :y => 0)
71
71
  sources.each do |source|
72
72
  source_image = get_image(source)
73
73
  gravity = Magick::SouthGravity
74
74
  x = 0
75
75
  y = dest_image.rows + span
76
- results << image_properties(source_image).merge(:x => x, :y => y)
76
+ results << image_properties(source_image, directory).merge(:x => x, :y => y)
77
77
  dest_image = composite_images(dest_image, source_image, x, y)
78
78
  end
79
79
  dest_image.image_type = Magick::PaletteMatteType
@@ -104,7 +104,7 @@ class Sprite
104
104
  f.print " \{\n background: url('/images/#{dest_image_name}?#{Time.now.to_i}') no-repeat;\n\}\n"
105
105
 
106
106
  results.each do |result|
107
- f.print ".#{result[:name]} \{"
107
+ f.print "#{class_name(result[:name])} \{"
108
108
  f.print " background-position: #{-result[:x]}px #{-result[:y]}px;"
109
109
  f.print " width: #{result[:width]}px;"
110
110
  f.print " height: #{result[:height]}px;"
@@ -137,7 +137,7 @@ class Sprite
137
137
  f.print " \n background: url('/images/#{dest_image_name}?#{Time.now.to_i}') no-repeat\n"
138
138
 
139
139
  results.each do |result|
140
- f.print ".#{result[:name]}\n"
140
+ f.print "#{class_name(result[:name])}\n"
141
141
  f.print " background-position: #{-result[:x]}px #{-result[:y]}px\n"
142
142
  f.print " width: #{result[:width]}px\n"
143
143
  f.print " height: #{result[:height]}px\n"
@@ -152,11 +152,15 @@ class Sprite
152
152
  class_names = []
153
153
  results = results.select { |result| result[:name] =~ %r|#{options[:suffix]}$| } if options[:suffix]
154
154
  results.each_slice(options[:count_per_line]) do |batch_results|
155
- class_names << batch_results.collect { |result| ".#{result[:name]}" }.join(', ')
155
+ class_names << batch_results.collect { |result| class_name(result[:name]) }.join(', ')
156
156
  end
157
157
  class_names
158
158
  end
159
159
 
160
+ def class_name(name)
161
+ ".#{name.gsub('/', ' .')}"
162
+ end
163
+
160
164
  def all_images(directory)
161
165
  images = []
162
166
  Find.find(directory) do |path|
@@ -196,8 +200,10 @@ class Sprite
196
200
  Magick::Image::read(image_filename).first
197
201
  end
198
202
 
199
- def image_properties(image)
200
- {:name => File.basename(image.filename, File.extname(image.filename)), :width => image.columns, :height => image.rows}
203
+ def image_properties(image, directory)
204
+ directory_length = directory.length + 1
205
+ extname_length = File.extname(image.filename).length
206
+ {:name => image.filename.slice(directory_length...-extname_length), :width => image.columns, :height => image.rows}
201
207
  end
202
208
 
203
209
  end
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
3
3
  describe Sprite do
4
4
  before(:all) do
5
5
  @sprite = Sprite.new
6
+ @directory_path = File.join(IMAGE_PATH, 'css_sprite')
6
7
  end
7
8
 
8
9
  describe "build" do
@@ -94,24 +95,29 @@ describe Sprite do
94
95
 
95
96
  describe "image_properties" do
96
97
  it "should get image properties" do
97
- image = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/gmail_logo.png'))
98
- @sprite.image_properties(image).should == {:name => 'gmail_logo', :width => 103, :height => 36}
98
+ image = @sprite.get_image(File.join(@directory_path, 'gmail_logo.png'))
99
+ @sprite.image_properties(image, @directory_path).should == {:name => 'gmail_logo', :width => 103, :height => 36}
100
+ end
101
+
102
+ it "should get a image with parent" do
103
+ image = @sprite.get_image(File.join(@directory_path, 'icons/twitter_icon.png'))
104
+ @sprite.image_properties(image, @directory_path).should == {:name => 'icons/twitter_icon', :width => 14, :height => 14}
99
105
  end
100
106
  end
101
107
 
102
108
  describe "composite_images" do
103
109
  it "should composite two images into one horizontally" do
104
- image1 = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/gmail_logo.png'))
105
- image2 = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/hotmail_logo.png'))
110
+ image1 = @sprite.get_image(File.join(@directory_path, 'gmail_logo.png'))
111
+ image2 = @sprite.get_image(File.join(@directory_path, 'hotmail_logo.png'))
106
112
  image = @sprite.composite_images(image1, image2, image1.columns, 0)
107
- @sprite.image_properties(image).should == {:name => "", :width => 206, :height => 36}
113
+ @sprite.image_properties(image, @directory_path).should == {:name => nil, :width => 206, :height => 36}
108
114
  end
109
115
 
110
116
  it "should composite two images into one verically" do
111
- image1 = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/gmail_logo.png'))
112
- image2 = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/hotmail_logo.png'))
117
+ image1 = @sprite.get_image(File.join(@directory_path, 'gmail_logo.png'))
118
+ image2 = @sprite.get_image(File.join(@directory_path, 'hotmail_logo.png'))
113
119
  image = @sprite.composite_images(image1, image2, 0, image1.rows)
114
- @sprite.image_properties(image).should == {:name => "", :width => 103, :height => 72}
120
+ @sprite.image_properties(image, @directory_path).should == {:name => nil, :width => 103, :height => 72}
115
121
  end
116
122
  end
117
123
  end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,6 @@ class Rails
8
8
  end
9
9
  end
10
10
 
11
- IMAGE_PATH = File.join(Rails.root, 'public/images')
12
- STYLESHEET_PATH = File.join(Rails.root, 'public/stylesheets')
11
+ IMAGE_PATH = File.expand_path(File.join(Rails.root, 'public/images'))
12
+ STYLESHEET_PATH = File.expand_path(File.join(Rails.root, 'public/stylesheets'))
13
13
  require File.join(File.dirname(__FILE__), '/../lib/css_sprite.rb')
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 3
8
- - 2
9
- version: 1.3.2
8
+ - 3
9
+ version: 1.3.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Richard Huang