Class Sketches::Cache
In: lib/sketches/cache.rb
Parent: Hash
StandardError UnknownSketch RuntimeError EditorNotDefined Tempfile TempSketch Hash Cache Sketch Config Sketches dot/f_1.png

Methods

External Aliases

each_value -> each_sketch

Public Class methods

Creates a new Sketches cache.

[Source]

# 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

Public Instance methods

Returns the sketch with the specified id_or_name.

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# 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.

[Source]

# File lib/sketches/cache.rb, line 150
    def next_id
      size + 1
    end

Creates a new sketch using the existing path.

  reuse_sketch 'path/to/foo.rb'

[Source]

# File lib/sketches/cache.rb, line 96
    def reuse_sketch(path)
      id = next_id

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

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

[Source]

# File lib/sketches/cache.rb, line 59
    def running?
      @thread.alive?
    end

Provides thread-safe access to the cache.

[Source]

# File lib/sketches/cache.rb, line 66
    def synchronize(&block)
      @mutex.synchronize(&block)
      return nil
    end

Returns the String representation of the cache.

[Source]

# File lib/sketches/cache.rb, line 159
    def to_s(verbose=false)
      values.inject('') { |str,sketch| str << sketch.to_s(verbose) }
    end

[Validate]