Module: Kernel
- Defined in:
- lib/merb-core/core_ext/kernel.rb,
lib/merb-core/test/test_ext/rspec.rb
Instance Method Summary collapse
-
#__caller_info__(i = 1) ⇒ Array[Array]
private
The file, line and method of the caller.
-
#__caller_lines__(file, line, size = 4) ⇒ Array[Array]
private
Triplets containing the line number, the line and whether this was the searched line.
-
#__profile__(name, min = 1, iter = 100) ⇒ String
private
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, &blk) ⇒ 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.
- #given(*args, &example_group_block) ⇒ Object
-
#load_dependencies(*args) ⇒ Object
private
Loads both gem and library dependencies that are passed in as arguments.
-
#load_dependency(name, *ver, &blk) ⇒ Gem::Dependency
private
Loads the given string as a gem.
- #rescue_require(library, message = nil) ⇒ Object deprecated private Deprecated.
-
#track_dependency(name, *ver, &blk) ⇒ Gem::Dependency
private
Keep track of all required dependencies.
-
#use_orm(orm) ⇒ Object
Used in Merb.root/config/init.rb to tell Merb which ORM (Object Relational Mapper) you wish to use.
-
#use_template_engine(template_engine) ⇒ Object
Used in Merb.root/config/init.rb to tell Merb which template engine to prefer.
- #use_test(*args) ⇒ Object
-
#use_testing_framework(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]
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.
Returns The file, line and method of the caller.
267 268 269 |
# File 'lib/merb-core/core_ext/kernel.rb', line 267 def __caller_info__(i = 1) file, line, meth = caller[i].scan(/(.*?):(\d+):in `(.*?)'/).first end |
#__caller_lines__(file, line, size = 4) ⇒ Array[Array]
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.
Returns Triplets containing the line number, the line and whether this was the searched line.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/merb-core/core_ext/kernel.rb', line 292 def __caller_lines__(file, line, size = 4) line = line.to_i if file =~ /\(erubis\)/ yield :error, "Template Error! Problem while rendering", false elsif !File.file?(file) || !File.readable?(file) yield :error, "File `#{file}' not available", false else lines = File.read(file).split("\n") first_line = (f = line - size - 1) < 0 ? 0 : f old_lines = lines lines = lines[first_line, size * 2 + 1] lines && lines.each_with_index do |str, index| yield index + line - size, str.chomp end end end |
#__profile__(name, min = 1, iter = 100) ⇒ String
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.
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.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/merb-core/core_ext/kernel.rb', line 338 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.
389 390 391 392 393 |
# File 'lib/merb-core/core_ext/kernel.rb', line 389 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.
Parameters
*args<String, Hash, Array> The dependencies to load.
Returns
- Array[(Gem::Dependency, Array)]
-
Gem::Dependencies for the
dependencies specified in args.
110 111 112 113 114 115 116 117 118 |
# File 'lib/merb-core/core_ext/kernel.rb', line 110 def dependencies(*args) args.map do |arg| case arg when String then dependency(arg) when Hash then arg.map { |r,v| dependency(r, v) } when Array then arg.map { |r| dependency(r) } end end end |
#dependency(name, *ver, &blk) ⇒ 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, but it will still be registered.
Parameters
name<String> The name of the gem to load. *ver<Gem::Requirement, Gem::Version, Array, #to_str>
Version requirements to be passed to Gem::Dependency.new.
If the last argument is a Hash, extract the :immediate option,
forcing a dependency to load immediately.
Returns
- Gem::Dependency
-
The dependency information.
53 54 55 56 57 58 59 60 |
# File 'lib/merb-core/core_ext/kernel.rb', line 53 def dependency(name, *ver, &blk) immediate = ver.last.is_a?(Hash) && ver.pop[:immediate] if immediate || Merb::BootLoader.finished?(Merb::BootLoader::Dependencies) load_dependency(name, *ver, &blk) else track_dependency(name, *ver, &blk) end end |
#enforce!(opts = {}) ⇒ Object
Checks that the given objects quack like the given conditions.
379 380 381 382 383 |
# File 'lib/merb-core/core_ext/kernel.rb', line 379 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.
365 366 367 |
# File 'lib/merb-core/core_ext/kernel.rb', line 365 def (args) args.pop if Hash === args.last end |
#given(*args, &example_group_block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/merb-core/test/test_ext/rspec.rb', line 3 def given(*args, &example_group_block) args << {} unless Hash === args.last params = args.last params[:shared] = true describe(*args) do prepend_before(:each) do self.instance_eval(&example_group_block) end end end |
#load_dependencies(*args) ⇒ 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.
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.
136 137 138 139 140 141 142 143 144 |
# File 'lib/merb-core/core_ext/kernel.rb', line 136 def load_dependencies(*args) args.map do |arg| case arg when String then load_dependency(arg) when Hash then arg.map { |r,v| load_dependency(r, v) } when Array then arg.map { |r| load_dependency(r) } end end end |
#load_dependency(name, *ver, &blk) ⇒ Gem::Dependency
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.
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).
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/merb-core/core_ext/kernel.rb', line 80 def load_dependency(name, *ver, &blk) dep = name.is_a?(Gem::Dependency) ? name : track_dependency(name, *ver) gem(dep) rescue Gem::LoadError => e Merb.fatal! "The gem #{name}, #{ver.inspect} was not found", e ensure if block = blk || dep.require_block block.call else begin require dep.name rescue LoadError => e Merb.fatal! "The file #{dep.name} was not found", e end end Merb.logger.verbose!("loading gem '#{dep.name}' ...") return dep # ensure needs explicit return end |
#rescue_require(library, message = nil) ⇒ 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.
Does a basic require, and prints a message if an error occurs.
153 154 155 156 157 158 159 |
# File 'lib/merb-core/core_ext/kernel.rb', line 153 def rescue_require(library, = nil) Merb.logger.warn("Deprecation warning: rescue_require is deprecated") sleep 2.0 require library rescue LoadError, RuntimeError Merb.logger.error!() if end |
#track_dependency(name, *ver, &blk) ⇒ Gem::Dependency
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.
Keep track of all required dependencies.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/merb-core/core_ext/kernel.rb', line 20 def track_dependency(name, *ver, &blk) dep = Gem::Dependency.new(name, ver.empty? ? nil : ver) dep.require_block = blk existing = Merb::BootLoader::Dependencies.dependencies.find { |d| d.name == dep.name } if existing index = Merb::BootLoader::Dependencies.dependencies.index(existing) Merb::BootLoader::Dependencies.dependencies.delete(existing) Merb::BootLoader::Dependencies.dependencies.insert(index, dep) else Merb::BootLoader::Dependencies.dependencies << dep end return dep end |
#use_orm(orm) ⇒ Object
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.
Parameters
- orm<Symbol>
-
The ORM to use.
Returns
nil
Example
use_orm :datamapper
# This will use the DataMapper generator for your ORM
$ merb-gen model ActivityEvent
Notes
If for some reason this is called more than once, latter
call takes over other.
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/merb-core/core_ext/kernel.rb', line 182 def use_orm(orm) begin Merb.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 nil end |
#use_template_engine(template_engine) ⇒ Object
Used in Merb.root/config/init.rb to tell Merb which template engine to prefer.
Parameters
template_engine<Symbol>
The template engine to use.
Returns
nil
Example
use_template_engine :haml
# This will now use haml templates in generators where available.
$ merb-gen resource_controller Project
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/merb-core/core_ext/kernel.rb', line 239 def use_template_engine(template_engine) Merb.template_engine = template_engine if template_engine != :erb if template_engine.in?(:haml, :builder) template_engine_plugin = "merb-#{template_engine}" else template_engine_plugin = "merb_#{template_engine}" end Kernel.dependency(template_engine_plugin) end nil rescue LoadError => e Merb.logger.warn!("The #{template_engine_plugin} gem was not found. You may need to install it.") raise e end |
#use_test(*args) ⇒ Object
218 219 220 |
# File 'lib/merb-core/core_ext/kernel.rb', line 218 def use_test(*args) use_testing_framework(*args) end |
#use_testing_framework(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.
Parameters
- test_framework<Symbol>
-
The test framework to use. Currently only supports :rspec and :test_unit.
Returns
nil
Example
use_test :rspec
# This will now use the RSpec generator for tests
$ merb-gen model ActivityEvent
211 212 213 214 215 216 |
# File 'lib/merb-core/core_ext/kernel.rb', line 211 def use_testing_framework(test_framework, *test_dependencies) Merb.test_framework = test_framework Kernel.dependencies test_dependencies if Merb.env == "test" || Merb.env.nil? nil end |