Module: Kernel

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

Instance Method Summary collapse

Instance Method Details

#__caller_info__(i = 1) ⇒ Array[Array]

Returns The file, line and method of the caller.

Examples:

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

Parameters:

  • i (Fixnum) (defaults to: 1)

    The caller number. Defaults to 1.

Returns:

  • (Array[Array])

    The file, line and method of the caller.



179
180
181
# File 'lib/merb-core/core_ext/kernel.rb', line 179

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.

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

Parameters:

  • file (String)

    The file to read.

  • line (Fixnum)

    The line number to look for.

  • size (Fixnum) (defaults to: 4)

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

Returns:

  • (Array[Array])

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/merb-core/core_ext/kernel.rb', line 202

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
    lines = lines[first_line, size * 2 + 1]
    
    lines.each_with_index do |str, index|
      yield index + line - size, str.chomp
    end
  end
  # 
  # 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

Note:

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.

Examples:

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

Parameters:

  • name (#to_s)

    The file name. The result will be written out to Merb.root/“log/#name.html”.

  • min (Fixnum) (defaults to: 1)

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

Returns:

  • (String)

    The result of the profiling.



264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/merb-core/core_ext/kernel.rb', line 264

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

#debuggerObject

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



311
312
313
314
315
# File 'lib/merb-core/core_ext/kernel.rb', line 311

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.



55
56
57
58
59
60
61
62
63
# File 'lib/merb-core/core_ext/kernel.rb', line 55

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.

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



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.

Parameters:

  • opts (Hash) (defaults to: {})

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

Raises:

  • (ArgumentError)

    An object failed to quack like a condition.



301
302
303
304
305
# File 'lib/merb-core/core_ext/kernel.rb', line 301

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.

Examples:

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

Parameters:

  • args (Array)

    The arguments to extract the hash from.



289
290
291
# File 'lib/merb-core/core_ext/kernel.rb', line 289

def extract_options_from_args!(args)
  args.pop if Hash === args.last
end

#load_dependencies(*args) ⇒ Object

Note:

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.

Examples:

dependencies “RedCloth” # Loads the the RedCloth gem

dependencies “RedCloth”, “merb_helpers” # Loads RedCloth and merb_helpers

dependencies “RedCloth” => “3.0” # Loads RedCloth 3.0

Parameters:

  • *args (String, Hash, Array)

    The dependencies to load.



79
80
81
82
83
84
85
86
87
# File 'lib/merb-core/core_ext/kernel.rb', line 79

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

Note:

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

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/merb-core/core_ext/kernel.rb', line 39

def load_dependency(name, *ver)
  begin
    gem(name, *ver) if ver
    require name
    Merb.logger.info!("loading gem '#{name}' ...")
  rescue LoadError
    Merb.logger.info!("loading gem '#{name}' ...")
    # Failed requiring as a gem, let's try loading with a normal require.
    require name
  end
end

#rescue_require(library, message = nil) ⇒ Object

Does a basic require, and prints a message if an error occurs.

Parameters:

  • library (to_s)

    The library to attempt to include.

  • message (String) (defaults to: nil)

    The error to add to the log upon failure. Defaults to nil.



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

def rescue_require(library, message = nil)
  require library
rescue LoadError, RuntimeError
  Merb.logger.error!(message) if message
end

#use_orm(orm) ⇒ Object

Note:

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.

Examples:

use_orm :datamapper

# This will use the DataMapper generator for your ORM
$ merb-gen model ActivityEvent

Parameters:

  • orm (Symbol)

    The ORM to use.



115
116
117
118
119
120
121
122
123
124
# File 'lib/merb-core/core_ext/kernel.rb', line 115

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
end

#use_template_engine(template_engine) ⇒ Object

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.
$ merb-gen resource_controller Project 

Parameters:

  • template_engine (Symbol)

    The template engine to use.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/merb-core/core_ext/kernel.rb', line 156

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

Examples:

use_test :rspec

# This will now use the RSpec generator for tests
$ merb-gen model ActivityEvent

Parameters:

  • test_framework (Symbol)

    The test framework to use. Currently only supports :rspec and :test_unit.



138
139
140
141
142
# File 'lib/merb-core/core_ext/kernel.rb', line 138

def use_test(test_framework, *test_dependencies)
  Merb.test_framework = test_framework
  
  Kernel.dependencies test_dependencies if Merb.env == "test" || Merb.env.nil?
end