Class: UniversalPipeHandler::Cmdlet

Inherits:
Base
  • Object
show all
Defined in:
lib/universal_pipe_handler/cmdlet/cmdlet.rb

Overview

UniversalPipeHandler::Cmdlet

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#allowed_actions?, #chdir, #commandline_arguments?, #copy, #first_argument?, #mkdir, #register_sigint, #remove_quotes, #return_pwd, #set_commandline_arguments

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = true) ⇒ Cmdlet

#

initialize

#


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 40

def initialize(
    commandline_arguments = ARGV,
    run_already           = true
  )
  reset
  # ======================================================================= #
  # Must ensure that it is an Array at all times:
  # ======================================================================= #
  commandline_arguments = [commandline_arguments].flatten.compact
  set_commandline_arguments(
    commandline_arguments
  )
  set_content(commandline_arguments.first)
  case run_already
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Class Method Details

.[](i = '', optional_argument = nil) ⇒ Object

#

UniversalPipeHandler::Cmdlet[]

#


265
266
267
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 265

def self.[](i = '', optional_argument = nil)
  new(i).data?
end

Instance Method Details

#content?Boolean Also known as: content, string?, input?

#

content?

#

Returns:

  • (Boolean)


118
119
120
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 118

def content?
  @internal_hash[:content]
end

#evaluate_the_content(content = content? ) ⇒ Object Also known as: do_evaluate

#

evaluate_the_content

Here we must fetch the toplevel method.

#


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 229

def evaluate_the_content(
    content = content?
  )
  _ = content.dup
  if _.include? ' '
    _ = _.split(' ').first
  end
  ::UniversalPipeHandler.set_working_directory(start_dir?)
  _ = return_alias_to_this_cmdlet_or_leave_it_unmodified(_)
  name_of_the_method = 'cmdlet_'+_.to_s
  unless UniversalPipeHandler.respond_to?(name_of_the_method.to_sym)
    # Require all toplevel methods next:
    require 'universal_pipe_handler/requires/do_require_the_individual_cmdlet_files.rb'
    UniversalPipeHandler.do_require_the_individual_cmdlet_files
  end
  _result = ::UniversalPipeHandler.send(name_of_the_method.to_sym)
  if _result.is_a? Array
    set_result(_result)
  end
end

#may_we_exit?Boolean

#

may_we_exit?

#

Returns:

  • (Boolean)


141
142
143
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 141

def may_we_exit?
  @internal_hash[:may_we_exit]
end

#pipe_aliases?Boolean

#

pipe_aliases?

#

Returns:

  • (Boolean)


134
135
136
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 134

def pipe_aliases?
  @internal_hash[:pipe_aliases]
end

#resetObject

#

reset (reset tag)

#


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 66

def reset
  super()
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @internal_hash
  # ======================================================================= #
  @internal_hash = {}
  # ======================================================================= #
  # === :valid_action
  #
  # This will be true if the action is valid.
  # ======================================================================= #
  @internal_hash[:valid_action] = false
  # ======================================================================= #
  # === :content
  # ======================================================================= #
  @internal_hash[:content] = nil
  # ======================================================================= #
  # === :may_we_exit
  # ======================================================================= #
  @internal_hash[:may_we_exit] = true
  set_start_dir
  # ======================================================================= #
  # === :pipe_aliases
  # ======================================================================= #
  @internal_hash[:pipe_aliases] = YAML.load_file(
    UniversalPipeHandler.file_pipe_aliases
  )
end

#result?Boolean

#

result?

#

Returns:

  • (Boolean)


148
149
150
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 148

def result?
  ::UniversalPipeHandler.result?
end

#return_alias_to_this_cmdlet_or_leave_it_unmodified(i, pipe_aliases = pipe_aliases? ) ⇒ Object

#

return_alias_to_this_cmdlet_or_leave_it_unmodified

#


253
254
255
256
257
258
259
260
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 253

def return_alias_to_this_cmdlet_or_leave_it_unmodified(
    i, pipe_aliases = pipe_aliases?
  )
  if pipe_aliases.has_key? i
    i = pipe_aliases[i]
  end
  return i 
end

#runObject

#

run (run tag)

#


155
156
157
158
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 155

def run
  try_to_find_an_alias
  evaluate_the_content
end

#set_content(i) ⇒ Object

#

set_content

#


102
103
104
105
106
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 102

def set_content(i)
  i = i.join(' ') if i.is_a? Array
  @internal_hash[:content] = i.dup
  # sanitize_content
end

#set_result(i) ⇒ Object

#

set_result

#


177
178
179
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 177

def set_result(i)
  ::UniversalPipeHandler.set_result(i)
end

#set_start_dir(i = return_pwd) ⇒ Object

#

set_start_dir

#


163
164
165
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 163

def set_start_dir(i = return_pwd)
  @internal_hash[:start_dir] = i
end

#start_dir?Boolean

#

start_dir?

#

Returns:

  • (Boolean)


170
171
172
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 170

def start_dir?
  @internal_hash[:start_dir]
end

#strip!Object

#

strip!

#


111
112
113
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 111

def strip!
  @internal_hash[:content].strip! if @internal_hash[:content]
end

#to_sObject

#

to_s

#


127
128
129
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 127

def to_s
  @internal_hash[:content].to_s
end

#try_to_find_an_aliasObject

#

try_to_find_an_alias

We try to find an alias here.

#


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/universal_pipe_handler/cmdlet/cmdlet.rb', line 186

def try_to_find_an_alias
  input_may_include_an_alias = false
  first_command = content?.split(' ')
  if pipe_aliases?.keys.include? first_command
    # ===================================================================== #
    # In this case, we don't need to do any replacement, because the
    # key is already registered.
    # ===================================================================== #
  else # else try to find an alias.
    # ===================================================================== #
    # Grab the possible results next:
    # ===================================================================== #
    result = pipe_aliases?.keys.select { |entry|
      content?.include? entry
    }
    # ===================================================================== #
    # Next sort it by size so we get the highest entry first:
    # ===================================================================== #
    result = result.sort_by(&:length).reverse
    # ===================================================================== #
    # Result may now be something like this Array here:
    #
    #   ["read file", "read"]
    #
    # ===================================================================== #
    input_may_include_an_alias = true unless result.empty?
    if input_may_include_an_alias
      # =================================================================== #
      # We grab the first entry.
      # =================================================================== #
      first = result.first
      the_alias = pipe_aliases?[first]
      content?.gsub!(/#{first}/,the_alias)
    else # else we don't have an alias. Pass through then.
    end
  end
end