class Confinicky::Parsers::Command
A simple class that parses a line of shell code to into a classified and attributed string.
Constants
- ALIAS_COMMAND
- EXPORT_COMMAND
Attributes
name[R]
The name of the command.
value[R]
The assigned value of the command.
Public Class Methods
new(line: nil)
click to toggle source
Takes a line of code as a parameter and performs classification.
# File lib/confinicky/parsers/command.rb, line 24 def initialize(line: nil) @line = line @current_command_type = EXPORT_COMMAND if export? @current_command_type = ALIAS_COMMAND if alias? process_attributes! unless line? end
Public Instance Methods
alias?()
click to toggle source
Returns true if the line of code matches an alias command.
# File lib/confinicky/parsers/command.rb, line 47 def alias? matches_command_type?(ALIAS_COMMAND) && has_expression? end
append(value)
click to toggle source
# File lib/confinicky/parsers/command.rb, line 66 def append(value) @expression.append_value value end
closed?()
click to toggle source
# File lib/confinicky/parsers/command.rb, line 62 def closed? !@expression.open? end
export?()
click to toggle source
Returns true if the line of code matches an export command.
# File lib/confinicky/parsers/command.rb, line 41 def export? matches_command_type?(EXPORT_COMMAND) && has_expression? end
line?()
click to toggle source
The command should be regarded as a general line of code that is of no interest if it doesn’t match one of the supported command types.
# File lib/confinicky/parsers/command.rb, line 35 def line? !export? && !alias? end
open?()
click to toggle source
# File lib/confinicky/parsers/command.rb, line 57 def open? return false if line? || !has_expression? @expression.open? end
values_array()
click to toggle source
Returns an array containing the name / value pair for the command.
# File lib/confinicky/parsers/command.rb, line 53 def values_array [@name, @expression.value] end
Private Instance Methods
has_expression?()
click to toggle source
Returns true if the command actually has an expression.
# File lib/confinicky/parsers/command.rb, line 81 def has_expression? return Confinicky::Parsers::Expression.new(statement: @line).valid? end
matches_command_type?(command_type)
click to toggle source
Returns true if the command matches the specified type.
# File lib/confinicky/parsers/command.rb, line 74 def matches_command_type?(command_type) raise "No command type was specified." if command_type.nil? return !(@line =~ /\A#{command_type} /).nil? end
process_attributes!()
click to toggle source
Parses the commands name and assigned values into attributes.
# File lib/confinicky/parsers/command.rb, line 87 def process_attributes! if !@current_command_type.nil? @expression = Confinicky::Parsers::Expression.new(statement: @line.gsub(/\A#{@current_command_type} /,"")) @name = @expression.name @value = @expression.value end end