Class: Sketches::Sketch

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

Constant Summary collapse

RUNNER =

command runner

Kernel.method(:system)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ 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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

Instance Attribute Details

#idObject (readonly)

ID of the sketch



34
35
36
# File 'lib/sketches/sketch.rb', line 34

def id
  @id
end

#mtimeObject (readonly)

Last modification time of the sketch



40
41
42
# File 'lib/sketches/sketch.rb', line 40

def mtime
  @mtime
end

#nameObject

Optional name of the sketch



37
38
39
# File 'lib/sketches/sketch.rb', line 37

def name
  @name
end

Instance Method Details

#editObject

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

#reload!Object

Reloads the sketch.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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

#save(path) ⇒ Object

Saves the sketch to the specified path.



142
143
144
145
146
147
148
149
# 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

#stale?Boolean

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

Returns:

  • (Boolean)


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

#synchronize(&block) ⇒ Object

Provides thread-safe access to the sketch.



74
75
76
77
# File 'lib/sketches/sketch.rb', line 74

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

#to_s(verbose = false) ⇒ Object

Returns the String representation of the sketch.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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