Class Index [+]

Quicksearch

Kernel

Public Instance Methods

__caller_info__(i = 1) click to toggle source

@param i The caller number. Defaults to 1.

@return The file, line and method of the caller.

@example

  __caller_info__(1)
    # => ['/usr/lib/ruby/1.8/irb/workspace.rb', '52', 'irb_binding']

:api: private

     # File lib/merb-core/core_ext/kernel.rb, line 118
118:   def __caller_info__(i = 1)
119:     file, line, meth = caller[i].scan(/(.*?):(\d+):in `(.*?)'/).first
120:   end
__caller_lines__(file, line, size = 4) click to toggle source

@param file The file to read. @param line The line number to look for. @param size

  Number of lines to include above and below the the line to look for.
  Defaults to 4.

@return

  Triplets containing the line number, the line and whether this was the
  searched line.

@example

  __caller_lines__('/usr/lib/ruby/1.8/debug.rb', 122, 2) # =>
    [
      [ 120, "  def check_suspend",                               false ],
      [ 121, "    return if Thread.critical",                     false ],
      [ 122, "    while (Thread.critical = true; @suspend_next)", true  ],
      [ 123, "      DEBUGGER__.waiting.push Thread.current",      false ],
      [ 124, "      @suspend_next = false",                       false ]
    ]

:api: private

     # File lib/merb-core/core_ext/kernel.rb, line 143
143:   def __caller_lines__(file, line, size = 4)
144:     line = line.to_i
145:     if file =~ /\(erubis\)/
146:       yield :error, "Template Error! Problem while rendering", false
147:     elsif !File.file?(file) || !File.readable?(file)
148:       yield :error, "File `#{file}' not available", false
149:     else
150:       lines = File.read(file).split("\n")
151:       first_line = (f = line - size - 1) < 0 ? 0 : f
152:       
153:       if first_line.zero?
154:         new_size = line - 1
155:         lines = lines[first_line, size + new_size + 1]
156:       else
157:         new_size = nil
158:         lines = lines[first_line, size * 2 + 1]
159:       end
160: 
161:       lines && lines.each_with_index do |str, index|
162:         line_n = index + line
163:         line_n = (new_size.nil?) ? line_n - size : line_n - new_size
164:         yield line_n, str.chomp
165:       end
166:     end
167:   end
__profile__(name, min=1, iter=100) click to toggle source

Takes a block, profiles the results of running the block specified number of times and generates HTML report.

@param name<#>

  The file name. The result will be written out to
  Merb.root/"log/#{name}.html".

@param min

  Minimum percentage of the total time a method must take for it to be
  included in the result. Defaults to 1.

@return

  The result of the profiling.

@note

  Requires ruby-prof (<tt>sudo gem install ruby-prof</tt>)

@example

  __profile__("MyProfile", 5, 30) do
    rand(10)**rand(10)
    puts "Profile run"
  end

  Assuming that the total time taken for #puts calls was less than 5% of the
  total time to run, #puts won't appear in the profile report.
  The code block will be run 30 times in the example above.

:api: private

     # File lib/merb-core/core_ext/kernel.rb, line 196
196:   def __profile__(name, min=1, iter=100)
197:     require 'ruby-prof' unless defined?(RubyProf)
198:     return_result = ''
199:     result = RubyProf.profile do
200:       iter.times{return_result = yield}
201:     end
202:     printer = RubyProf::GraphHtmlPrinter.new(result)
203:     path = File.join(Merb.root, 'log', "#{name}.html")
204:     File.open(path, 'w') do |file|
205:       printer.print(file, {:min_percent => min,
206:                       :print_file => true})
207:     end
208:     return_result
209:   end
debugger() click to toggle source

Define debugger method so that code even works if debugger was not requested. Drops a note to the logs that Debugger was not available.

     # File lib/merb-core/core_ext/kernel.rb, line 247
247:     def debugger
248:       Merb.logger.info! "\n***** Debugger requested, but was not " +
249:         "available: Start server with --debugger " +
250:         "to enable *****\n"
251:     end
dependencies(*args) click to toggle source

Loads both gem and library dependencies that are passed in as arguments.

:api: public @deprecated

    # File lib/merb-core/core_ext/kernel.rb, line 23
23:   def dependencies(*args)
24:     args.map do |arg|
25:       case arg
26:       when String then dependency(arg)
27:       when Hash then arg.map { |r,v| dependency(r, v) }
28:       when Array then arg.map { |r| dependency(r) }
29:       end
30:     end
31:     nil
32:   end
dependency(name, *opts, &blk) click to toggle source

Loads the given string as a gem.

:api: public @deprecated

    # File lib/merb-core/core_ext/kernel.rb, line 6
 6:   def dependency(name, *opts, &blk)
 7:     warn "DEPRECATED: Use bundler to setup and load dependency #{name}."
 8:     options = opts.last.is_a?(Hash) ? opts.pop : {}
 9:     version = opts.pop unless opts.empty?
10:     if version
11:       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."
12:     end
13:     require (options[:require_as] ? options[:require_as] : name)
14:     nil
15:   end
enforce!(opts = {}) click to toggle source

Checks that the given objects quack like the given conditions.

@param opts

  Conditions to enforce. Each key will receive a quacks_like? call with the
  value (see Object#quacks_like? for details).

@raise

  An object failed to quack like a condition.

:api: public

     # File lib/merb-core/core_ext/kernel.rb, line 237
237:   def enforce!(opts = {})
238:     opts.each do |k,v|
239:       raise ArgumentError, "#{k.inspect} doesn't quack like #{v.inspect}" unless k.quacks_like?(v)
240:     end
241:   end
extract_options_from_args!(args) click to toggle source

Extracts an options hash if it is the last item in the args array. Used internally in methods that take *args.

@param args The arguments to extract the hash from.

@example

  def render(*args,&blk)
    opts = extract_options_from_args!(args) || {}
    # [...]
  end

:api: public

     # File lib/merb-core/core_ext/kernel.rb, line 223
223:   def extract_options_from_args!(args)
224:     args.pop if (args.last.instance_of?(Hash) || args.last.instance_of?(Mash))
225:   end
given(*args, &example_group_block) click to toggle source
    # File lib/merb-core/test/test_ext/rspec.rb, line 3
 3:   def given(*args, &example_group_block)
 4:     args << {} unless Hash === args.last
 5:     params = args.last
 6:     
 7:     params[:shared] = true
 8:     
 9:     describe(*args) do
10:       prepend_before(:each) do
11:         self.instance_eval(&example_group_block)
12:       end
13:     end
14:   end
use_orm(orm) click to toggle source

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

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

    # File lib/merb-core/core_ext/kernel.rb, line 55
55:   def use_orm(orm)
56:     Merb.orm = orm
57:     nil
58:   end
use_template_engine(template_engine) click to toggle source

Used in Merb.root/config/init.rb to tell Merb which template engine to prefer.

Parameters

template_engine

  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

     # File lib/merb-core/core_ext/kernel.rb, line 103
103:   def use_template_engine(template_engine)
104:     Merb.template_engine = template_engine
105:     nil
106:   end
use_test(*args) click to toggle source
    # File lib/merb-core/core_ext/kernel.rb, line 82
82:   def use_test(*args)
83:     use_testing_framework(*args)
84:   end
use_testing_framework(test_framework) click to toggle source

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

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

    # File lib/merb-core/core_ext/kernel.rb, line 77
77:   def use_testing_framework(test_framework)
78:     Merb.test_framework = test_framework
79:     nil
80:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.