Class: Ruber::RubyRunner::RubyRunnerPlugin

Inherits:
ExternalProgramPlugin show all
Defined in:
plugins/ruby_runner/ruby_runner.rb

Overview

Abstract class plugins which need to run a ruby program can derive from.

It provides some convenience methods to retrieve an option for a target (which can be either a global project, a document or a file) so that, if the option isn’t found in the target itself, a global value is used. It also provide methods to retrieve the path of the ruby interpreter to use for a given target, taking into account the choice made by the user in the “Interpreters” submenu.

Lastly, this class adds a project option called ruby_options to the PS for the plugin.

Note: classes which want to derive from this class should derive from RubyRunnerPlugin rather than from this.

Direct Known Subclasses

FilesRunner::Plugin, Ruber::RSpec::Plugin, Ruber::Rake::Plugin

Constant Summary

Constants inherited from Plugin

LICENSES

Instance Attribute Summary

Attributes inherited from ExternalProgramPlugin

process

Attributes included from PluginLike

plugin_description

Instance Method Summary (collapse)

Methods inherited from ExternalProgramPlugin

#display_exit_message, #do_stderr, #do_stdout, #failed_to_start, #process_standard_error, #process_standard_output, #run_process, #shutdown, #slot_process_finished, #stop_process

Methods inherited from GuiPlugin

#action_collection, #execute_action, #register_action_handler, #setup_actions, #unload

Methods inherited from Plugin

#about_data

Methods included from PluginLike

#add_extensions_to_project, #add_options_to_project, #add_widgets_to_project, #create_tool_widget, #delayed_initialize, #initialize_plugin, #load_settings, #plugin_name, #query_close, #register_options, #register_with_project, #remove_extensions_from_project, #remove_from_project, #remove_options_from_project, #remove_widgets_from_project, #restore_session, #save_settings, #session_data, #setup_action, #setup_actions, #shutdown, #unload, #update_project

API for feature ruby_runner

Methods API

Constructor Details

- (RubyRunnerPlugin) initialize(pso, group, rules = {}, order = nil, line_buffered = true)

Creates a new instance.

Before passing it to RunnerPlugin’s constructor, it adds the ruby_options project option to the plugin specification object, in the given group.

the plugin to. If an option called ruby_options already exists in the group, it won’t be overwritten but cannot contain the :all entry and must be an array. must be an array, even if it only contains one entry must be an array, even if it only contains one entry

Parameters:

  • pso (PluginSpecification)

    the plugin specification object associated with

  • group (String, Symbol)

    the group the ruby_options option should be added

  • rules (Hash) (defaults to: {})

    the rules to apply to the ruby_options option

  • order (Integer, nil) (defaults to: nil)

    has the same meaning as in the PSF and applies to the

  • line_buffered (Boolean) (defaults to: true)

Options Hash (rules):

  • scope (<Symbol>) — default: [:global]

    has the same meaning as in the PSF,

  • mimetypes (<String>) — default: []

    has the same meaning as in the PSF, but

  • file_extension (<String>) — default: []

    has the same meaning as in the PSF, but



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'plugins/ruby_runner/ruby_runner.rb', line 305

def initialize pso, group, rules = {}, order = nil, line_buffered = true
  group = group.to_sym
  @group = group
  default_rules = {:scope => [:global], :file_extension => [], :mimetype => [], :place => [:local]}
  rules = default_rules.merge rules
  ruby_options_data = {
    :name => :ruby_options,
    :group => group,
    :default => Ruber[:config][:ruby, :ruby_options],
    :scope => rules[:scope],
    :mimetype => rules[:mimetype],
    :file_extension => rules[:file_extension],
    :order => order,
    :place => rules[:place]
  }
  pso.project_options[[group, :ruby_options]] ||= PluginSpecificationReader::Option.new ruby_options_data
  super pso, line_buffered
end

Instance Method Details

- (String) interpreter_for(target, ignore_project = false)

Returns the name of the ruby interpreter to use for the given target

It is specialized version of #option_for which looks for the ruby/ruby option and takes into account the selected entry in the “Interpreters” submenu.

Parameters:

  • ignore_project (Boolean) (defaults to: false)

Returns:

  • (String)

    the path of the interpreter to use



381
382
383
384
# File 'plugins/ruby_runner/ruby_runner.rb', line 381

def interpreter_for target, ignore_project = false
  Ruber[:ruby_interpreters].chosen_interpreter || 
      option_for(target, :ruby, :ruby, ignore_project)
end

- (Object) option_for(target, group, name, ignore_project = false, abs = false)

Searches for an option for the given target.

The behaviour of this method depends on what target is:

  • if it is an AbstractProject, the option is searched in it
  • if it is a Document or a string representing a file name and belongins to the current project, the option is searched in the current project
  • if it is a Document but doesn’t belong to the current project (or no current project exists), the option is searched in the document’s own project
  • if it is a string representing a file which doesn’t belong to the current project (or there’s no current project) the option is looked for in the global configuration object.

In all cases, if the search fails, the option is looked up in the global configuration object.

option for. If nil, the option will directly be looked up in the global configuration object current project, even if target is a Document or the name of a file belonging to it

Parameters:

  • target (AbstractProject, Document, String, nil)

    the object to look up the

  • group (Symbol)

    the group the option belongs to

  • name (Symbol)

    the name of the option

  • ignore_project (Boolean) (defaults to: false)

    if true the option won’t be looked for in the

  • abs (Boolean) (defaults to: false)

    it has the same meaning as in SettingsContainer#[]

Raises:

  • (IndexError)

    if the option can’t be found



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'plugins/ruby_runner/ruby_runner.rb', line 351

def option_for target, group, name, ignore_project = false, abs = false
  cont = []
  case target
  when AbstractProject then cont << target
  when Document
    cont << (ignore_project ? (target.own_project) : target.project)
  when String
    if !ignore_project and Ruber[:world].projects.project_for_file target
      cont << Ruber[:world].active_project
    end
  end
  cont << Ruber[:config]
  abs = :abs if abs
  cont.each_with_index do |c, i|
    begin return c[group, name, abs]
    rescue IndexError
      raise if i == cont.size - 1
    end
  end
end

- (<String>) ruby_command_for(target, dir, opts = {})

Returns the command to use to execute ruby according to the appropriate options for the given target.

To build the command, it uses the ruby/ruby and the ruby_options (in the group specified in the constructor).

the -C flag) made by the user in the “Interpreters” submenu option as default interpreter of the ruby_options option. to use and whose other elements are the options to pass to the interpreter.

Parameters:

  • target (AbstractProject, Document, String, nil)
  • dir (String)

    the directory to run ruby from (it will be passed to ruby using

  • opts (Hash) (defaults to: {})

    some options to fine tune the behaviour of the method

Options Hash (opts):

  • default_interpreter (Boolean) — default: false

    if true, ignores the choice

  • ignore_project (Boolean) — default: false
  • ruby (String) — default: nil

    if given, use this instead of the content of the ruby/ruby

  • ruby_options (<String>) — default: nil

    if given, use this instead of the content

Returns:

  • (<String>)

    an array whose first entry is the name of the ruby interpreter



408
409
410
411
412
413
414
415
416
417
418
# File 'plugins/ruby_runner/ruby_runner.rb', line 408

def ruby_command_for target, dir, opts = {}
  ruby =  Ruber[:ruby_interpreters].chosen_interpreter
  if !ruby or opts[:default_interpreter]
    ruby = if opts[:ruby] then opts[:ruby]
    else option_for target, :ruby, :ruby, opts[:ignore_project]
    end
  end
  ruby_options = opts[:ruby_options]
  ruby_options ||= option_for target, @group, :ruby_options, opts[:ignore_project]
  [ruby] + ruby_options + ['-C', dir] 
end