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

Methods

crc32   edit   new   reload!   save   stale?   synchronize   to_s  

Constants

RUNNER = Kernel.method(:system)   command runner

Attributes

id  [R]  ID of the sketch
mtime  [R]  Last modification time of the sketch
name  [RW]  Optional name of the sketch

Public Class methods

Creates a new sketch object with the specified id and the given options.

options may contain the following keys:

:name:The name of the sketch.
:path:The path to an existing sketch.

[Source]

# File lib/sketches/sketch.rb, line 53
    def initialize(id,options={})
      @id = id
      @name = options[:name]

      @mtime = Time.now
      @checksum = 0
      @mutex = Mutex.new

      if options[:path]
        @path = options[:path]
        @name ||= File.basename(@path)
      else
        TempSketch.open { |file| @path = file.path }
      end

      reload!
    end

Public Instance methods

Spawns the Sketches.editor with the path of the sketch.

[Source]

# File lib/sketches/sketch.rb, line 82
    def edit
      if Config.editor.nil?
        raise(EditorNotDefined,"no editor has defined via ENV['EDITOR'] or Sketches.config",caller)
      elsif Config.editor.kind_of?(Proc)
        cmd = Config.editor.call(@path)
      else
        cmd = "#{Config.editor} #{@path}"
      end

      if Config.terminal
        if Config.terminal.kind_of?(Proc)
          cmd = Config.terminal.call(cmd)
        else
          cmd = "#{Config.terminal} #{cmd}"
        end
      end

      if Config.background
        Thread.new(cmd,&RUNNER)
      else
        RUNNER.call(cmd)
        self.reload! if Config.eval_after_editor_quit
      end
    end

Reloads the sketch.

[Source]

# File lib/sketches/sketch.rb, line 124
    def reload!
      if File.file?(@path)
        @mtime = File.mtime(@path)
        @checksum = crc32

        begin
          return load(@path)
        rescue LoadError => e
          STDERR.puts "#{e.class}: #{e.message}"
        end
      end

      return false
    end

Saves the sketch to the specified path.

[Source]

# File lib/sketches/sketch.rb, line 142
    def save(path)
      if (File.file?(@path) && @path != path)
        FileUtils.cp(@path,path)
        return true
      end

      return false
    end

Returns true if the sketch has become stale and needs reloading, returns false otherwise.

[Source]

# File lib/sketches/sketch.rb, line 111
    def stale?
      if File.file?(@path)
        if File.mtime(@path) > @mtime
          return crc32 != @checksum
        end
      end

      return false
    end

Provides thread-safe access to the sketch.

[Source]

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

Returns the String representation of the sketch.

[Source]

# File lib/sketches/sketch.rb, line 154
    def to_s(verbose=false)
      str = "##{@id}"
      str << ": #{@name}" if @name
      str << "\n\n"

      if @path && File.file?(@path)
        File.open(@path) do |file|
          unless verbose
            4.times do
              if file.eof?
                str << "\n" unless str[-1..-1] == "\n"
                str << "  ..."
                break
              end

              str << "  #{file.readline}"
            end
          else
            file.each_line { |line| str << "  #{line}" }
          end

          str << "\n"
        end
      end

      return str
    end

Protected Instance methods

Returns the CRC32 checksum of the sketch file.

[Source]

# File lib/sketches/sketch.rb, line 187
    def crc32
      r = 0xffffffff

      File.open(@path) do |file|
        file.each_byte do |b|
          r ^= b
          8.times { r = ((r >> 1) ^ (0xEDB88320 * (r & 1))) }
        end
      end

      return r ^ 0xffffffff
    end

[Validate]