Module: Briefly

Defined in:
lib/briefly.rb,
lib/briefly/rails.rb,
lib/briefly/errors.rb,
lib/briefly/facade.rb,
lib/briefly/builder.rb,
lib/briefly/version.rb,
lib/briefly/rails/db.rb,
lib/briefly/definition.rb,
lib/briefly/rails/reload.rb,
lib/briefly/error_registry.rb

Overview

A terse, curated facade over an application's most frequently reached-for objects.

App = Briefly.define do
use Briefly::Rails
shortcut(:redis) { REDIS_POOL }
end

Defined Under Namespace

Modules: Rails Classes: Builder, Definition, Error, ErrorRegistry, Facade, ReservedNameError, UnknownPackError, UnknownShortcutError

Constant Summary collapse

UNSET =

Sentinel distinguishing "not memoized yet" from a memoized nil.

Object.new.freeze
VERSION =

The gem version.

"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.errorsBriefly::ErrorRegistry (readonly)

Returns the global registry.

Returns:



92
93
94
# File 'lib/briefly.rb', line 92

def errors
  @errors
end

Class Method Details

.define { ... } ⇒ Briefly::Facade

Builds a facade. The block is +instance_eval+'d on a Builder.

Yields:

  • [] the shortcut declarations

Returns:



39
# File 'lib/briefly.rb', line 39

def define(&) = Facade.new.configure(&)

.pack(name) ⇒ #install

Resolves a short name to a pack. A registered constant path is resolved here, not at registration, so a pack's file loads only when something uses it.

The path is walked one segment at a time rather than handed to Object.const_get whole, so a NameError raised inside a pack's file as it autoloads propagates untouched. Rescuing around the whole resolution would launder that bug into an UnknownPackError.

Parameters:

  • name (String, Symbol)

Returns:

  • (#install)

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/briefly.rb', line 62

def pack(name)
  entry = @packs.fetch(name.to_s) { raise UnknownPackError, "unknown pack: #{name.inspect}" }
  return entry unless entry.is_a?(String)

  entry.split("::").reduce(Object) do |mod, segment|
    unless mod.const_defined?(segment, false)
      raise UnknownPackError, "pack #{name.inspect} names #{entry.inspect}, which does not resolve"
    end

    mod.const_get(segment, false)
  end
end

.register(name, pack) ⇒ self

Registers a pack under a short name, so use "myapp/redis" resolves it. Re-registering a name overrides it. There is no inflection and no path guessing: this table is the only source of truth.

Parameters:

  • name (String, Symbol)

    the short name

  • pack (#install, String)

    the pack, or a constant path resolved on first use

Returns:

  • (self)


47
48
49
50
# File 'lib/briefly.rb', line 47

def register(name, pack)
  @packs[name.to_s] = pack
  self
end

.rescue_from(error_class) {|error, shortcut_name| ... } ⇒ self

Registers a handler that applies to every shortcut on every facade, consulted only after the facade's own handlers. Takes no shortcut names.

Parameters:

  • error_class (Class)

    matched against the raised error and its subclasses

Yields:

  • (error, shortcut_name)

    the recovery block; its return value becomes the shortcut's value

Returns:

  • (self)

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
# File 'lib/briefly.rb', line 81

def rescue_from(error_class, &handler)
  unless error_class.is_a?(Class)
    raise ArgumentError, "rescue_from expects the error class first, got #{error_class.inspect}"
  end
  raise ArgumentError, "rescue_from(#{error_class}) requires a block" unless handler

  @errors.add(error_class, nil, handler)
  self
end