Module: Gat::Checks
Overview
Gat::Checks Module
Check methods return true or false through some arguments. Extend module if you want to add more checkable methods
Check methods are hash based to better human arguments readeable. I mean, when you make a comparation beetwen two values, it is better to use arguments > arguments thatn arguments > arguments
So, need_arguments method is avaible as public inside this module. This function will check the arguments hash and raise a GatgetConfixException if check_method miss one argument. For example
def check_minor_than(arguments)
need_arguments(['minor', 'major'], arguments)
arguments['minor'] < arguments['major']
end
check_minor_than(‘minor’ => 10, ‘bigger’ => 20) # => raise GatgetConfigException check_minor_than(‘minor’ => 10, ‘major’ => 20) # => true check_minor_than(‘minor’ => 20, ‘major’ => 10) # => false
Instance Method Summary collapse
- #check_enough_space(arguments) ⇒ Object
- #check_false(arguments) ⇒ Object
-
#check_file_exists(arguments) ⇒ Object
Check if file exist.
-
#check_file_not_exists(arguments) ⇒ Object
Check if file exist.
- #check_i_am_running(myname = nil) ⇒ Object
- #check_true(arguments) ⇒ Object
- #exit_if_i_am_running(myname = nil) ⇒ Object
-
#need_arguments(needed, avalaible) ⇒ Object
need_arguments checks that all the needed arguments are in array raise GagetConfigException if someone is not avaliable.
Instance Method Details
#check_enough_space(arguments) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/gat/checks.rb', line 60 def check_enough_space(arguments) need_arguments(['where', 'space_needed'], arguments) # get space at where space = %x[ df -B 1 #{ arguments['where'] }].split("\n").last.split(" ")[3].to_i space > arguments['space_needed'].to_i end |
#check_false(arguments) ⇒ Object
72 73 74 75 |
# File 'lib/gat/checks.rb', line 72 def check_false(arguments) need_arguments(['false_expression'], arguments) !eval(arguments['false_expression']) end |
#check_file_exists(arguments) ⇒ Object
Check if file exist
49 50 51 52 |
# File 'lib/gat/checks.rb', line 49 def check_file_exists(arguments) need_arguments(['file_url'], arguments) File.exists?(arguments['file_url']) end |
#check_file_not_exists(arguments) ⇒ Object
Check if file exist
55 56 57 58 |
# File 'lib/gat/checks.rb', line 55 def check_file_not_exists(arguments) need_arguments(['file_url'], arguments) !File.exists?(arguments['file_url']) end |
#check_i_am_running(myname = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gat/checks.rb', line 95 def check_i_am_running(myname = nil) # lock dirs/files myname ||= File.basename($0, ".rb") lockdir="/tmp/#{myname}-lock" pidfile="#{lockdir}/PID" ### start locking attempt r = FileUtils.mkdir(lockdir) rescue r = nil if not r.nil? # lock succeeded, install signal handlers and store the PID trap 0, proc { FileUtils.rm_rf lockdir } r = File.open(pidfile, 'w') {|f| f.write($$) } else # lock failed, now check if the other PID is alive begin f = File.open(pidfile, 'r') otherpid = f.readline.to_i rescue # if it wasn't possible to read the file anymore, another instance probably is # about to remove the lock -- exit, we're *still* locked return true end r = Process.kill(0, otherpid) rescue r = nil if r == nil # lock is stale, remove it and go on trap 0, proc { FileUtils.rm_rf lockdir } r = File.open(pidfile, 'w') {|f| f.write($$) } rescue r=nil return false else # lock is valid and otherpid is active - exit, we're locked! return true end end return false end |
#check_true(arguments) ⇒ Object
67 68 69 70 |
# File 'lib/gat/checks.rb', line 67 def check_true(arguments) need_arguments(['true_expression'], arguments) eval("#{ arguments['true_expression'] }") end |
#exit_if_i_am_running(myname = nil) ⇒ Object
89 90 91 92 93 |
# File 'lib/gat/checks.rb', line 89 def exit_if_i_am_running(myname = nil) if check_i_am_running(myname) exit 0 end end |
#need_arguments(needed, avalaible) ⇒ Object
need_arguments checks that all the needed arguments are in array raise GagetConfigException if someone is not avaliable
81 82 83 84 85 86 87 |
# File 'lib/gat/checks.rb', line 81 def need_arguments(needed, avalaible) needed.each do |need| unless avalaible.keys.include?(need) raise GatgetConfigException.new("needed argument #{ need } not avalaible at #{ self.class.name }", "need_arguments") end end end |