module Chance
The Chance
factory. All methods require two parts: a key and a hash of options. The hash will be used in the case of a cache missed.
Spriting support.
The sprite method performs the spriting. It creates collections of images to sprite and then calls layout_sprite and generate_sprite.
The layout_sprite method arranges the slices, defining their positions within the sprites.
The generate_sprite method combines the slices into an image.
Constants
- CONFIG
- VERSION
Attributes
_current_instance[RW]
clear_files_immediately[RW]
files[RW]
Public Class Methods
add_file(path, content=nil)
click to toggle source
# File vendor/chance/lib/chance.rb, line 28 def add_file(path, content=nil) mtime = 0 if content.nil? mtime = File.mtime(path).to_f end if @files[path] return update_file_if_needed(path, content) end file = { :mtime => mtime, :path => path, :content => content, :preprocessed => false } @files[path] = file #puts "Added " + path if Chance::CONFIG[:verbose] end
get_file(path)
click to toggle source
# File vendor/chance/lib/chance.rb, line 107 def get_file(path) if not @files.has_key? path puts "Could not find " + path + " in Chance." return nil end file = @files[path] if file[:content].nil? # note: CSS files should be opened as UTF-8. f = File.open(path, path =~ /css$/ ? 'r:UTF-8' : 'rb') file[:content] = f.read f.close end if not file[:preprocessed] _preprocess file end return file end
has_file(path)
click to toggle source
# File vendor/chance/lib/chance.rb, line 102 def has_file(path) return false if not @files.has_key? path return true end
remove_all_files()
click to toggle source
Removes all files from Chance
; used to reset environment for testing.
# File vendor/chance/lib/chance.rb, line 98 def remove_all_files @files = {} end
remove_file(path)
click to toggle source
# File vendor/chance/lib/chance.rb, line 86 def remove_file(path) if not @files.has_key? path puts "Could not remove " + path + " because it is not in system." return end @files.delete(path) puts "Removed " + path if Chance::CONFIG[:verbose] end
update_file(path, content=nil)
click to toggle source
# File vendor/chance/lib/chance.rb, line 49 def update_file(path, content=nil) if not @files.has_key? path puts "Could not update " + path + " because it is not in system." return end mtime = 0 if content.nil? mtime = File.mtime(path).to_f end file = { :mtime => mtime, :path => path, :content => content, :preprocessed => false } @files[path] = file puts "Updated " + path if Chance::CONFIG[:verbose] end
update_file_if_needed(path, content=nil)
click to toggle source
if the path is a valid filesystem path and the mtime has changed, this invalidates the file. Returns the mtime if the file was updated.
# File vendor/chance/lib/chance.rb, line 73 def update_file_if_needed(path, content=nil) if @files[path] mtime = File.mtime(path).to_f if mtime > @files[path][:mtime] update_file(path, content) end return mtime else return false end end
Private Class Methods
_preprocess(file)
click to toggle source
# File vendor/chance/lib/chance.rb, line 130 def _preprocess(file) _preprocess_css(file) if file[:path] =~ /css$/ _preprocess_png(file) if file[:path] =~ /png$/ _preprocess_rmagick_image(file) if file[:path] =~ /gif$|jpg$/ file[:preprocessed] = true end
_preprocess_css(file)
click to toggle source
# File vendor/chance/lib/chance.rb, line 159 def _preprocess_css(file) content = file[:content] requires = [] content = content.gsub(/(sc_)?require\(['"]?(.*?)['"]?\);?/) {|match| requires.push $2 "" } # sc_resource will already be handled by the build tool. We just need to ignore it. content.gsub!(/sc_resource\(['"]?(.*?)['"]?\);?/, '') file[:requires] = requires file[:content] = content end
_preprocess_png(file)
click to toggle source
# File vendor/chance/lib/chance.rb, line 139 def _preprocess_png(file) file[:canvas] = ChunkyPNG::Canvas.from_blob(file[:content]) end
_preprocess_rmagick_image(file)
click to toggle source
# File vendor/chance/lib/chance.rb, line 143 def _preprocess_rmagick_image(file) file[:canvas] = nil begin require "rmagick" rescue LoadError file[:error] = "RMagick is require to process gifs and jpgs. (gem install rmagick)" return end begin file[:canvas] = Magick::Image.from_blob(file[:content])[0] rescue Exception => e file[:error] = e end end