Module: Brice::DSL

Includes:
Nuggets::Object::SilenceMixin
Included in:
Brice
Defined in:
lib/brice/dsl.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#brice(package) ⇒ Object

call-seq:

brice(package)  # package == lib
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, ...].


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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 = false, quiet = Brice.quiet, &block) ⇒ Object

call-seq:

brice_load(filename[, wrap[, quiet]])
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.


114
115
116
# 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 = Brice.quiet, &block) ⇒ Object

call-seq:

brice_require(string[, quiet])
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.


101
102
103
# 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 = Exception, quiet = Brice.quiet) ⇒ Object

call-seq:

brice_rescue(what[, args[, error[, quiet]]])

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.


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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 = {}) ⇒ Object

call-seq:

brice_run_cmd(cmd, env = {})

Runs cmd with ENV modified according to env.


122
123
124
# File 'lib/brice/dsl.rb', line 122

def brice_run_cmd(cmd, env = {})
  ENV.with(env) { %x{#{cmd}} }
end

#irb_def(symbol, method = nil, &block) ⇒ Object Also known as: define_irb_method

call-seq:

irb_def(symbol) { ... }
irb_def(symbol, method)

Define a method for use inside the IRb session.


52
53
54
55
56
57
58
# 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

#irb_rc(&block) ⇒ Object

call-seq:

irb_rc { ... }

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


43
44
45
# File 'lib/brice/dsl.rb', line 43

def irb_rc(&block)
  Brice.irb_rc << block
end