Class: Editserver
- Inherits:
-
Object
- Object
- Editserver
- Defined in:
- lib/editserver.rb,
lib/editserver/editor.rb,
lib/editserver/command.rb,
lib/editserver/version.rb,
lib/editserver/response.rb,
lib/editserver/terminal/vim.rb,
lib/editserver/terminal/emacs.rb
Defined Under Namespace
Classes: Command, EditError, Editor, Emacs, Response, RoutingError, Vim
Constant Summary collapse
- GUI_EDITORS =
{ # OS X editors 'mate' => 'mate -w', 'mvim' => 'mvim --nofork --servername EDITSERVER', # does not return when app must be launched 'bbedit' => 'bbedit -w' # does not open file properly when app is launched }.reject { |k,v| not File.executable? %x(which #{k.shellsplit[0]}).chomp }
- VERSION =
'0.1.6'
Instance Attribute Summary collapse
-
#editors ⇒ Object
readonly
Returns the value of attribute editors.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#editor(path_info) ⇒ Object
returns Editserver handler based on path.
-
#initialize(options = {}) ⇒ Editserver
constructor
TODO: We are playing with global constants here; this is simple, but we should stop.
-
#register_editors(options = {}) ⇒ Object
returns Hash of name => EditorClass.
Constructor Details
#initialize(options = {}) ⇒ Editserver
TODO: We are playing with global constants here;
this is simple, but we should stop
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/editserver.rb', line 22 def initialize = {} opts = .dup Editor.terminal = opts.delete 'terminal' # seed with terminal-based editors, whose server/client modes are a bit too # complicated to configure dynamically @editors = { 'vim' => Vim, 'emacs' => Emacs } register_editors GUI_EDITORS.merge(opts) end |
Instance Attribute Details
#editors ⇒ Object (readonly)
Returns the value of attribute editors.
18 19 20 |
# File 'lib/editserver.rb', line 18 def editors @editors end |
Instance Method Details
#call(env) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/editserver.rb', line 75 def call env request = Rack::Request.new env klass = editor request.path_info Response.new(klass.new, request).call rescue RoutingError => e res = Rack::Response.new res.write e.to_s res.status = 500 res.finish end |
#editor(path_info) ⇒ Object
returns Editserver handler based on path
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/editserver.rb', line 62 def editor path_info path = path_info[%r(\A([/\w\.-]+)), 1] if klass = editors[path[/\/(.*)/, 1]] klass elsif path == '/' klass = editors[editors['default']] end raise RoutingError, "No handler for #{path}" if klass.nil? klass end |
#register_editors(options = {}) ⇒ Object
returns Hash of name => EditorClass
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/editserver.rb', line 37 def register_editors = {} opts = .dup if default = opts.delete('default') @editors['default'] = default end # rest should be editor definitions opts.each do |name, cmd| klass = pascalize name editor, params = cmd.shellsplit.partition.with_index { |w,i| i.zero? } self.class.class_eval %Q( # e.g. mate: mate -w class #{klass} < Editor # class Mate < Editor define_editor #{editor[0].inspect}, *#{params.inspect} # define_editor 'mate', *['-w'] end # end ) @editors[name] = self.class.const_get klass.to_sym end @editors end |