Class: Confinicky::Controllers::Commands
- Inherits:
-
Object
- Object
- Confinicky::Controllers::Commands
- Defined in:
- lib/confinicky/controllers/commands.rb
Overview
The command group controller allows you to manipulate the contents of a specific grouping of commands in a nice OO way.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
The path to the file on disk representing the model.
Instance Method Summary collapse
-
#backup! ⇒ Object
Creates a copy of the associated shell file.
-
#clean! ⇒ Object
Finds duplicate export statements and reduces them to the most recent statement.
-
#duplicates ⇒ Object
Detects duplicate definitions.
-
#find(query: nil) ⇒ Object
Matches a given string against the names of the group’s contents.
-
#initialize(file_type_key: :env) ⇒ Commands
constructor
A new instance of Commands.
-
#inspect(name: nil, separator: ":") ⇒ Object
Returns a table for the contents of a specific variable when split by a specified separating string.
-
#length ⇒ Object
The total number of commands managed by the controller.
-
#remove!(variable_name) ⇒ Object
Removes an environment variable if it exists.
-
#save! ⇒ Object
Updates the actual shell file on disk.
-
#set!(assignment) ⇒ Object
Parses an assignment such as “MY_VAR=1234” and injects it into the exports or updates an existing variable if possible.
-
#to_table ⇒ Object
Creates a table representation of the command data.
Constructor Details
#initialize(file_type_key: :env) ⇒ Commands
Returns a new instance of Commands.
15 16 17 18 19 20 |
# 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 |
Instance Attribute Details
#path ⇒ Object (readonly)
The path to the file on disk representing the model.
13 14 15 |
# File 'lib/confinicky/controllers/commands.rb', line 13 def path @path end |
Instance Method Details
#backup! ⇒ Object
Creates a copy of the associated shell file.
97 98 99 |
# File 'lib/confinicky/controllers/commands.rb', line 97 def backup! @shell_file.backup! end |
#clean! ⇒ Object
Finds duplicate export statements and reduces them to the most recent statement.
52 53 54 55 56 57 58 |
# 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 ⇒ Object
Detects duplicate definitions.
41 42 43 44 45 46 47 |
# 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) ⇒ Object
34 35 36 37 |
# 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: ":") ⇒ Object
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 |
# +--------+-----------------------------------------------------------+
135 136 137 138 139 140 |
# 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 ⇒ Object
The total number of commands managed by the controller.
103 104 105 |
# File 'lib/confinicky/controllers/commands.rb', line 103 def length @commands.length end |
#remove!(variable_name) ⇒ Object
Removes an environment variable if it exists.
85 86 87 |
# File 'lib/confinicky/controllers/commands.rb', line 85 def remove!(variable_name) @commands.delete_if { |i| i[0] == variable_name } end |
#save! ⇒ Object
Updates the actual shell file on disk.
91 92 93 |
# File 'lib/confinicky/controllers/commands.rb', line 91 def save! @shell_file.write! end |
#set!(assignment) ⇒ Object
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 ~")
75 76 77 78 79 80 81 |
# 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 ⇒ Object
Creates a table representation of the command data.
109 110 111 |
# File 'lib/confinicky/controllers/commands.rb', line 109 def to_table make_table(title: @table_title, rows: @commands) end |