| Class | Sketches::Sketch |
| In: |
lib/sketches/sketch.rb
|
| Parent: | Object |
| RUNNER | = | Kernel.method(:system) | command runner |
| id | [R] | ID of the sketch |
| mtime | [R] | Last modification time of the sketch |
| name | [RW] | Optional name of the sketch |
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. |
# 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
Spawns the Sketches.editor with the path of the sketch.
# 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.
# 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.
# 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.
# 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.
# File lib/sketches/sketch.rb, line 74 def synchronize(&block) @mutex.synchronize(&block) return nil end
Returns the String representation of the sketch.
# 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