Class: Sketches::Cache

Inherits:
Hash
  • Object
show all
Defined in:
lib/sketches/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Creates a new Sketches cache.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#[](id_or_name) ⇒ Object

Returns the sketch with the specified id_or_name.



139
140
141
142
143
144
145
# 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

#find_by_name(name) ⇒ Object

Returns the sketch with the specified name.

cache.find_by_name :foobar


126
127
128
129
130
131
132
133
134
# 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

#name_sketch(id, name) ⇒ Object

Finds the sketch with the specified id and gives it then specified name.

cache.name_sketch 1, :foobar


108
109
110
111
112
113
114
115
116
117
118
119
# 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

#new_sketch(id_or_name = nil) ⇒ Object

Creates a new Sketch with the given id_or_name.

cache.new_sketch 2

cache.new_sketch :foobar


78
79
80
81
82
83
84
85
86
87
88
89
# 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

#next_idObject

Returns the next available sketch id.



150
151
152
# File 'lib/sketches/cache.rb', line 150

def next_id
  size + 1
end

#reuse_sketch(path) ⇒ Object

Creates a new sketch using the existing path.

reuse_sketch 'path/to/foo.rb'


96
97
98
99
100
# File 'lib/sketches/cache.rb', line 96

def reuse_sketch(path)
  id = next_id

  return self[id] = Sketch.new(id,:path => path)
end

#running?Boolean

Returns true if the cache is still checking if any sketches have been modified, returns false otherwise.

Returns:

  • (Boolean)


59
60
61
# File 'lib/sketches/cache.rb', line 59

def running?
  @thread.alive?
end

#synchronize(&block) ⇒ Object

Provides thread-safe access to the cache.



66
67
68
69
# File 'lib/sketches/cache.rb', line 66

def synchronize(&block)
  @mutex.synchronize(&block)
  return nil
end

#to_s(verbose = false) ⇒ Object

Returns the String representation of the cache.



159
160
161
# File 'lib/sketches/cache.rb', line 159

def to_s(verbose=false)
  values.inject('') { |str,sketch| str << sketch.to_s(verbose) }
end