Class: CssSpriter::MtimeTracker
- Inherits:
-
Object
- Object
- CssSpriter::MtimeTracker
- Defined in:
- lib/css-spriter/mtime_tracker.rb
Instance Method Summary collapse
- #changeset ⇒ Object
- #cleanup ⇒ Object
- #current_mtimes ⇒ Object
- #file_changed?(file) ⇒ Boolean
- #files ⇒ Object
- #fresh? ⇒ Boolean
- #has_changes? ⇒ Boolean
-
#initialize(dir, options = {}) ⇒ MtimeTracker
constructor
A new instance of MtimeTracker.
- #mtime_file ⇒ Object
- #mtimes ⇒ Object
- #read_mtimes ⇒ Object
- #reset ⇒ Object
- #update ⇒ Object
- #without_exclusions(files) ⇒ Object
Constructor Details
#initialize(dir, options = {}) ⇒ MtimeTracker
Returns a new instance of MtimeTracker.
3 4 5 6 |
# File 'lib/css-spriter/mtime_tracker.rb', line 3 def initialize(dir, = {}) @dir = dir @options = end |
Instance Method Details
#changeset ⇒ Object
53 54 55 56 57 |
# File 'lib/css-spriter/mtime_tracker.rb', line 53 def changeset changed = files.select{|f| file_changed?(f)} deleted = without_exclusions(mtimes.keys) - current_mtimes.keys changed + deleted end |
#cleanup ⇒ Object
8 9 10 |
# File 'lib/css-spriter/mtime_tracker.rb', line 8 def cleanup File.delete(mtime_file) rescue nil end |
#current_mtimes ⇒ Object
26 27 28 29 30 |
# File 'lib/css-spriter/mtime_tracker.rb', line 26 def current_mtimes @current ||= files.inject({}) do |map, f| map[f] = File.mtime(f).to_i; map end end |
#file_changed?(file) ⇒ Boolean
32 33 34 |
# File 'lib/css-spriter/mtime_tracker.rb', line 32 def file_changed?(file) mtimes[file] != current_mtimes[file] end |
#files ⇒ Object
16 17 18 |
# File 'lib/css-spriter/mtime_tracker.rb', line 16 def files @files = without_exclusions(Dir.glob(@dir + "/**/*")) end |
#fresh? ⇒ Boolean
12 13 14 |
# File 'lib/css-spriter/mtime_tracker.rb', line 12 def fresh? not File.exists?(mtime_file) end |
#has_changes? ⇒ Boolean
59 60 61 |
# File 'lib/css-spriter/mtime_tracker.rb', line 59 def has_changes? not changeset.empty? end |
#mtime_file ⇒ Object
63 64 65 |
# File 'lib/css-spriter/mtime_tracker.rb', line 63 def mtime_file @dir + "/.mtimes" end |
#mtimes ⇒ Object
36 37 38 39 40 |
# File 'lib/css-spriter/mtime_tracker.rb', line 36 def mtimes return @mtimes if @mtimes return {} unless File.exists?(mtime_file) @mtimes = read_mtimes end |
#read_mtimes ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/css-spriter/mtime_tracker.rb', line 42 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 |
#reset ⇒ Object
76 77 78 79 80 |
# File 'lib/css-spriter/mtime_tracker.rb', line 76 def reset @mtimes = nil @current = nil @files = nil end |
#update ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/css-spriter/mtime_tracker.rb', line 67 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 |
#without_exclusions(files) ⇒ Object
20 21 22 23 24 |
# File 'lib/css-spriter/mtime_tracker.rb', line 20 def without_exclusions(files) return files unless @options[:exclude] exclusions = [@options[:exclude]].flatten files.select{|f| not exclusions.any?{|e| f.match e}} end |