Class: DirectoryProcessor
- Inherits:
-
Object
- Object
- DirectoryProcessor
- Defined in:
- lib/spittle/directory_processor.rb
Constant Summary collapse
- DEFAULT_TEMPLATE =
<<-EOF .<name>_<image_name> { background: transparent url(<image_loc>) <offset>px 0px no-repeat; width:<width>; height:<height>; text-indent:-5000px; } EOF
Instance Method Summary collapse
- #cleanup ⇒ Object
- #css ⇒ Object
- #css_file ⇒ Object
- #dir_name ⇒ Object
- #image_loc ⇒ Object
- #images ⇒ Object
-
#initialize(dir, options = {}) ⇒ DirectoryProcessor
constructor
A new instance of DirectoryProcessor.
- #sprite_file ⇒ Object
- #template ⇒ Object
- #template_file ⇒ Object
- #truncate_abs_path ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(dir, options = {}) ⇒ DirectoryProcessor
Returns a new instance of DirectoryProcessor.
13 14 15 16 17 18 19 |
# File 'lib/spittle/directory_processor.rb', line 13 def initialize(dir, = {}) @options = @dir = dir @sprite = Sprite.new @tracker = MtimeTracker.new(@dir, :exclude => ['sprite.css', 'fragment.css', 'sprite.png']) end |
Instance Method Details
#cleanup ⇒ Object
35 36 37 38 39 |
# File 'lib/spittle/directory_processor.rb', line 35 def cleanup File.delete(sprite_file) rescue nil File.delete(css_file) rescue nil @tracker.cleanup end |
#css ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/spittle/directory_processor.rb', line 83 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 |
#css_file ⇒ Object
45 46 47 |
# File 'lib/spittle/directory_processor.rb', line 45 def css_file @dir + "/fragment.css" end |
#dir_name ⇒ Object
49 50 51 |
# File 'lib/spittle/directory_processor.rb', line 49 def dir_name @dir.split('/').last end |
#image_loc ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/spittle/directory_processor.rb', line 53 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 |
#images ⇒ Object
21 22 23 |
# File 'lib/spittle/directory_processor.rb', line 21 def images Dir.glob(@dir + "/*.png").reject{|i| i.match /sprite\.png/} end |
#sprite_file ⇒ Object
41 42 43 |
# File 'lib/spittle/directory_processor.rb', line 41 def sprite_file @dir + "/sprite.png" end |
#template ⇒ Object
76 77 78 79 80 81 |
# File 'lib/spittle/directory_processor.rb', line 76 def template if File.exists?(template_file) return File.read(template_file) end return DEFAULT_TEMPLATE end |
#template_file ⇒ Object
72 73 74 |
# File 'lib/spittle/directory_processor.rb', line 72 def template_file @dir + "/template.css" end |
#truncate_abs_path ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/spittle/directory_processor.rb', line 64 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 |
#write ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/spittle/directory_processor.rb', line 25 def write return unless @tracker.has_changes? images.each {|f| @sprite.append(PNG::Image.image_data(f))} @sprite.write(sprite_file) File.open(css_file, 'w') do |f| f.write(css) end @tracker.update end |