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



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/lib/utils.rb', line 434

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



414
415
416
417
418
# File 'lib/lib/utils.rb', line 414

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:

  • to extract.

  • (defaults to: true)

    boolean to repeat

Returns:

  • variable boolean, arguments - extract_option



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/lib/utils.rb', line 477

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, args = {}) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/lib/utils.rb', line 504

def extract_option_value option, args={}

  if args[:multiple].nil?
    args[:multiple] = false
  end

  if args[:separator].nil?
    args[:separator] = '='
  end

  result = false
  if args[:multiple]
    multiple_value = []
    while self.include? option
      index = self.index(option)
      self.delete_at(index)
      result = true
      value = self.at(index)
      multiple_value << self.at(index).split(args[:separator]).first
      multiple_value << self.at(index).split(args[:separator]).last
      self.delete_at(index)
    end
    return [result, multiple_value]
  else
    value  = nil
    while 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
end

#extract_symbol(symbol) ⇒ Object

Self extract symbol.

Parameters:

  • to retrive

Returns:

  • boolean



423
424
425
426
427
428
429
430
# File 'lib/lib/utils.rb', line 423

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