Module: Kernel
- Defined in:
- lib/merb-core/core_ext/kernel.rb
Instance Method Summary collapse
-
#__caller_info__(i = 1) ⇒ Array[Array]
The file, line and method of the caller.
-
#__caller_lines__(file, line, size = 4) ⇒ Array[Array]
Triplets containing the line number, the line and whether this was the searched line.
-
#__profile__(name, min = 1, iter = 100) ⇒ String
Takes a block, profiles the results of running the block specified number of times and generates HTML report.
-
#debugger ⇒ Object
Define debugger method so that code even works if debugger was not requested.
-
#dependencies(*args) ⇒ Object
Loads both gem and library dependencies that are passed in as arguments.
-
#dependency(name, *ver) ⇒ Object
Loads the given string as a gem.
-
#enforce!(opts = {}) ⇒ Object
Checks that the given objects quack like the given conditions.
-
#extract_options_from_args!(args) ⇒ Object
Extracts an options hash if it is the last item in the args array.
-
#load_dependencies(*args) ⇒ Object
Loads both gem and library dependencies that are passed in as arguments.
-
#load_dependency(name, *ver) ⇒ Object
Loads the given string as a gem.
-
#register_orm(orm) ⇒ Object
private
Registers ORM at generator scope.
-
#register_test_framework(test_framework) ⇒ Object
private
Register test framework at generator scope.
-
#rescue_require(library, message = nil) ⇒ Object
Does a basic require, and prints a message if an error occurs.
-
#supported_test_framework?(test_framework) ⇒ Boolean
Check whether Merb supports test framework.
-
#use_orm(orm) ⇒ Object
Used in Merb.root/config/init.rb to tell Merb which ORM (Object Relational Mapper) you wish to use.
-
#use_test(test_framework, *test_dependencies) ⇒ Object
Used in Merb.root/config/init.rb to tell Merb which testing framework to use.
Instance Method Details
#__caller_info__(i = 1) ⇒ Array[Array]
Returns The file, line and method of the caller.
195 196 197 |
# File 'lib/merb-core/core_ext/kernel.rb', line 195 def __caller_info__(i = 1) file, line, meth = caller[i].scan(/(.*?):(\d+):in `(.*?)'/).first end |
#__caller_lines__(file, line, size = 4) ⇒ Array[Array]
Returns Triplets containing the line number, the line and whether this was the searched line.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/merb-core/core_ext/kernel.rb', line 218 def __caller_lines__(file, line, size = 4) return [['Template Error!', "problem while rendering", false]] if file =~ /\(erubis\)/ lines = File.readlines(file) current = line.to_i - 1 first = current - size first = first < 0 ? 0 : first last = current + size last = last > lines.size ? lines.size : last log = lines[first..last] area = [] log.each_with_index do |line, index| index = index + first + 1 area << [index, line.chomp, index == current + 1] end area end |
#__profile__(name, min = 1, iter = 100) ⇒ String
Requires ruby-prof (sudo gem install ruby-prof
)
Takes a block, profiles the results of running the block specified number of times and generates HTML report.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/merb-core/core_ext/kernel.rb', line 266 def __profile__(name, min=1, iter=100) require 'ruby-prof' unless defined?(RubyProf) return_result = '' result = RubyProf.profile do iter.times{return_result = yield} end printer = RubyProf::GraphHtmlPrinter.new(result) path = File.join(Merb.root, 'log', "#{name}.html") File.open(path, 'w') do |file| printer.print(file, {:min_percent => min, :print_file => true}) end return_result end |
#debugger ⇒ Object
Define debugger method so that code even works if debugger was not requested. Drops a note to the logs that Debugger was not available.
313 314 315 316 317 |
# File 'lib/merb-core/core_ext/kernel.rb', line 313 def debugger Merb.logger.info! "\n***** Debugger requested, but was not " + "available: Start server with --debugger " + "to enable *****\n" end |
#dependencies(*args) ⇒ Object
Loads both gem and library dependencies that are passed in as arguments. Execution is deferred to the Merb::BootLoader::Dependencies.run during bootup.
68 69 70 71 72 73 74 75 76 |
# File 'lib/merb-core/core_ext/kernel.rb', line 68 def dependencies(*args) args.each do |arg| case arg when String then dependency(arg) when Hash then arg.each { |r,v| dependency(r, v) } when Array then arg.each { |r| dependency(r) } end end end |
#dependency(name, *ver) ⇒ Object
Loads the given string as a gem. Execution is deferred until after the logger has been instantiated and the framework directory structure is defined.
If that has already happened, the gem will be activated immediately.
Parameters
Returns
- Array[String, Array[Gem::Requirement, Gem::Version, Array, #to_str]]
-
The name and version information that was passed in.
17 18 19 20 21 22 23 24 |
# File 'lib/merb-core/core_ext/kernel.rb', line 17 def dependency(name, *ver) if Merb::BootLoader.finished?(Merb::BootLoader::Dependencies) load_dependency(name, *ver) else Merb::BootLoader::Dependencies.dependencies << [name, ver] end [name, ver] end |
#enforce!(opts = {}) ⇒ Object
Checks that the given objects quack like the given conditions.
303 304 305 306 307 |
# File 'lib/merb-core/core_ext/kernel.rb', line 303 def enforce!(opts = {}) opts.each do |k,v| raise ArgumentError, "#{k.inspect} doesn't quack like #{v.inspect}" unless k.quacks_like?(v) end end |
#extract_options_from_args!(args) ⇒ Object
Extracts an options hash if it is the last item in the args array. Used internally in methods that take *args.
291 292 293 |
# File 'lib/merb-core/core_ext/kernel.rb', line 291 def (args) args.pop if Hash === args.last end |
#load_dependencies(*args) ⇒ Object
Each argument can be:
- String
-
Single dependency.
- Hash
-
Multiple dependencies where the keys are names and the values versions.
- Array
-
Multiple string dependencies.
Loads both gem and library dependencies that are passed in as arguments.
92 93 94 95 96 97 98 99 100 |
# File 'lib/merb-core/core_ext/kernel.rb', line 92 def load_dependencies(*args) args.each do |arg| case arg when String then load_dependency(arg) when Hash then arg.each { |r,v| load_dependency(r, v) } when Array then arg.each { |r| load_dependency(r) } end end end |
#load_dependency(name, *ver) ⇒ Object
If the gem cannot be found, the method will attempt to require the string as a library.
Loads the given string as a gem.
This new version tries to load the file via ROOT/gems first before moving off to the system gems (so if you have a lower version of a gem in ROOT/gems, it’ll still get loaded).
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/merb-core/core_ext/kernel.rb', line 39 def load_dependency(name, *ver) try_framework = Merb.frozen? begin # If this is a piece of merb, and we're frozen, try to require # first, so we can pick it up from framework/, # otherwise try activating the gem if name =~ /^merb/ && try_framework require name else gem(name, *ver) if ver require name Merb.logger.info!("loading gem '#{name}' ...") end rescue LoadError if try_framework try_framework = false retry else Merb.logger.info!("loading gem '#{name}' ...") # Failed requiring as a gem, let's try loading with a normal require. require name end end end |
#register_orm(orm) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Registers ORM at generator scope.
145 146 147 |
# File 'lib/merb-core/core_ext/kernel.rb', line 145 def register_orm(orm) Merb.orm_generator_scope = orm end |
#register_test_framework(test_framework) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Register test framework at generator scope. Currently Merb has plugins to support RSpec and Test::Unit.
184 185 186 |
# File 'lib/merb-core/core_ext/kernel.rb', line 184 def register_test_framework(test_framework) Merb.test_framework_generator_scope = test_framework end |
#rescue_require(library, message = nil) ⇒ Object
Does a basic require, and prints a message if an error occurs.
106 107 108 109 110 |
# File 'lib/merb-core/core_ext/kernel.rb', line 106 def rescue_require(library, = nil) require library rescue LoadError, RuntimeError Merb.logger.error!() if end |
#supported_test_framework?(test_framework) ⇒ Boolean
Check whether Merb supports test framework. Currently Merb has plugins to support RSpec and Test::Unit.
173 174 175 |
# File 'lib/merb-core/core_ext/kernel.rb', line 173 def supported_test_framework?(test_framework) [:rspec, :test_unit].include?(test_framework.to_sym) end |
#use_orm(orm) ⇒ Object
If for some reason this is called more than once, latter call takes over other.
Used in Merb.root/config/init.rb to tell Merb which ORM (Object Relational Mapper) you wish to use. Currently Merb has plugins to support ActiveRecord, DataMapper, and Sequel.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/merb-core/core_ext/kernel.rb', line 127 def use_orm(orm) begin register_orm(orm) orm_plugin = "merb_#{orm}" Kernel.dependency(orm_plugin) rescue LoadError => e Merb.logger.warn!("The #{orm_plugin} gem was not found. You may need to install it.") raise e end end |
#use_test(test_framework, *test_dependencies) ⇒ Object
Used in Merb.root/config/init.rb to tell Merb which testing framework to use. Currently Merb has plugins to support RSpec and Test::Unit.
160 161 162 163 164 165 |
# File 'lib/merb-core/core_ext/kernel.rb', line 160 def use_test(test_framework, *test_dependencies) raise "use_test only supports :rspec and :test_unit currently" unless supported_test_framework?(test_framework) register_test_framework(test_framework) dependencies test_dependencies if Merb.env == "test" || Merb.env.nil? end |