#---------------------------------------------------------------# # # # (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, mid) return true if $rubyrun_controller_classes.include?(klass) $rubyrun_rails_env ||= ENV['RAILS_ENV'] begin ($rubyrun_controller_classes << klass; return true) if $rubyrun_rails_env && klass.to_s[-10, 10] == 'Controller' && is_application_controller(klass) && !klass.private_instance_methods(false).include?(return_method_name(mid)) rescue end false 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? [klass.to_s, klass.class.to_s, '*'].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 end