Permalink
Browse files

namespaceing everything so it will play nicely with other libraries.

  • Loading branch information...
1 parent 506abdb commit 0bd040875c0bebb1602cc781b084bbca327e3c42 @aberant committed Mar 23, 2011
@@ -1,96 +1,98 @@
-class DirectoryProcessor
-
- DEFAULT_TEMPLATE = <<-EOF
- .<name>_<image_name> {
- background: transparent url(<image_loc>) <offset>px 0px no-repeat;
- width:<width>;
- height:<height>;
- text-indent:-5000px;
- }
-
- EOF
-
- def initialize(dir, options = {})
- @options = options
- @dir = dir
- @sprite = Sprite.new
- @tracker = MtimeTracker.new(@dir,
- :exclude => ['sprite.css', 'fragment.css', 'sprite.png'])
- end
+module CssSpriter
+ class DirectoryProcessor
+
+ DEFAULT_TEMPLATE = <<-EOF
+ .<name>_<image_name> {
+ background: transparent url(<image_loc>) <offset>px 0px no-repeat;
+ width:<width>;
+ height:<height>;
+ text-indent:-5000px;
+ }
+
+ EOF
+
+ def initialize(dir, options = {})
+ @options = options
+ @dir = dir
+ @sprite = Sprite.new
+ @tracker = MtimeTracker.new(@dir,
+ :exclude => ['sprite.css', 'fragment.css', 'sprite.png'])
+ end
- def images
- Dir.glob(@dir + "/*.png").reject{|i| i.match /sprite\.png/}
- end
+ def images
+ Dir.glob(@dir + "/*.png").reject{|i| i.match /sprite\.png/}
+ end
- def write
- return unless @tracker.has_changes?
- images.each {|f| @sprite.append_file(f)}
+ def write
+ return unless @tracker.has_changes?
+ images.each {|f| @sprite.append_file(f)}
- @sprite.write(sprite_file)
+ @sprite.write(sprite_file)
- File.open(css_file, 'w') do |f|
- f.write(css)
+ File.open(css_file, 'w') do |f|
+ f.write(css)
+ end
+ @tracker.update
end
- @tracker.update
- end
- def cleanup
- File.delete(sprite_file) rescue nil
- File.delete(css_file) rescue nil
- @tracker.cleanup
- end
+ def cleanup
+ File.delete(sprite_file) rescue nil
+ File.delete(css_file) rescue nil
+ @tracker.cleanup
+ end
- def sprite_file
- @dir + "/sprite.png"
- end
+ def sprite_file
+ @dir + "/sprite.png"
+ end
- def css_file
- @dir + "/fragment.css"
- end
+ def css_file
+ @dir + "/fragment.css"
+ end
- def dir_name
- @dir.split('/').last
- end
+ def dir_name
+ @dir.split('/').last
+ end
- def image_loc
- #TODO: Lame!
+ def image_loc
+ #TODO: Lame!
- dir = truncate_abs_path
- base = ("/" + dir + "/sprite.png").gsub(/^\/.\//, "/").gsub("//", "/")
- source = @options[:source]
- base = base.gsub(source, "") if source && source != "."
- base = @options[:path_prefix] + base if @options[:path_prefix]
- base
- end
+ dir = truncate_abs_path
+ base = ("/" + dir + "/sprite.png").gsub(/^\/.\//, "/").gsub("//", "/")
+ source = @options[:source]
+ base = base.gsub(source, "") if source && source != "."
+ base = @options[:path_prefix] + base if @options[:path_prefix]
+ base
+ end
- def truncate_abs_path
- return @dir unless @options[:source]
- path_elements = @options[:source].split('/')
- path_elements.pop #we want to remove everything above the root
- to_truncate = path_elements.join("/")
- @dir.gsub(to_truncate, "")
- end
+ def truncate_abs_path
+ return @dir unless @options[:source]
+ path_elements = @options[:source].split('/')
+ path_elements.pop #we want to remove everything above the root
+ to_truncate = path_elements.join("/")
+ @dir.gsub(to_truncate, "")
+ end
- def template_file
- @dir + "/template.css"
- end
+ def template_file
+ @dir + "/template.css"
+ end
- def template
- if File.exists?(template_file)
- return File.read(template_file)
+ def template
+ if File.exists?(template_file)
+ return File.read(template_file)
+ end
+ return DEFAULT_TEMPLATE
end
- return DEFAULT_TEMPLATE
- end
- def css
- @sprite.locations.inject("") do |out, image|
- image_name, properties = image
- out << template.gsub("<name>", dir_name).
- gsub("<image_name>", image_name.to_s).
- gsub("<width>", properties[:width].to_s).
- gsub("<height>", properties[:height].to_s).
- gsub("<offset>", properties[:x].to_s).
- gsub("<image_loc>", image_loc)
+ def css
+ @sprite.locations.inject("") do |out, image|
+ image_name, properties = image
+ out << template.gsub("<name>", dir_name).
+ gsub("<image_name>", image_name.to_s).
+ gsub("<width>", properties[:width].to_s).
+ gsub("<height>", properties[:height].to_s).
+ gsub("<offset>", properties[:x].to_s).
+ gsub("<image_loc>", image_loc)
+ end
end
end
-end
+end
@@ -1,80 +1,82 @@
-class MtimeTracker
- def initialize(dir, options = {})
- @dir = dir
- @options = options
- end
+module CssSpriter
+ class MtimeTracker
+ def initialize(dir, options = {})
+ @dir = dir
+ @options = options
+ end
- def cleanup
- File.delete(mtime_file) rescue nil
- end
+ def cleanup
+ File.delete(mtime_file) rescue nil
+ end
- def fresh?
- not File.exists?(mtime_file)
- end
+ def fresh?
+ not File.exists?(mtime_file)
+ end
- def files
- @files = without_exclusions(Dir.glob(@dir + "/**/*"))
- end
+ def files
+ @files = without_exclusions(Dir.glob(@dir + "/**/*"))
+ end
- def without_exclusions(files)
- return files unless @options[:exclude]
- exclusions = [@options[:exclude]].flatten
- files.select{|f| not exclusions.any?{|e| f.match e}}
- end
+ def without_exclusions(files)
+ return files unless @options[:exclude]
+ exclusions = [@options[:exclude]].flatten
+ files.select{|f| not exclusions.any?{|e| f.match e}}
+ end
- def current_mtimes
- @current ||= files.inject({}) do |map, f|
- map[f] = File.mtime(f).to_i; map
+ def current_mtimes
+ @current ||= files.inject({}) do |map, f|
+ map[f] = File.mtime(f).to_i; map
+ end
end
- end
- def file_changed?(file)
- mtimes[file] != current_mtimes[file]
- end
+ def file_changed?(file)
+ mtimes[file] != current_mtimes[file]
+ end
- def mtimes
- return @mtimes if @mtimes
- return {} unless File.exists?(mtime_file)
- @mtimes = read_mtimes
- end
+ def mtimes
+ return @mtimes if @mtimes
+ return {} unless File.exists?(mtime_file)
+ @mtimes = read_mtimes
+ end
- def read_mtimes
- mtimes = {}
- File.open(mtime_file) do |f|
- f.each do |line|
- name, time = line.split("\t")
- mtimes[name] = time.to_i
+ def read_mtimes
+ mtimes = {}
+ File.open(mtime_file) do |f|
+ f.each do |line|
+ name, time = line.split("\t")
+ mtimes[name] = time.to_i
+ end
end
+ mtimes
end
- mtimes
- end
- def changeset
- changed = files.select{|f| file_changed?(f)}
- deleted = without_exclusions(mtimes.keys) - current_mtimes.keys
- changed + deleted
- end
+ def changeset
+ changed = files.select{|f| file_changed?(f)}
+ deleted = without_exclusions(mtimes.keys) - current_mtimes.keys
+ changed + deleted
+ end
- def has_changes?
- not changeset.empty?
- end
+ def has_changes?
+ not changeset.empty?
+ end
- def mtime_file
- @dir + "/.mtimes"
- end
+ def mtime_file
+ @dir + "/.mtimes"
+ end
- def update
- File.open(mtime_file, 'w') do |f|
- current = current_mtimes
- flat = current.map{|k, v| "#{k}\t#{v}\n"}.join
- f.write flat
+ def update
+ File.open(mtime_file, 'w') do |f|
+ current = current_mtimes
+ flat = current.map{|k, v| "#{k}\t#{v}\n"}.join
+ f.write flat
+ end
+ reset
end
- reset
- end
- def reset
- @mtimes = nil
- @current = nil
- @files = nil
+ def reset
+ @mtimes = nil
+ @current = nil
+ @files = nil
+ end
end
-end
+end
Oops, something went wrong.

0 comments on commit 0bd0408

Please sign in to comment.