class Cookbooks::ShowLastUpdated

Constants

DISPLAY_N_PROGRAMS
#

DISPLAY_N_PROGRAMS

How many programs to use by default. We can also use the maximum amount of programs instead.

#
#

HEADER

#
NAMESPACE
#

NAMESPACE

#

Public Class Methods

dataset?() click to toggle source
#

#dataset?

This method will return an Array that is already sorted. Most recent entries, aka the first ones in that Array, will be the programs that were updated last.

Usage example:

x = Cookbooks::ShowLastUpdated.dataset?; pp x; ''
#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 349
def self.dataset?
  self.new { :do_not_output_the_result }.dataset?
end
new( commandline_arguments = nil, run_already = true ) { || ... } click to toggle source
#

initialize

#
Calls superclass method
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 48
def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  super()
  reset
  set_commandline_arguments(commandline_arguments)
  if block_given?
    yielded = yield
    case yielded
    when :do_not_output_the_result
      do_not_output_the_result
    end
  end
  run if run_already
end

Public Instance Methods

array?()
Alias for: array_dataset?
array_dataset?() click to toggle source
#

array_dataset?

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 333
def array_dataset?
  @array_dataset
end
Also aliased as: array?, main_dataset?, dataset?
check_against_menu( i = @commandline_arguments )
Alias for: menu
dataset?()
Alias for: array_dataset?
do_not_output_the_result()
do_not_show_the_result() click to toggle source
#

#do_not_show_the_result

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 81
def do_not_show_the_result
  @show_result = false
end
Also aliased as: do_not_output_the_result
filter_the_array_dataset() click to toggle source
#

#filter_the_array_dataset

Here we will apply the maximum threshold stored in @display_n_programs.

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 199
def filter_the_array_dataset
  unless @display_n_programs.is_a? Symbol # If it is :everything then we will show every program.
    @array_dataset = @array_dataset[0, @display_n_programs]
  end
end
inform_the_user_what_we_will_do() click to toggle source
#

#inform_the_user_what_we_will_do

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 208
def inform_the_user_what_we_will_do
  if @show_result
    cliner {
      opnn; e "Displaying the #{swarn('last '+n_programs?.to_s)}"\
              " updated programs next:"
    }
  end
end
main_dataset?()
Alias for: array_dataset?
menu( i = @commandline_arguments ) click to toggle source
#

menu (menu tag)

#
Also aliased as: check_against_menu
n_programs?() click to toggle source
#

n_programs?

Tell us how many programs are there.

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 262
def n_programs?
  @display_n_programs
end
obtain_last_updated_programs() click to toggle source
#

#obtain_last_updated_programs

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 119
def obtain_last_updated_programs
  if use_grep?
    cmd = 'grep -r last_update '+individual_cookbooks_dir?+'*'
    if show_result?
      opnn; e 'The command "grep" will be used, with the following syntax:'
      e
      e cmd # Display that particular grep command as well.
      e
    end
  end
  inform_the_user_what_we_will_do if show_result?
  # ======================================================================= #
  # This is the header.
  # ======================================================================= #
  output_header # <- This method calls a trailing cliner already.
  case @obtain_results_via
  when :grep
    # ===================================================================== #
    # We still have to populate the @array_dataset variable.
    # So first, make use of grep:
    # ===================================================================== #
    result = `#{cmd}`
    array = result.split(N)
    use_this_regex = /last_update: (.+)/
    array.each_with_index {|line, index| index += 1
      line.chomp!
      splitted_at_colon = line.split(':') # Split at ':' colon tokens.
      program = File.basename(splitted_at_colon.first).sub(/\.yml$/,'')
      last_update = line.scan(use_this_regex).flatten.first
      @array_dataset << [program, last_update, index]
    }
  when :ruby
    programs = Cookbooks.available_programs?
    programs.each_with_index { |program, index|
      index += 1
      # =================================================================== #
      # @dataset.assign(program) # This is an instance of Cookbooks::Cookbook.
      # Note that the next line can be fairly slow. As an alternative
      # solution, we can also use grep.
      # =================================================================== #
      @dataset = Cookbooks::Cookbook.new(program) { :bypass_menu }
      begin
        last_update = @dataset.last_update?
        if last_update.empty?
          opnn; e swarn('No last update was found for the program:')
          opnn; e sfancy(program)
        else # Else append the dataset.
          @array_dataset << [program, last_update, index]
        end
      rescue Exception => error
        pp error
        opnn; e 'The program that failed was: `'+sfancy(program.to_s)+'`'
      end
    }
  end
  cliner
  sort_the_array_dataset
  filter_the_array_dataset
end
opnn() click to toggle source
#

opnn

#
Calls superclass method Cookbooks::Base#opnn
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 269
def opnn
  super(NAMESPACE)
end
output_header() click to toggle source
#

#output_header

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 105
def output_header
  e HEADER if @show_result
end
report_results(i = @array_dataset) click to toggle source
#

#report_results

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 276
def report_results(i = @array_dataset) 
  if @show_result
    i.each { |name, date, index|
      name = '%-25s' % name
      date = date.center(20)
      date = mediumspringgreen(date) if use_colours?
      index = index.to_s.rjust(4)
      if use_colours?
        index = seagreen(index)
      end
      e '| '+name+' | '+date.to_s+' | '+index
    }
    cliner
  end
end
reset() click to toggle source
#

reset

#
Calls superclass method Cookbooks::Base#reset
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 68
def reset # reset tag
  super()
  set_commandline_arguments # Initialize to its default value.
  set_display_n_programs
  @dataset = nil
  @array_dataset = [] # <- Will be an Array of: [program, last_update, index]
  @obtain_results_via = :grep # Can be :grep or :ruby.
  @show_result = true
end
run() click to toggle source
#

run (run tag)

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 324
def run
  check_against_menu # First check against the menu.
  obtain_last_updated_programs
  report_results
end
set_commandline_arguments(i = nil) click to toggle source
#

#set_commandline_arguments

Right now we need not more than one option here.

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 97
def set_commandline_arguments(i = nil)
  i = i.join(' ') if i.is_a? Array
  @commandline_arguments = i
end
set_display_n_programs( i = Cookbooks.n_registered_programs? ) click to toggle source
#

#set_display_n_programs

This method will also accept a regex optionally.

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 238
def set_display_n_programs(
    i = Cookbooks.n_registered_programs?
  )
  if i.is_a? String
    if i.include? 'n_programs'
      i =~ /n_programs (\d+)/
      i = $1.to_s.dup
    elsif i.include? 'nprograms'
      i =~ /nprograms (\d+)/
      i = $1.to_s.dup
    elsif i.start_with?('--n ') or i.start_with?('-n')
      i =~ /n ?(\d+)/
      i = $1.to_s.dup
    end
  end
  i = i.to_i # Must be an Integer.
  @display_n_programs = i
end
show_help() click to toggle source
#

#show_help (help tag)

Invocation example:

slu --n_programs 3000
#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 223
def show_help
  e 'To display '+simp('n programs')+', where '+simp('n')+' may '\
    'be 500, do this:'
  e
  e '   slu --n_programs 500'
  e '   slu --nprograms 500'
  e '   slu --n 500'
  e
end
show_result?() click to toggle source
#

show_result?

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 112
def show_result?
  @show_result
end
sort_the_array_dataset() click to toggle source
#

#sort_the_array_dataset

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 182
def sort_the_array_dataset
  @array_dataset = @array_dataset.sort_by {|i|
    begin
      this_date = i[1]
      DateTime.parse this_date
    rescue Exception => error
      pp error
      pp i
    end
  }.reverse # .reverse so we can keep it in a nicer layout.
end
use_grep?() click to toggle source
#

use_grep?

#
# File lib/cookbooks/utility_scripts/show_last_updated.rb, line 88
def use_grep?
  @obtain_results_via == :grep
end