class Confinicky::Controllers::Commands

The command group controller allows you to manipulate the contents of a specific grouping of commands in a nice OO way.

Attributes

path[R]

The path to the file on disk representing the model.

Public Class Methods

new(file_type_key: :env) click to toggle source
# File lib/confinicky/controllers/commands.rb, line 15
def initialize(file_type_key: :env)
  @path = Confinicky::ConfigurationFile.path_for_key(key: file_type_key)
  @shell_file = Confinicky::ShellFile.new(file_path: path)
  @commands = []
  @table_tite = "Commands"
end

Public Instance Methods

backup!() click to toggle source

Creates a copy of the associated shell file.

# File lib/confinicky/controllers/commands.rb, line 97
def backup!
  @shell_file.backup!
end
clean!() click to toggle source

Finds duplicate export statements and reduces them to the most recent statement.

# File lib/confinicky/controllers/commands.rb, line 52
def clean!
  for duplicate in duplicates.map{|duplicate| duplicate[0]}
    last_value = @commands.find_all{|c| c[0] =~ /^#{duplicate}/ }.last
    @commands.delete_if{ |c| c[0] == duplicate}
    @commands << [duplicate, last_value]
  end
end
duplicates() click to toggle source

Detects duplicate definitions.

# File lib/confinicky/controllers/commands.rb, line 41
def duplicates
  duplicates = {}
  @commands.each do |command|
    duplicates[command[0]] = (duplicates[command[0]].nil?) ? 1 : duplicates[command[0]]+1
  end
  duplicates.delete_if { |key,value| value==1}.sort_by{|key,value| value}.reverse
end
find(query: nil) click to toggle source

Matches a given string against the names of the group’s contents.

Attributes

  • query - The string used to match a given command name.

Examples

# Find the PATH environment variable.
Exports.new.find("PATH")
# => {name: "PATH", value: "/Users/name/bin/"}
# File lib/confinicky/controllers/commands.rb, line 34
def find(query: nil)
  match = @commands.find{|command| command[0] =~ /^#{query}/ }
  {name: match[0], value: match[1]} unless match.nil?
end
inspect(name: nil, separator:":") click to toggle source

Returns a table for the contents of a specific variable when split by a specified separating string.

Attributes

  • name - The name of the variable, alias, etc., to inspect.

  • separator - A string used to split the value. Defaults to a ‘:’.

Examples

# Create or update an environment variable called MY_VAR.
Exports.inspect("PATH")
# +--------+-----------------------------------------------------------+
# |                           Values in PATH                           |
# +--------+-----------------------------------------------------------+
# | index  | value                                                     |
# +--------+-----------------------------------------------------------+
# | 1      | /Users/name/.rvm/gems/ruby-2.1.2/bin                      |
# | 2      | /Users/name/.rvm/gems/ruby-2.1.2@global/bin               |
# | 3      | /Users/name/.rvm/rubies/ruby-2.1.2/bin                    |
# +--------+-----------------------------------------------------------+
# File lib/confinicky/controllers/commands.rb, line 135
def inspect(name: nil, separator:":")
  return nil if (match = find(query: name)).nil?
  count = 0
  rows = match[:value].split(separator).map{|partition| [count+=1, partition]}
  make_table(title: "Values in #{name}", rows: rows, headings: ['index', 'value'])
end
length() click to toggle source

The total number of commands managed by the controller.

# File lib/confinicky/controllers/commands.rb, line 103
def length
  @commands.length
end
remove!(variable_name) click to toggle source

Removes an environment variable if it exists.

# File lib/confinicky/controllers/commands.rb, line 85
def remove!(variable_name)
  @commands.delete_if { |i| i[0] == variable_name }
end
save!() click to toggle source

Updates the actual shell file on disk.

# File lib/confinicky/controllers/commands.rb, line 91
def save!
  @shell_file.write!
end
set!(assignment) click to toggle source

Parses an assignment such as “MY_VAR=1234” and injects it into the exports or updates an existing variable if possible.

Attributes

  • assignment - The value which will be assigned to the command.

Examples

# Create or update an environment variable called MY_VAR.
Exports.new.set("MY_VAR=A short phrase.")

# Create or update an environment variable called MY_VAR.
Aliases.new.set("home=cd ~")
# File lib/confinicky/controllers/commands.rb, line 75
def set!(assignment)
  assignment = assignment.split("=")
  return false if assignment.length < 2
  remove! assignment[0]
  assignment[1] = "\'#{assignment[1]}\'" if assignment[1] =~ /\s/
  @commands << assignment
end
to_table() click to toggle source

Creates a table representation of the command data.

# File lib/confinicky/controllers/commands.rb, line 109
def to_table
  make_table(title: @table_title, rows: @commands)
end

Private Instance Methods

make_table(title: '', rows: [], headings: ['Name', 'Value']) click to toggle source

Returns a terminal table with a specified title and contents.

Attributes

  • title - A string to be printed out as the title.

  • rows - An array of sub arrays +[[name, value],[name, value],…]+

  • headings - An array of strings for heading titles +[‘Name’, ‘Value’]+

Examples

# Create or update an environment variable called MY_VAR.
Exports.inspect("PATH")
# +--------+-----------------------------------------------------------+
# |                           [title]                                  |
# +--------+-----------------------------------------------------------+
# | Name   | Value                                                     |
# +--------+-----------------------------------------------------------+
# | PATH   | '/Users/name/.rvm/gems/ruby-2.1.2/bin:/Users/name/.rvm... |
# | FOO    | 3000                                                      |
# | BAR    | 'some other value'                                        |
# +--------+-----------------------------------------------------------+
# File lib/confinicky/controllers/commands.rb, line 166
def make_table(title: '', rows: [], headings: ['Name', 'Value'])
  return nil if rows.length < 1
  table = Terminal::Table.new(title: title, headings: headings) do |t|
    for row in rows
      if row[1].length > 100
        t.add_row [row[0], row[1][0...100]+"..."]
      else
        t.add_row row
      end
    end
  end
  return table
end