Class: Live::Session

Inherits:
Object show all
Includes:
HighLine::SystemExtensions
Defined in:
lib/live/session.rb

Instance Method Summary collapse

Constructor Details

#initializeSession

Starts a live session using a named pipe to receive code from a remote source and evaluates it within a context, a bit like an IRB session but evaluates code sent from a text editor



71
72
73
74
75
76
77
78
# File 'lib/live/session.rb', line 71

def initialize 
  return p( Exception.new("Another session sems to be running") ) if File.exist?( "#{Dir.tmpdir}/ruby_live.pipe" )
  p( Notice.new("Live Session") )
  get_binding
  init_pipe
  expect_input
  serve
end

Instance Method Details

#bind(key, &block) ⇒ Object

Expects a one char Symbol or String which will bind to a passed block so it can be called later with a keystroke



105
106
107
108
109
110
# File 'lib/live/session.rb', line 105

def bind( key, &block ) 
  @bindings = [] unless @bindings.instance_of?(Array)
  block ||= Proc.new{}
  @bindings[ key.to_s[0] ] = Ruby2Ruby.new.process( [:block, block.to_sexp.last] )
  Notice.new( "Key '#{key}' is bound to an action")
end

#clearObject



117
118
119
# File 'lib/live/session.rb', line 117

def clear
  print "\e[2J\e[f"
end

#evaluate(string = nil) ⇒ Object

Evaluates a ruby expression within the @context Binding



113
114
115
# File 'lib/live/session.rb', line 113

def evaluate( string = nil ) 
  return resilient_eval( string, @context ) if string
end

#expect_inputObject

Starts a new Thread that will loop capturing keystrokes and evaluating the bound block within the @context Binding, will be called on initialize



93
94
95
96
97
98
99
100
101
102
# File 'lib/live/session.rb', line 93

def expect_input 
  Thread.new do
    loop do 
      char = get_character 
      @bindings ||= []
      bind = @bindings[ char ]
      p evaluate( bind ) if bind 
    end
  end
end

#get_bindingObject Also known as: reaload!

:nodoc:



121
122
123
# File 'lib/live/session.rb', line 121

def get_binding #:nodoc:
  @context = binding
end

#init_pipeObject



80
81
82
83
# File 'lib/live/session.rb', line 80

def init_pipe
  @pipe_path = Pipe.new('').path
  `rm #{@pipe_path}; mkfifo #{@pipe_path}`
end

#run_updates(code) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/live/session.rb', line 125

def run_updates( code )
  source = ParseTree.new.parse_tree_for_string( code ).first
  final = []
  while iter = source.assoc(:iter)
    source -= [iter]
    final << [:block, iter.last] if iter[1].include?(:update)
  end
  evaluate( final.collect{ |exp| Ruby2Ruby.new.process(exp) }.join("\n") )
  Notice.new('Update blocks evaluated')
end

#serveObject

Starts a loop that checks the named pipe and evaluate its contents, will be called on initialize



86
87
88
89
90
# File 'lib/live/session.rb', line 86

def serve 
  File.open( @pipe_path, File::RDONLY | File::NONBLOCK) do |f|
    loop { p evaluate( f.gets.to_s.gsub("∂", "\n") ) }
  end
end

#updateObject

Allmost a stub



136
137
138
# File 'lib/live/session.rb', line 136

def update # Allmost a stub
  yield
end