Class: Briefly::Shortcut

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

Overview

One shortcut — and the object Builder#shortcut hands back so you can refine it. A declaration's memoization and its scoped error handlers both live here, on the shortcut itself, so refining after a redeclaration affects the declaration you named, exactly as its body and aliases do.

shortcut(:catalog) { Catalog.load }.memoize
shortcut(:redis) { REDIS_POOL }.rescue_from(Redis::BaseError) { nil }

The name, aliases, body and location are internal; #memoize and #rescue_from are its public refinement surface. It holds no Builder reference — both refinements are self-contained.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canonical, aliases, body, source_location) ⇒ Shortcut

Returns a new instance of Shortcut.

Parameters:

  • canonical (Symbol)
  • aliases (Array<Symbol>)
  • body (Proc)
  • source_location (Array(String, Integer))


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

def initialize(canonical, aliases, body, source_location)
  @canonical = canonical
  @aliases = aliases
  @body = body
  @raw_name = Candor.body_name(canonical)
  @memoized = false
  @rescues = []
  @source_location = source_location
end

Instance Attribute Details

#aliasesArray<Symbol> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns additional method names delegating to the same body and memo cell.

Returns:

  • (Array<Symbol>)

    additional method names delegating to the same body and memo cell



20
21
22
# File 'lib/briefly/shortcut.rb', line 20

def aliases
  @aliases
end

#bodyProc (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the implementation, bound to the facade at call time.

Returns:

  • (Proc)

    the implementation, bound to the facade at call time



24
25
26
# File 'lib/briefly/shortcut.rb', line 24

def body
  @body
end

#canonicalSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the primary method name.

Returns:

  • (Symbol)

    the primary method name



16
17
18
# File 'lib/briefly/shortcut.rb', line 16

def canonical
  @canonical
end

#raw_nameSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the private singleton method the body compiles to.

Returns:

  • (Symbol)

    the private singleton method the body compiles to



28
29
30
# File 'lib/briefly/shortcut.rb', line 28

def raw_name
  @raw_name
end

#rescuesArray<Array(Class, Proc)> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns this shortcut's own error handlers, oldest first.

Returns:

  • (Array<Array(Class, Proc)>)

    this shortcut's own error handlers, oldest first



32
33
34
# File 'lib/briefly/shortcut.rb', line 32

def rescues
  @rescues
end

#source_locationArray(String, Integer)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The [file, line] the compiled methods report. Builder#shortcut resolves it — the body's own location, or the shortcut call site for a location-less Proc like &:upcase — and Builder#namespace overwrites it: its child-returning proc literal lives in builder.rb, and a namespace that pointed there would reintroduce, for namespaces, the lie this field exists to remove.

Returns:

  • (Array(String, Integer))


42
43
44
# File 'lib/briefly/shortcut.rb', line 42

def source_location
  @source_location
end

Instance Method Details

#handler_for(error) ⇒ Proc?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns this shortcut's most recently registered handler whose class matches, or nil.

Parameters:

  • error (Exception)

Returns:

  • (Proc, nil)

    this shortcut's most recently registered handler whose class matches, or nil



98
# File 'lib/briefly/shortcut.rb', line 98

def handler_for(error) = @rescues.reverse_each.find { |klass, _| error.is_a?(klass) }&.last

#initialize_copy(other) ⇒ void

This method returns an undefined value.

Builder copies the facade's shortcuts before mutating them, so an aborted pass cannot leave the live facade half-configured. Hash#dup would share these arrays.

Parameters:



63
64
65
66
67
# File 'lib/briefly/shortcut.rb', line 63

def initialize_copy(other)
  super
  @aliases = other.aliases.dup
  @rescues = other.rescues.dup
end

#memoizeself

Memoizes the value: computed once, cached for the process lifetime. A body that takes any argument is refused at build time (in Builder#compile!); the compiled method takes none.

Returns:

  • (self)


73
74
75
76
# File 'lib/briefly/shortcut.rb', line 73

def memoize
  @memoized = true
  self
end

#memoized?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


80
# File 'lib/briefly/shortcut.rb', line 80

def memoized? = @memoized

#namesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns canonical name followed by every alias.

Returns:

  • (Array<Symbol>)

    canonical name followed by every alias



102
# File 'lib/briefly/shortcut.rb', line 102

def names = [@canonical, *@aliases]

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

Registers an error handler scoped to this shortcut. The handler's return value becomes the shortcut's value; it is +call+ed with (error, shortcut_name), never bound to the facade. Later registrations for the same error class win.

Parameters:

  • error_class (Class)

    matched against the raised error and its subclasses

Yields:

  • (error, shortcut_name)

    the recovery block

Returns:

  • (self)


89
90
91
92
93
# File 'lib/briefly/shortcut.rb', line 89

def rescue_from(error_class, &handler)
  Briefly.send(:validate_rescue!, error_class, handler)
  @rescues << [error_class, handler]
  self
end