Class AutomateIt::EditManager::EditSession
In: lib/automateit/edit_manager.rb
Parent: AutomateIt::Common

EditSession

EditSession provides a way to edit files and strings.

For example, here‘s how to edit a string from the Interpreter:

  edit(:text => "# hello") do
    uncomment "llo"
    append "world"
  end
  # => "hello\nworld"

The above example edits a text string containing "# hello". The editing session uncomments the line containing "llo" and then appends a line with the word "world". The edited result is returned, containing two lines: "hello" and "world".

The edit session only makes changes if they‘re needed. In the above example, once the "hello" line is uncommented, the "uncomment" command won‘t do anything. Similarly, once the word "world" has been appended, it won‘t be appended again. So if you re-edit the resulting string, it won‘t be changed because it‘s already in the desired state.

This approach simplifies editing because you only need to specify the commands that are needed to change the file, and the session will figure out which ones to run.

Methods

_backup   _exists?   _read   _readable?   _write   append   comment   comment_style   contains?   delete   different?   edit   manipulate   new   prepend   replace   uncomment  

Attributes

comment_prefix  [RW]  Comment prefix, e.g., "/*"
comment_suffix  [RW]  Comment suffix, e.g., "*/"
contents  [RW]  Current contents of the editing buffer.
filename  [RW]  File that was read for editing.
original_contents  [RW]  Original contents of the editing buffer before any changes were made.
params  [RW]  Hash of parameters to make available to the editing session.

Public Class methods

Create an EditSession.

Options:

  • :interpreter — AutomateIt Interpreter, required. Will be automatically set if you use AutomateIt::Interpreter#edit.

Public Instance methods

Append line to the bottom of the buffer, but only if it‘s not in this file already.

Options:

  • :unless — Look for this String or Regexp instead and don‘t append if it matches.

See example for prepend.

Comment out lines matching the String or Regexp query.

Specify the comment style‘s prefix and suffix.

Example:

  # C style comments
  comment_style "/*", "*/"

Does the buffer contain anything that matches the String or Regexp query?

Delete lines matching the String or Regexp query

Is the buffer currently different than its original contents?

Edit a file or string.

Requires a filename argument or options hash — e.g.,. edit("foo") and edit(:file => "foo") will both edit a file called foo.

Options:

  • :file — File to edit.
  • :text — String to edit.
  • :params — Hash to make available to editor session.
  • :create — Create the file if it doesn‘t exist? Defaults to false.
  • :mode, :user, :group — Set permissions on generated file, see ShellManager#chperm
  • :backup — Make a backup of original file? Defaults to true.

Edit a string:

  edit(:text => "foo") do
    replace "o", "@"
  end
  # => "f@@"

Edit a file and pass parameters to the editing session:

  edit(:file => "myfile", :params => {:greet => "world"} do
    prepend "MyHeader"
    append "Hello "+params[:greet]
  end

Edit a file, create it and set permissions if necessary:

  edit("/tmp/foo", :create => true, :mode => 0600, :user => :root) do
    prepend "Hello world!"
  end

Manipulate the buffer. The result of your block will replace the buffer. This is very useful for complex edits.

Example:

  manipulate do |buffer|
    buffer.gsub(/foo/, "bar")
  end

Prepend line to the top of the buffer, but only if it‘s not in this file already.

Options:

  • :unless — Look for this String or Regexp instead and don‘t prepend if it matches.

Example:

  # Buffer's contents are 'add    this line'

  # This will prepend a line because they're not identical.
  prepend("add this line")

  # Won't prepend line because Regexp matches exisint line in buffer.
  prepend("add this line", :unless => /add\s*this\*line/)

Replace contents matching the String or Regexp query with the string.

Uncomment lines matching the String or Regexp query.

Protected Instance methods

Backup the original file.

Does the file exist?

Read contents from filename. Called by the edit command to load text into the buffer.

Is the file readable?

Write contents to filename. Used by the edit command to write the buffer to a file.

[Validate]