Class: Editors

Inherits:
Object
  • Object
show all
Includes:
Helpers::EditorSyntax, Helpers::PlatformDetection
Defined in:
lib/editor.rb,
lib/editors/version.rb,
lib/editors/helpers/editor_syntax.rb,
lib/editors/helpers/platform_detection.rb

Overview

Editor class. Lets you edit both real and temporary files, and returns their values after editing.

Author:

  • Jeff Sandberg

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

MAJOR =
0
MINOR =
0
PATCH =
1
VERSION =
[MAJOR, MINOR, PATCH].join('.')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::EditorSyntax

#blocking_flag_for_editor, #editor_name, #start_line_syntax_for_editor

Methods included from Helpers::PlatformDetection

#jruby?, #jruby_19?, #mri?, #mri_19?, #mri_20?, #mri_21?, #mri_2?, #rbx?, #windows?, #windows_ansi?

Constructor Details

#initialize(options = {}) ⇒ Editor

Initialize a new editor, with options. You should probably use Editor.edit or Editor.new instead of these You must pass a file OR temp string, but NOT both

Parameters:

  • options (Hash) (defaults to: {})
  • file (Hash)

    a customizable set of options

  • temp (Hash)

    a customizable set of options

  • line (Hash)

    a customizable set of options

  • blocking (Hash)

    a customizable set of options



24
25
26
27
28
29
30
31
32
33
# File 'lib/editor.rb', line 24

def initialize(options = {})
  opts = { file: nil, temp: nil, line: 1, blocking: true }.merge(options)
  if !opts_is_valid?opts
    fail ArgumentError, 'define file OR temp'
  elsif opts[:file]
    edit_file(opts)
  elsif opts[:temp]
    edit_temp(opts)
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/editor.rb', line 13

def content
  @content
end

Class Method Details

.file(file, line = 1, blocking = true) ⇒ String

Edit a file from the filesystem, and return its value

Parameters:

  • file (String)

    A file path to edit

  • line (Fixnum) (defaults to: 1)

    The line of the file to place the cursor at

  • blocking (Boolean) (defaults to: true)

    Pass the editor a blocking flag

Returns:

  • (String)

    The value of the file AFTER it was edited



42
43
44
# File 'lib/editor.rb', line 42

def file(file, line = 1, blocking = true)
  new(file: file, line: line, blocking: blocking).content
end

.temp(initial_content, line = 1) ⇒ String

Edits a temporary file, and return its value

Parameters:

  • initial_content (String)

    The content of the tempfile, prior to editing

  • line (Fixnum) (defaults to: 1)

    Tempfile line to place cursor at

Returns:

  • (String)

    The value of the tempfile AFTER it was edited



51
52
53
# File 'lib/editor.rb', line 51

def temp(initial_content, line = 1)
  new(temp: initial_content, line: line, blocking: true).content
end