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]
:api: private.
-
#__caller_lines__(file, line, size = 4) ⇒ Array[Array]
:api: private.
-
#__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 deprecated Deprecated.
- #dependency(name, *opts, &blk) ⇒ Object deprecated Deprecated.
-
#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
-
#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) ⇒ 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]
:api: private
118 119 120 |
# File 'lib/merb-core/core_ext/kernel.rb', line 118 def __caller_info__(i = 1) file, line, meth = caller[i].scan(/(.*?):(\d+):in `(.*?)'/).first end |
#__caller_lines__(file, line, size = 4) ⇒ Array[Array]
:api: private
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/merb-core/core_ext/kernel.rb', line 143 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 if first_line.zero? new_size = line - 1 lines = lines[first_line, size + new_size + 1] else new_size = nil lines = lines[first_line, size * 2 + 1] end lines && lines.each_with_index do |str, index| line_n = index + line line_n = (new_size.nil?) ? line_n - size : line_n - new_size yield line_n, str.chomp end end 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.
:api: private
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/merb-core/core_ext/kernel.rb', line 196 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.
247 248 249 250 251 |
# File 'lib/merb-core/core_ext/kernel.rb', line 247 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.
:api: public
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/merb-core/core_ext/kernel.rb', line 23 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 nil end |
#dependency(name, *opts, &blk) ⇒ Object
Loads the given string as a gem.
:api: public
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/merb-core/core_ext/kernel.rb', line 6 def dependency(name, *opts, &blk) warn "DEPRECATED: Use bundler to setup and load dependency #{name}." = opts.last.is_a?(Hash) ? opts.pop : {} version = opts.pop unless opts.empty? if version warn "DEPRECATED: You want to load gem #{name} with specific version " \ "#{version}. This feature is not supported and the LATEST VERSION " \ "OF THE GEM WILL BE LOADED." end require ([:require_as] ? [:require_as] : name) nil end |
#enforce!(opts = {}) ⇒ Object
Checks that the given objects quack like the given conditions.
:api: public
237 238 239 240 241 |
# File 'lib/merb-core/core_ext/kernel.rb', line 237 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.
:api: public
223 224 225 |
# File 'lib/merb-core/core_ext/kernel.rb', line 223 def (args) args.pop if (args.last.instance_of?(Hash) || args.last.instance_of?(Mash)) 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 |
#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.
:api: public
55 56 57 58 |
# File 'lib/merb-core/core_ext/kernel.rb', line 55 def use_orm(orm) Merb.orm = orm 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
:api: public
103 104 105 106 |
# File 'lib/merb-core/core_ext/kernel.rb', line 103 def use_template_engine(template_engine) Merb.template_engine = template_engine nil end |
#use_test(*args) ⇒ Object
82 83 84 |
# File 'lib/merb-core/core_ext/kernel.rb', line 82 def use_test(*args) use_testing_framework(*args) end |
#use_testing_framework(test_framework) ⇒ 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
:api: public
77 78 79 80 |
# File 'lib/merb-core/core_ext/kernel.rb', line 77 def use_testing_framework(test_framework) Merb.test_framework = test_framework nil end |