Class: Briefly::Facade

Inherits:
Object
  • Object
show all
Defined in:
lib/briefly/facade.rb

Overview

The object returned by define. Shortcuts are compiled onto its singleton class as real methods, so respond_to?, console tab-completion and test stubbing all work unaided.

Memo store: reads are lock-free against a frozen snapshot hash; writes swap in a new frozen hash under a reentrant Monitor, because a memoized body may call another memoized shortcut.

Constant Summary collapse

RESERVED =

Names a shortcut may not take: every facade method, plus Briefly's own private ones, plus the Kernel private methods the facade itself calls with an implicit receiver. Other Kernel privates (+puts+, format, ...) are deliberately absent: shadowing those is the user's call.

(Facade.instance_methods + Facade.private_instance_methods(false) + %i[raise]).freeze

Instance Method Summary collapse

Constructor Details

#initializeFacade

Returns a new instance of Facade.



12
13
14
15
16
17
18
19
20
# File 'lib/briefly/facade.rb', line 12

def initialize
  @__defs = {}
  @__aliases = {}
  @__memos = {}.freeze
  @__monitor = Monitor.new
  @__errors = ErrorRegistry.new
  @__public = [].freeze
  @__children = {}
end

Instance Method Details

#clear_memos!self Also known as: reset!

Drops every memoized value, here and in every namespace. Thread-safe.

Returns:

  • (self)


32
33
34
35
36
37
38
# File 'lib/briefly/facade.rb', line 32

def clear_memos!
  @__monitor.synchronize { @__memos = {}.freeze }
  # Each namespace owns its memo store and its own lock, so a child clears outside our monitor.
  # One `Reload` on the root therefore clears the whole tree, including namespaces holding no pack.
  @__children.each_value(&:clear_memos!)
  self
end

#configure { ... } ⇒ self

Reopens the facade for another builder pass and recompiles. The builder starts from copies of the current definitions, so a pass that raises leaves the live facade — and every namespace under it — untouched.

Yields:

Returns:

  • (self)


47
48
49
50
51
52
53
54
55
# File 'lib/briefly/facade.rb', line 47

def configure(&block)
  builder = __prepare
  builder.instance_eval(&block) if block
  # `compile!` validates the whole tree before `__commit` installs any of it. Splitting the two is
  # what keeps a namespace pass atomic: `Builder#namespace` collects its child's pass rather than
  # running it, so a later failure anywhere cannot leave an already-reachable child half-updated.
  __commit(builder.compile!)
  self
end

#inspectString Also known as: to_s

Returns a summary listing shortcut names; never dumps memo internals.

Returns:

  • (String)

    a summary listing shortcut names; never dumps memo internals



58
# File 'lib/briefly/facade.rb', line 58

def inspect = "#<#{self.class.name} shortcuts=#{shortcuts.inspect}>"

#shortcut?(name) ⇒ Boolean

Parameters:

  • name (Symbol)

    a canonical name or an alias

Returns:

  • (Boolean)


27
# File 'lib/briefly/facade.rb', line 27

def shortcut?(name) = @__defs.key?(name) || @__aliases.key?(name)

#shortcutsArray<Symbol>

Returns canonical shortcut names, sorted; aliases excluded.

Returns:

  • (Array<Symbol>)

    canonical shortcut names, sorted; aliases excluded



23
# File 'lib/briefly/facade.rb', line 23

def shortcuts = @__defs.keys.sort