Class: Confinicky::Parsers::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/confinicky/parsers/command.rb

Overview

A simple class that parses a line of shell code to into a classified and attributed string.

Constant Summary collapse

EXPORT_COMMAND =
'export'
ALIAS_COMMAND =
'alias'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line: nil) ⇒ Command

Takes a line of code as a parameter and performs classification.



24
25
26
27
28
29
# 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

Instance Attribute Details

#nameObject (readonly)

The name of the command.



15
16
17
# File 'lib/confinicky/parsers/command.rb', line 15

def name
  @name
end

#valueObject (readonly)

The assigned value of the command.



19
20
21
# File 'lib/confinicky/parsers/command.rb', line 19

def value
  @value
end

Instance Method Details

#alias?Boolean

Returns true if the line of code matches an alias command.

Returns:

  • (Boolean)


47
48
49
# File 'lib/confinicky/parsers/command.rb', line 47

def alias?
  matches_command_type?(ALIAS_COMMAND) && has_expression?
end

#append(value) ⇒ Object



66
67
68
# File 'lib/confinicky/parsers/command.rb', line 66

def append(value)
  @expression.append_value value
end

#closed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/confinicky/parsers/command.rb', line 62

def closed?
  !@expression.open?
end

#export?Boolean

Returns true if the line of code matches an export command.

Returns:

  • (Boolean)


41
42
43
# File 'lib/confinicky/parsers/command.rb', line 41

def export?
  matches_command_type?(EXPORT_COMMAND) && has_expression?
end

#line?Boolean

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.

Returns:

  • (Boolean)


35
36
37
# File 'lib/confinicky/parsers/command.rb', line 35

def line?
  !export? && !alias?
end

#open?Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/confinicky/parsers/command.rb', line 57

def open?
  return false if line? || !has_expression?
  @expression.open?
end

#values_arrayObject

Returns an array containing the name / value pair for the command.



53
54
55
# File 'lib/confinicky/parsers/command.rb', line 53

def values_array
  [@name, @expression.value]
end