Class: String

Inherits:
Object show all
Defined in:
lib/mysh/string_helpers.rb,
lib/mysh/shell_variables/evaluate.rb,
lib/mysh/handlebars/eval_handlebars.rb

Overview

Monkey patches for Mysh handlebars

Instance Method Summary collapse

Instance Method Details

#dress_up_quotesObject

Dress up in quotes if needed.



23
24
25
# File 'lib/mysh/string_helpers.rb', line 23

def dress_up_quotes
  self[' '] ? "\"#{self}\"" : self
end

#dress_up_slashesObject

Dress up slashes and backslashes.



18
19
20
# File 'lib/mysh/string_helpers.rb', line 18

def dress_up_slashes
  MiniTerm.windows? ? self.gsub("/", "\\") : self
end

#eval_handlebars(evaluator = $mysh_exec_binding) ⇒ Object

Evaluate any variable substitutions in the input.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mysh/handlebars/eval_handlebars.rb', line 7

def eval_handlebars(evaluator=$mysh_exec_binding)
  string, text, buffer = self, "", []

  # Translate the string with embedded code into Ruby code.
  until string.empty?
    text, code, string = string.partition(/{{.*?}}/m)

    unless text.empty?
      text = text.gsub(/\\[{}]/) {|found| found[1]}
      buffer << "_m_<<#{text.inspect};"
    else
      buffer << "" if buffer.empty?
    end

    unless code.empty?
      if code[-3] == "#"
        buffer << "#{code[2...-3]};"
      else
        buffer << "_m_<<(#{code[2...-2]}).to_s;"
      end
    end
  end

  # Evaluate the result of the translation.
  if buffer.length > 1
    evaluator.eval("_m_ = '';" + buffer.join + "_m_")
  else
    text
  end
end

#eval_variablesObject

Evaluate any variable substitutions in the string.



7
8
9
10
11
12
13
# File 'lib/mysh/shell_variables/evaluate.rb', line 7

def eval_variables
  self.gsub(/((?<!\\)\$\$)|((?<!\\)\$[a-z][a-z0-9_]*)/) do |str|
    sym = str[1..-1].to_sym
    MNV.key?(sym) ? MNV[sym].to_s : str
  end.gsub(/\\\$/, "$")

end

#extract_booleanObject

Extract Boolean data from this string.



7
8
9
# File 'lib/mysh/string_helpers.rb', line 7

def extract_boolean
  self !~ /\A(false|no|off)?\z/i
end

#preprocess(evaluator = $mysh_exec_binding) ⇒ Object

The mysh string pre-processor stack.



33
34
35
# File 'lib/mysh/string_helpers.rb', line 33

def preprocess(evaluator=$mysh_exec_binding)
  self.eval_variables.eval_handlebars(evaluator)
end

#to_host_specObject

Make the file name fit the local system.



12
13
14
15
# File 'lib/mysh/string_helpers.rb', line 12

def to_host_spec
  self.dress_up_slashes
      .dress_up_quotes
end

#to_std_specObject

Make the file name fit the standard notation.



28
29
30
# File 'lib/mysh/string_helpers.rb', line 28

def to_std_spec
  self.gsub("\\", "/")
end