Module: Sketches

Defined in:
lib/sketches/version.rb,
lib/sketches/cache.rb,
lib/sketches/config.rb,
lib/sketches/sketch.rb,
lib/sketches/sketches.rb,
lib/sketches/temp_sketch.rb,
lib/sketches/exceptions/unknown_sketch.rb,
lib/sketches/exceptions/editor_not_defined.rb

Overview

– Sketches - A Ruby library for live programming and code reloading.

Copyright © 2009 Hal Brodigan (postmodern.mod3 at gmail.com)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ++

Defined Under Namespace

Modules: Config Classes: Cache, EditorNotDefined, Sketch, TempSketch, UnknownSketch

Constant Summary collapse

VERSION =

Sketches version

'0.1.1'
@@sketches_cache =
nil

Class Method Summary collapse

Class Method Details

.cacheObject

The cache of sketches.



77
78
79
80
81
82
83
# File 'lib/sketches/sketches.rb', line 77

def Sketches.cache
  unless @@sketches_cache
    @@sketches_cache = Cache.new
  end

  return @@sketches_cache
end

.config(options = {}) ⇒ Object

Configure Sketches with the given options.

options may contain the following keys:

:tmpdir

Directory to store temporary sketches in. Defaults to Dir.tmpdir if unspecified.

:background

Specifies wether to run the editor as a background or a foreground process.

:terminal

The terminal to optionally run the editor within.

:editor

The editor to use to edit sketches.

:pause

The number of seconds to pause in-between checking if any sketches were modified.

Sketches.config :editor => 'gvim', :pause => 2

Sketches.config :editor => 'vim',
                :background => true,
                :terminal => lambda { |cmd|
                  "xterm -fg gray -bg black -e #{cmd.dump}"
                }


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sketches/sketches.rb', line 48

def Sketches.config(options={})
  if options[:tmpdir]
    Config.tmpdir = options[:tmpdir]
  end

  if options[:background]
    Config.background = options[:background]
  end

  if options[:terminal]
    Config.terminal = options[:terminal]
  end

  if options[:editor]
    Config.editor = options[:editor]
  end

  if options[:pause]
    Config.pause = options[:pause]
  end

  return nil
end

.from(path) ⇒ Object

Creates a new sketch using the specified path.

Sketches.from 'path/to/foo.rb'


107
108
109
110
111
112
113
# File 'lib/sketches/sketches.rb', line 107

def Sketches.from(path)
  Sketches.cache.synchronize do
    sketch = Sketches.cache.reuse_sketch(path)

    sketch.synchronize { sketch.edit }
  end
end

.name(id, name) ⇒ Object

Names the sketch with the specified id with the specified name.

Sketches.name 2, :foo


120
121
122
123
124
# File 'lib/sketches/sketches.rb', line 120

def Sketches.name(id,name)
  Sketches.cache.synchronize do
    Sketches.cache.name_sketch(id,name)
  end
end

Print out all of the sketches.



145
146
147
# File 'lib/sketches/sketches.rb', line 145

def Sketches.print
  Sketches.cache.synchronize { puts Sketches.cache }
end

.save(id_or_name, path) ⇒ Object

Saves the sketch with the specified id_or_name to the specified path.

Sketches.save 2, 'path/to/example.rb'

Sketches.save :foo, 'path/to/foo.rb'


134
135
136
137
138
139
140
# File 'lib/sketches/sketches.rb', line 134

def Sketches.save(id_or_name,path)
  Sketches.cache.synchronize do
    if (sketch = Sketches.cache[id_or_name])
      sketch.save(path)
    end
  end
end

.sketch(id_or_name) ⇒ Object

Edits the sketch with the specified id_or_name. If no sketch exists with the specified id_or_name, one will be created.

Sketches.sketch 2

Sketches.sketch :foo


93
94
95
96
97
98
99
100
# File 'lib/sketches/sketches.rb', line 93

def Sketches.sketch(id_or_name)
  Sketches.cache.synchronize do
    sketch = Sketches.cache[id_or_name]
    sketch ||= Sketches.cache.new_sketch(id_or_name)

    sketch.synchronize { sketch.edit }
  end
end