Class: Roebe::Shell::ShellrcParser

Inherits:
Object
  • Object
show all
Defined in:
lib/roebe/shell/shellrc_parser/shellrc_parser.rb

Overview

Roebe::Shell::ShellrcParser

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
HOME_DIR =

else simply hardcode it.

'/root/'
DEFAULT_ROEBESHELLRC_FILE_LOCATION =
#

DEFAULT_ROEBESHELLRC_FILE_LOCATION

Where the roebeshellrc file is usually stored.

#
HOME_DIR+'RoebeShell/roebeshellrc'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i = nil, run_already = true) ⇒ ShellrcParser

#

initialize

#


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 56

def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_location_to_the_roebeshellrc_file(i)
  if block_given?
    yielded = yield
    if yielded.is_a?(Hash) and yielded.has_key?(:debug)
      yielded = yielded.delete(:debug)
      if yielded == true
        enable_debug
      else
        disable_debug
      end
    end
  end
  run if run_already
end

Class Method Details

.path_to_main_file?Boolean

#

Roebe::Shell::ShellrcParser.path_to_main_file?

#

Returns:

  • (Boolean)


192
193
194
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 192

def self.path_to_main_file?
  DEFAULT_ROEBESHELLRC_FILE_LOCATION
end

Instance Method Details

#add_option_to_main_hash(this_key, this_value = nil) ⇒ Object Also known as: set_in_main_hash

#

add_option_to_main_hash

We store the keys as Strings always.

#


130
131
132
133
134
135
136
137
138
139
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 130

def add_option_to_main_hash(
    this_key, this_value = nil
  )
  this_key = this_key.to_s
  case this_key
  when 'colours','colors' # Perform some sanitizing.
    this_key = 'use_colours'
  end
  @hash[this_key] = this_value
end

#consider_displaying_the_dataset_to_the_userObject

#

consider_displaying_the_dataset_to_the_user

#


159
160
161
162
163
164
165
166
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 159

def consider_displaying_the_dataset_to_the_user
  if @debug
    opne 'We have enabled debugging for this class, thus we will '\
         'show more information.'
    opne 'The dataset is:'
    pp dataset?
  end
end

#dataset?Boolean Also known as: dataset

#

dataset?

#

Returns:

  • (Boolean)


121
122
123
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 121

def dataset?
  @dataset
end

#disable_debugObject

#

disable_debug

#


178
179
180
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 178

def disable_debug
  @debug = false
end

#enable_debugObject Also known as: do_debug

#

enable_debug

#


171
172
173
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 171

def enable_debug
  @debug = true
end

#hash?Boolean

#

hash?

#

Returns:

  • (Boolean)


90
91
92
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 90

def hash?
  @hash
end

#location_to_the_roebeshellrc_file?Boolean Also known as: input?

#

location_to_the_roebeshellrc_file?

#

Returns:

  • (Boolean)


114
115
116
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 114

def location_to_the_roebeshellrc_file?
  @location_to_the_roebeshellrc_file
end

#opnnObject

#

opnn

#


185
186
187
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 185

def opnn
  Opn.opn(namespace: NAMESPACE)
end

#resetObject

#

reset (reset tag)

#


79
80
81
82
83
84
85
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 79

def reset
  set_location_to_the_roebeshellrc_file # Initialize.
  @debug = false # if true then we show more info
  @hash = {}
  @dataset = nil
  set_in_main_hash(:colours, nil)
end

#runObject

#

run (run tag)

#


256
257
258
259
260
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 256

def run
  try_to_assign_the_dataset
  start_to_parse_the_file
  consider_displaying_the_dataset_to_the_user
end

#sanitize_datasetObject

#

sanitize_dataset

#


199
200
201
202
203
204
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 199

def sanitize_dataset
  # ======================================================================= #
  # First, replace all "\n\n" with "\n".
  # ======================================================================= #
  dataset?.gsub!(/\n\n/,"\n") if dataset?.include? "\n\n"
end

#set_location_to_the_roebeshellrc_file(i = '') ⇒ Object

#

set_location_to_the_roebeshellrc_file

#


104
105
106
107
108
109
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 104

def set_location_to_the_roebeshellrc_file(i = '')
  i = i.first if i.is_a? Array
  i = i.to_s.dup
  i = DEFAULT_ROEBESHELLRC_FILE_LOCATION if i.empty?
  @location_to_the_roebeshellrc_file = i
end

#start_to_parse_the_fileObject

#

start_to_parse_the_file

This is the method that will parse through the file and set corresponding options.

#


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 212

def start_to_parse_the_file
  if File.exist? input? # Check that the file exists first.
    if @dataset.include? 'colo'
      @dataset =~ /colou?rs: (.+)/ # Handle colours settings.
      add_option_to_main_hash(:use_colours, to_boolean($1.to_s.dup))
    end
    if @dataset.include? 'use_irc'
      @dataset =~ /use_irc: (.+)/ # Handle irc-usage settings.
      add_option_to_main_hash(:use_irc, to_boolean($1.to_s.dup))
    end
    if @dataset.include? 'which_shell'
      @dataset =~ /which_shell: (.+)/ # Handle irc-usage settings.
      add_option_to_main_hash(:which_shell, to_boolean($1.to_s.dup))
    end
  end
end

#to_boolean(i) ⇒ Object

#

to_boolean

This method will return either true or false eventually.

#


146
147
148
149
150
151
152
153
154
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 146

def to_boolean(i)
  case i
  when 'true'
    return true
  when 'false'
    return false
  end
  i
end

#try_to_assign_the_dataset(i = input?) ) ⇒ Object

#

try_to_assign_the_dataset

#


232
233
234
235
236
237
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 232

def try_to_assign_the_dataset(i = input?)
  if File.exist? i # Check that the file exists first.
    @dataset = File.read(i)
    sanitize_dataset
  end
end

#use_colours?Boolean Also known as: colours?

#

use_colours?

#

Returns:

  • (Boolean)


97
98
99
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 97

def use_colours?
  hash?['use_colours']
end

#use_irc?Boolean

#

use_irc?

#

Returns:

  • (Boolean)


242
243
244
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 242

def use_irc?
  hash?['use_irc']
end

#which_shell?Boolean

#

which_shell?

#

Returns:

  • (Boolean)


249
250
251
# File 'lib/roebe/shell/shellrc_parser/shellrc_parser.rb', line 249

def which_shell?
  hash?['which_shell']
end