Class: Array

Inherits:
Object show all
Defined in:
lib/lib/utils.rb

Instance Method Summary collapse

Instance Method Details

#extract_color ⇒ Object

Self extract color.

Returns:

  • boolean



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/lib/utils.rb', line 329

def extract_color
  colors = String.colors
  color  = :default
  self.each do |argument|
    if argument.symbol?
      if colors.include? argument
        color = argument
        self.delete(argument)
        return color
      end
    else
      if argument.string?
        if argument.start_with? ':'
          color_candidate = argument.gsub(':','').to_sym
          color = color_candidate if colors.include? color_candidate
          self.delete(argument)
          return color
        end
      end
    end
  end
  return color
end

#extract_first ⇒ Object

Self pop first element.

Returns:

  • first element



309
310
311
312
313
# File 'lib/lib/utils.rb', line 309

def extract_first
  first = self[0]
  self.delete_at(0)
  return first
end

#extract_option(option, single = true) ⇒ Object

Self extract option. Sample args = [xxx -x -vvv -c -vcv -v2 -vvvvv -s :json :yellow :red] args.extract_option '-x' => true args.extract_option '-f' => false args.extract_option '-v', false => 8 args.extract_symbol :json => true args.extract_symbol :yaml => false args.extract_color => :yellow args.extract_color => red args => [-c -vcv -v2 -s ]

Parameters:

  • option —

    to extract.

  • single (defaults to: true) —

    boolean to repeat

Returns:

  • variable boolean, arguments - extract_option



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/lib/utils.rb', line 372

def extract_option option, single = true
  if single
    result = false
    if self.include? option
      index = self.index(option)
      self.delete_at(index)
      result = true
    end
  else
    result = 0
    self.each do |argument|
      if argument.start_with? option
       #puts "'#{option}' '#{argument}' #{argument.start_with? option} \n"
       search_char = option.sub('-','')
       aux_string  = argument.sub('-','')
       count       = argument.count(search_char)
       if (count == aux_string.size)
         result = result + count
         #self.delete(argument)
       end
      end

    end
  end
  return result
end

#extract_option_value(option) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/lib/utils.rb', line 399

def extract_option_value option
  result = false
  value  = nil
  if self.include? option
    index = self.index(option)
    self.delete_at(index)
    result = true
    value = self.at(index)
    self.delete_at(index)
  end
  return [result, value]
end

#extract_symbol(symbol) ⇒ Object

Self extract symbol.

Parameters:

  • symbol —

    to retrive

Returns:

  • boolean



318
319
320
321
322
323
324
325
# File 'lib/lib/utils.rb', line 318

def extract_symbol symbol
  status = false
  if self.include? symbol
    status = true
    self.delete(symbol)
  end
  return status
end