Class: RedGrape::Tools::Trellis

Inherits:
GraphStore show all
Defined in:
lib/red_grape/tools/trellis.rb

Constant Summary collapse

DEFAULT_PORT =
28282

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GraphStore

#<<, #graph, #graph_names, #put_graph

Constructor Details

#initialize(filename = nil) ⇒ Trellis

Returns a new instance of Trellis.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/red_grape/tools/trellis.rb', line 24

def initialize(filename=nil)
  @filename = filename
  @graphs =
    if @filename && File.exist?(@filename)
      File.open @filename, 'r' do |file|
        Marshal.load file
      end
    else
      {tinker:RedGrape::Graph.create_tinker_graph}
    end
end

Class Method Details

.open(port = nil) ⇒ Object



19
20
21
# File 'lib/red_grape/tools/trellis.rb', line 19

def open(port=nil)
  DRbObject.new_with_uri(uri port)
end

.start(port = nil, filename = nil, &block) ⇒ Object



11
12
13
# File 'lib/red_grape/tools/trellis.rb', line 11

def start(port=nil, filename=nil, &block)
  self.new(filename).start port, &block
end

.uri(port = nil) ⇒ Object



15
16
17
# File 'lib/red_grape/tools/trellis.rb', line 15

def uri(port=nil)
  "druby://localhost:#{port || DEFAULT_PORT}"
end

Instance Method Details

#start(port = nil, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/red_grape/tools/trellis.rb', line 36

def start(port=nil, &block)
  at_exit do
    File.open @filename, 'w' do |file|
      Marshal.dump @graphs, file
    end if @filename
  end

  DRb.start_service self.class.uri(port), self
  block.call if block
  sleep
end