class Ichigeki::Hissatsu

Attributes

confirm_dialog[R]
dialog_message[R]
exec_date[R]
in_compilation[R]
is_running[RW]
log_file_postfix[R]
script[R]

Public Class Methods

new(init_properties = {}) click to toggle source
# File lib/ichigeki/hissatsu.rb, line 11
def initialize(init_properties = {})
  init_properties = {
    exec_date: Time.now.strftime("%Y-%m-%d"),
    confirm_dialog: true,
    log_file_postfix: '.log',
    dialog_message: "Do you really execute %s",
    in_compilation: 1,
    script: $0,
    is_running: nil,
  }.merge(init_properties)

  @exec_date        = init_properties[:exec_date]
  @confirm_dialog   = init_properties[:confirm_dialog]
  @in_compilation   = init_properties[:in_compilation]
  @log_file_postfix = init_properties[:log_file_postfix]
  @script           = init_properties[:script]
  @dialog_message   = init_properties[:dialog_message]
  @is_running       = init_properties[:is_running]
end

Public Instance Methods

execute() click to toggle source
# File lib/ichigeki/hissatsu.rb, line 31
def execute
  now   = Time.now;
  today = now.strftime("%Y-%m-%d")

  exiting("exec_date: #{exec_date} is not today") unless exec_date == today

  exiting(sprintf("Can't execue! Execution log file [%s] already exists!", log_file)) if File.exists?(log_file)

  if confirm_dialog
    printf(dialog_message + ' [y/n] [n] ', script.to_s)
    answer = gets.chomp
    exiting 'canceled.' unless answer == 'y'
  end

  STDOUT.flush
  STDERR.flush

  log(["\n",
      '# This log file is generated by ichigeki.',
      "start: #{now.to_s} \n",
      '---',''].join
     )

  self.is_running = 1

  $stdout = std_out
  $stderr = std_err

  done
end

Private Instance Methods

done() click to toggle source
# File lib/ichigeki/hissatsu.rb, line 118
def done
  if is_running
    now =  Time.now.to_s
    log(["\n",
      '','---',"\n",
      "end: #{now}\n", ''].join)
  end
end
exiting(msg = '') click to toggle source
# File lib/ichigeki/hissatsu.rb, line 102
def exiting(msg = '')
  msg = msg + "\n"
  if (self.in_compilation)
    puts msg
    exit 1
  end
end
log(msg) click to toggle source
# File lib/ichigeki/hissatsu.rb, line 114
def log(msg)
  log_fh.puts msg
end
log_fh() click to toggle source
# File lib/ichigeki/hissatsu.rb, line 98
def log_fh
  open(log_file, "a")
end
log_file() click to toggle source
# File lib/ichigeki/hissatsu.rb, line 110
def log_file
  "#{File.dirname(script)}/.#{script.to_s}#{log_file_postfix}"
end
std_err() click to toggle source
# File lib/ichigeki/hissatsu.rb, line 94
def std_err
  twin_err(log_fh)
end
std_out() click to toggle source
# File lib/ichigeki/hissatsu.rb, line 90
def std_out
  twin_out(log_fh)
end
twin_err(f) click to toggle source
# File lib/ichigeki/hissatsu.rb, line 77
def twin_err(f)
  err = Object.new
  err.instance_eval{@ofile=f}
  class <<err
    def write(str)
      STDERR.write(str)
      @ofile.write(str)
    end
  end

  return err
end
twin_out(f) click to toggle source
# File lib/ichigeki/hissatsu.rb, line 64
def twin_out(f)
  out = Object.new
  out.instance_eval{@ofile=f}
  class <<out
    def write(str)
      STDOUT.write(str)
      @ofile.write(str)
    end
  end

  return out
end