Module: Kernel

Defined in:
merb-core/lib/merb-core/core_ext/kernel.rb

Instance Method Summary (collapse)

Instance Method Details

- (Array<String>) __caller_info__(i = 1)

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.

TODO:

Docs: originally, return type was given as Array<Array>, but that's inconsistent with the example.

Returns The file, line and method of the caller.

Examples:

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


112
113
114
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 112

def __caller_info__(i = 1)
  file, line, meth = caller[i].scan(/(.*?):(\d+):in `(.*?)'/).first
end

- (Array<Array>) __caller_lines__(file, line, size = 4)

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.

Examples:

__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 ]
  ]


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 136

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

- (String) __profile__(name, min = 1, iter = 100)

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.

Note:

Requires the ruby-prof gem.

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

__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.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 184

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

- (Object) debugger

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



236
237
238
239
240
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 236

def debugger
  Merb.logger.info! "\n***** Debugger requested, but was not " +
    "available: Start server with --debugger " +
    "to enable *****\n"
end

- (Object) dependencies(*args)

Deprecated.

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



23
24
25
26
27
28
29
30
31
32
# File 'merb-core/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

- (Object) dependency(name, *opts, &blk)

Deprecated.

Loads the given string as a gem.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 6

def dependency(name, *opts, &blk)
  warn "DEPRECATED: Use bundler to setup and load dependency #{name}."
  options = 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 (options[:require_as] ? options[:require_as] : name)
  nil
end

- (Object) enforce!(opts = {})

Note:

Object#quacks_like? is provided by extlib.

Checks that the given objects quack like the given conditions.

Raises:

  • (ArgumentError)

    An object failed to quack like a condition.



226
227
228
229
230
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 226

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

- (Object) extract_options_from_args!(args)

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

Examples:

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


211
212
213
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 211

def extract_options_from_args!(args)
  args.pop if (args.last.instance_of?(Hash) || args.last.instance_of?(Mash))
end

- (nil) use_orm(orm)

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.

If for some reason this is called more than once, later call takes over other.

Examples:

use_orm :datamapper

# This will use the DataMapper generator for your ORM


52
53
54
55
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 52

def use_orm(orm)
  Merb.orm = orm
  nil
end

- (nil) use_template_engine(template_engine)

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

Examples:

use_template_engine :haml

# This will now use haml templates in generators where available.


95
96
97
98
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 95

def use_template_engine(template_engine)
  Merb.template_engine = template_engine
  nil
end

- (Object) use_test(*args)



77
78
79
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 77

def use_test(*args)
  use_testing_framework(*args)
end

- (nil) use_testing_framework(test_framework)

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.

Examples:

use_test :rspec

# This will now use the RSpec generator for tests


72
73
74
75
# File 'merb-core/lib/merb-core/core_ext/kernel.rb', line 72

def use_testing_framework(test_framework)
  Merb.test_framework = test_framework
  nil
end