#---------------------------------------------------------------# # # # (C) Copyright Rubysophic Inc. 2007-2008 # # All rights reserved. # # # # Use, duplication or disclosure of the code is not permitted # # unless licensed. # # # # Last Updated: 7/09/08 # #---------------------------------------------------------------# # # # RubyRunUtils__ is a module that owns common methods shared # # by all the other RubyRun modules. # # # #---------------------------------------------------------------# module RubyRunUtils__ require 'rubyrun_globals' include RubyRunGlobals # Return a readable thread ID for the current thread of execution def get_thread_id(th=Thread.current) th.inspect.split(/Thread:0x/)[1].split(/ .*?>/)[0] end # Retrieve caller details (filename, line number, method name) def get_caller_detail(n=3) caller(0)[n].gsub("#{RUBYRUN_PREFIX}_", '').gsub('//', '/') end # Environment variable not defined or defined with nil value is deemed to # be non-existent def env_var_exists?(var) ENV[var].nil? || ENV[var] == '' ? false : true end # Error exit def fatal_exit(e) $stderr.print e.to_s + "\n" + e.backtrace.join("\n") exit(-1) end # Return true if a Rails Action Controller class # A module or object has no superclass hence the rescue clause def is_rails_controller?(klass) return true if $rubyrun_controller_classes.include?(klass) $rubyrun_rails_env ||= ENV['RAILS_ENV'] if $rubyrun_rails_env && !$rubyrun_monitor_thr.alive? && !$rubyrun_monitor $rubyrun_logger.info('----- RubyRun Thread Monitor Restarted -----') start_thread_monitor $rubyrun_monitor = true end begin ($rubyrun_controller_classes << klass; return true) if $rubyrun_rails_env && klass.to_s[-10, 10] == 'Controller' && is_application_controller(klass) rescue end false end # Return true if this is an action method in a controller class def is_action?(klass, mid) !(klass.private_instance_methods(false).include?(return_mid(mid)) || klass.protected_instance_methods(false).include?(return_mid(mid))) end # Given a class, it's deemed to be a Rails Action Controller if one of its # ancestors is ApplicationController def is_application_controller(klass) return false unless klass.superclass if klass.superclass == ApplicationController return true else is_application_controller(klass.superclass) end end # Return false if the passed in hash is empty # Return false if the hash doenst even have the class name as a key # Return true if the hash has the key but the method array is empty # Return true if the method array has a case-insensitive matching name, # matching can be exact or 'include'. # Otherwise return false def is_in?(hash, klass, mid, mode='loose') return false if hash.empty? [return_class_name(klass), '*'].each {|name| if hash.has_key?(name) return true if hash[name].empty? method_name = return_method_name(mid) hash[name].each {|meth_name| case mode when 'strict' return true if method_name.downcase == meth_name.downcase when 'loose' return true if method_name.downcase.include?(meth_name.downcase) end } end } false end # Return method name since mid can be an method object ID or a string def return_method_name(mid) mid.kind_of?(String) ? mid : mid.id2name end # Return the true class name of either class or object singleton def return_class_name(klass) klass_name = klass.to_s klass_name.include?('#<') ? klass.class.to_s : klass_name end def return_mid(id) RUBY_VERSION =~ /1.9/ ? id.to_sym : return_method_name(id) end end