Class: RubyXmlMapper::FileCacheItem

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-xml-mapper/file_cache_item.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileCacheItem

Returns a new instance of FileCacheItem.



18
19
20
21
# File 'lib/ruby-xml-mapper/file_cache_item.rb', line 18

def initialize filename
  @filename = filename
  @mutex = Mutex.new
end

Instance Method Details

#getObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-xml-mapper/file_cache_item.rb', line 23

def get
  raise(RubyXmlMapper::CircularReference, "in `#{@filename}'") if @mutex.locked?
  
  @mutex.synchronize do
    mtime = File.mtime(@filename)
    
    if @data && @mtime == mtime
      return @data
      
    elsif block_given?
      @mtime = mtime
      return (@data = yield)
      
    else
      return @data
    end
  end
end

#no_changes?Boolean

Returns:



42
43
44
45
46
# File 'lib/ruby-xml-mapper/file_cache_item.rb', line 42

def no_changes?
  @mutex.synchronize do
    File.mtime(@filename) == @mtime
  end
end