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
136 def brice(package)
137   package, libs = case package
138     when Hash
139       names = package.keys
140 
141       err = names.size > 1 ? "Too many package names: #{names.join(' ')}" :
142             names.size < 1 ? 'No package name given' : nil
143 
144       raise ArgumentError, err if err
145 
146       [names.first, Array(package.values.first)]
147     else
148       [package, [package]]
149   end
150 
151   if Brice.include?(package)
152     if libs.all? { |lib| !lib || brice_require(lib) { true } }
153       yield Brice.config[package] if block_given?
154     end
155   end
156 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
114 def brice_load(filename, wrap = false, quiet = Brice.quiet, &block)
115   brice_rescue(:load, [filename, wrap], Exception, quiet, &block)
116 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
101 def brice_require(string, quiet = Brice.quiet, &block)
102   brice_rescue(:require, [string], LoadError, quiet, &block)
103 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
71 def brice_rescue(what, args = [], error = Exception, quiet = Brice.quiet)
72   res = send(what, *args)
73 
74   block_given? ? yield : res
75 rescue error => err
76   return if quiet
77 
78   # ideally, we'd want the __FILE__ and __LINE__
79   # of the rc file where the error occurred.
80   location = caller.find { |c|
81     c =~ %r{(?:\A|/)lib/brice/rc/} ||
82     c !~ %r{(?:\A|/)lib/brice[/.]}
83   }
84 
85   warn "#{err.class}: #{err} [#{location}]"
86 
87   warn err.backtrace.map { |line|
88     "        from #{line}"
89   }.join("\n") if Brice.verbose
90 end
brice_run_cmd(cmd, env = {}) click to toggle source

Runs cmd with ENV modified according to env.

    # File lib/brice/dsl.rb
122 def brice_run_cmd(cmd, env = {})
123   ENV.with(env) { %x{#{cmd}} }
124 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
52 def irb_def(symbol, method = nil, &block)
53   irb_rc {
54     IRB::ExtendCommandBundle.class_eval {
55       define_method(symbol, method || block)
56     }
57   }
58 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
43 def irb_rc(&block)
44   Brice.irb_rc << block
45 end