Class: Briefly::Shortcut
- Inherits:
-
Object
- Object
- Briefly::Shortcut
- 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
-
#aliases ⇒ Array<Symbol>
readonly
private
Additional method names delegating to the same body and memo cell.
-
#body ⇒ Proc
readonly
private
The implementation, bound to the facade at call time.
-
#canonical ⇒ Symbol
readonly
private
The primary method name.
-
#raw_name ⇒ Symbol
readonly
private
The private singleton method the body compiles to.
-
#rescues ⇒ Array<Array(Class, Proc)>
readonly
private
This shortcut's own error handlers, oldest first.
-
#source_location ⇒ Array(String, Integer)
private
The
[file, line]the compiled methods report.
Instance Method Summary collapse
-
#handler_for(error) ⇒ Proc?
private
This shortcut's most recently registered handler whose class matches, or
nil. -
#initialize(canonical, aliases, body, source_location) ⇒ Shortcut
constructor
A new instance of Shortcut.
-
#initialize_copy(other) ⇒ void
Builder copies the facade's shortcuts before mutating them, so an aborted pass cannot leave the live facade half-configured.
-
#memoize ⇒ self
Memoizes the value: computed once, cached for the process lifetime.
- #memoized? ⇒ Boolean private
-
#names ⇒ Array<Symbol>
private
Canonical name followed by every alias.
-
#rescue_from(error_class) {|error, shortcut_name| ... } ⇒ self
Registers an error handler scoped to this shortcut.
Constructor Details
#initialize(canonical, aliases, body, source_location) ⇒ Shortcut
Returns a new instance of Shortcut.
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
#aliases ⇒ Array<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.
20 21 22 |
# File 'lib/briefly/shortcut.rb', line 20 def aliases @aliases end |
#body ⇒ 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 the implementation, bound to the facade at call time.
24 25 26 |
# File 'lib/briefly/shortcut.rb', line 24 def body @body end |
#canonical ⇒ 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 the primary method name.
16 17 18 |
# File 'lib/briefly/shortcut.rb', line 16 def canonical @canonical end |
#raw_name ⇒ 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 the private singleton method the body compiles to.
28 29 30 |
# File 'lib/briefly/shortcut.rb', line 28 def raw_name @raw_name end |
#rescues ⇒ Array<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.
32 33 34 |
# File 'lib/briefly/shortcut.rb', line 32 def rescues @rescues end |
#source_location ⇒ Array(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.
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.
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.
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 |
#memoize ⇒ self
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.
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.
80 |
# File 'lib/briefly/shortcut.rb', line 80 def memoized? = @memoized |
#names ⇒ Array<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.
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.
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 |