module Brice::DSL

Certain global helper methods for use inside IRb extensions. Also available inside the IRb session.

Public Instance Methods

brice(package) # package == lib click to toggle source
brice(package → lib)
brice(package → [lib, ...])

Declare package package. Optionally load given libraries (see below) and configure the package if it has been enabled/included.

package can be a String which already names the library to be loaded or a Hash of the form package => lib or package => [lib, ...].

# File lib/brice/dsl.rb, line 136
def brice(package)
  package, libs = case package
    when Hash
      names = package.keys

      err = names.size > 1 ? "Too many package names: #{names.join(' ')}" :
            names.size < 1 ? 'No package name given' : nil

      raise ArgumentError, err if err

      [names.first, Array(package.values.first)]
    else
      [package, [package]]
  end

  if Brice.include?(package)
    if libs.all? { |lib| !lib || brice_require(lib) { true } }
      yield Brice.config[package] if block_given?
    end
  end
end
brice_load(filename[, wrap[, quiet]]) click to toggle source
brice_load(filename[, wrap[, quiet]]) { ... }

Kernel#load the file named filename with argument wrap and optionally execute the block in case of success. Doesn't warn about load errors when quiet is true.

Returns either the result of the executed method or of the block.

# File lib/brice/dsl.rb, line 114
def brice_load(filename, wrap = false, quiet = Brice.quiet, &block)
  brice_rescue(:load, [filename, wrap], Exception, quiet, &block)
end
brice_require(string[, quiet]) click to toggle source
brice_require(string[, quiet]) { ... }

Kernel#require the library named string and optionally execute the block in case of success. Doesn't warn about load errors when quiet is true.

Returns either the result of the executed method or of the block.

# File lib/brice/dsl.rb, line 101
def brice_require(string, quiet = Brice.quiet, &block)
  brice_rescue(:require, [string], LoadError, quiet, &block)
end
brice_rescue(what[, args[, error[, quiet]]]) click to toggle source

Call what with args and rescue potential error, optionally executing block in case of success. Gives a nicer error location instead of the full backtrace. Doesn't warn about any errors when quiet is true.

Returns either the result of the executed method or of the block.

# File lib/brice/dsl.rb, line 71
def brice_rescue(what, args = [], error = Exception, quiet = Brice.quiet)
  res = send(what, *args)

  block_given? ? yield : res
rescue error => err
  return if quiet

  # ideally, we'd want the __FILE__ and __LINE__
  # of the rc file where the error occurred.
  location = caller.find { |c|
    c =~ %r{(?:\A|/)lib/brice/rc/} ||
    c !~ %r{(?:\A|/)lib/brice[/.]}
  }

  warn "#{err.class}: #{err} [#{location}]"

  warn err.backtrace.map { |line|
    "        from #{line}"
  }.join("\n") if Brice.verbose
end
brice_run_cmd(cmd, env = {}) click to toggle source

Runs cmd with ENV modified according to env.

# File lib/brice/dsl.rb, line 122
def brice_run_cmd(cmd, env = {})
  ENV.with(env) { %x{#{cmd}} }
end
define_irb_method(symbol, method = nil, &block)
Alias for: irb_def
irb_def(symbol) { ... } click to toggle source
irb_def(symbol, method)

Define a method for use inside the IRb session.

# File lib/brice/dsl.rb, line 52
def irb_def(symbol, method = nil, &block)
  irb_rc {
    IRB::ExtendCommandBundle.class_eval {
      define_method(symbol, method || block)
    }
  }
end
Also aliased as: define_irb_method
irb_rc { ... } click to toggle source

Add IRB_RC proc (to be executed whenever a (sub-)session is started).

# File lib/brice/dsl.rb, line 43
def irb_rc(&block)
  Brice.irb_rc << block
end