Class: DirectoryProcessor

Inherits:
Object
  • Object
show all
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

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 = {})
  @options = options
  @dir = dir
  @sprite = Sprite.new
  @tracker = MtimeTracker.new(@dir,
                              :exclude => ['sprite.css', 'fragment.css', 'sprite.png'])
end

Instance Method Details

#cleanupObject



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

#cssObject



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_fileObject



45
46
47
# File 'lib/spittle/directory_processor.rb', line 45

def css_file
  @dir + "/fragment.css"
end

#dir_nameObject



49
50
51
# File 'lib/spittle/directory_processor.rb', line 49

def dir_name
  @dir.split('/').last
end

#image_locObject



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

#imagesObject



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_fileObject



41
42
43
# File 'lib/spittle/directory_processor.rb', line 41

def sprite_file
  @dir + "/sprite.png"
end

#templateObject



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_fileObject



72
73
74
# File 'lib/spittle/directory_processor.rb', line 72

def template_file
  @dir + "/template.css"
end

#truncate_abs_pathObject



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

#writeObject



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