class Confinicky::Parsers::Expression
A simple class that parses a basic assignment expression
Constants
- DOUBLE_QUOTE_MATCHER
- SINGLE_QUOTE_MATCHER
Attributes
The name of the variable being assigned. (LHS)
Public Class Methods
Takes a line of code as a parameter and performs classification.
# File lib/confinicky/parsers/expression.rb, line 19 def initialize(statement: nil) @statement = statement.split("=") if valid? @name = @statement[0] @value = @statement[1] @value.gsub!("\n","") unless open? end end
Public Instance Methods
Appends additional string content to the value stored in the parser.
# File lib/confinicky/parsers/expression.rb, line 55 def append_value(value) @value += value end
Determines if our statement is open ended. Meaning no closing quote for a provided opening quote.
# File lib/confinicky/parsers/expression.rb, line 62 def open? !@value.nil? && (uses_double_quotes? || uses_single_quotes?) && extracted_value.nil? end
Determines whether or not both types of quotes are present.
# File lib/confinicky/parsers/expression.rb, line 36 def uses_both_quotes? !(@value =~ /'/).nil? && !(@value =~ /"/).nil? end
Determines if the expression is wrapped in double quotes.
# File lib/confinicky/parsers/expression.rb, line 42 def uses_double_quotes? uses_both_quotes? && (@value =~ /'/) > (@value =~ /"/) || (@value =~ /"/) && (@value =~ /'/).nil? end
Determines if the expression is wrapped in single quotes.
# File lib/confinicky/parsers/expression.rb, line 48 def uses_single_quotes? !uses_double_quotes? && !(@value =~ /'/).nil? end
Ensures that the expression has an assigned value.
# File lib/confinicky/parsers/expression.rb, line 30 def valid? @statement.length == 2 && !@statement[1].nil? end
The value which is being assigned to the variable in the expression (RHS)
# File lib/confinicky/parsers/expression.rb, line 68 def value quote = uses_double_quotes? ? "\"" : "\'" value = extracted_value value = "#{quote}#{value}#{quote}" if !value.nil? && value =~ /\s/ && value != "\n" value end
Protected Instance Methods
Extracts the value from quotes if necessary. If the expression is has an opening quote without a corresponding closing quote the method will return nil.
# File lib/confinicky/parsers/expression.rb, line 81 def extracted_value if uses_single_quotes? || uses_double_quotes? matcher = uses_double_quotes? ? DOUBLE_QUOTE_MATCHER : SINGLE_QUOTE_MATCHER matches = @value.scan(matcher) return nil if matches.length < 1 return matches[0][0] end @value end