| Class | Sketches::Cache |
| In: |
lib/sketches/cache.rb
|
| Parent: | Hash |
| each_value | -> | each_sketch |
# File lib/sketches/cache.rb, line 35 def initialize @mutex = Mutex.new super() @thread = Thread.new(self) do |cache| loop do cache.synchronize do cache.each_sketch do |sketch| sketch.synchronize do sketch.reload! if sketch.stale? end end end sleep(Config.pause) end end end
Returns the sketch with the specified id_or_name.
# File lib/sketches/cache.rb, line 139 def [](id_or_name) if id_or_name.kind_of?(Integer) return super(id_or_name) elsif (id_or_name.kind_of?(String) || id_or_name.kind_of?(Symbol)) return find_by_name(id_or_name) end end
Returns the sketch with the specified name.
cache.find_by_name :foobar
# File lib/sketches/cache.rb, line 126 def find_by_name(name) name = name.to_sym each_value do |sketch| return sketch if sketch.name == name end return nil end
Finds the sketch with the specified id and gives it then specified name.
cache.name_sketch 1, :foobar
# File lib/sketches/cache.rb, line 108 def name_sketch(id,name) id = id.to_i unless has_key?(id) raise(UnknownSketch,"cannot find the sketch with id: #{id}",caller) end sketch = self[id] sketch.synchronize { sketch.name = name.to_sym } return sketch end
Creates a new Sketch with the given id_or_name.
cache.new_sketch 2 cache.new_sketch :foobar
# File lib/sketches/cache.rb, line 78 def new_sketch(id_or_name=nil) id_or_name ||= next_id if id_or_name.kind_of?(Integer) return self[id_or_name] = Sketch.new(id_or_name) else id = next_id name = id_or_name return self[id] = Sketch.new(id,:name => name) end end
Returns the next available sketch id.
# File lib/sketches/cache.rb, line 150 def next_id size + 1 end
Returns true if the cache is still checking if any sketches have been modified, returns false otherwise.
# File lib/sketches/cache.rb, line 59 def running? @thread.alive? end
Provides thread-safe access to the cache.
# File lib/sketches/cache.rb, line 66 def synchronize(&block) @mutex.synchronize(&block) return nil end