Class: Dagger::Default
- Inherits:
-
Object
- Object
- Dagger::Default
- Defined in:
- lib/dagger/default.rb
Overview
Default value generator for a dictionary
Instance Attribute Summary collapse
-
#cached ⇒ Object
writeonly
Sets the attribute cached.
-
#default_proc ⇒ Object
readonly
Returns the value of attribute default_proc.
Class Method Summary collapse
Instance Method Summary collapse
-
#cached? ⇒ Boolean
Verify state of caching of future values.
-
#generate(tree, key) ⇒ Object
Generate a default value for a
key
, possibly caching the result in thetree
. -
#initialize(vertex, cached: false, fallback: nil, rule_prefix: '_default') ⇒ Default
constructor
Initialize a default value generator for a
vertex
.
Constructor Details
#initialize(vertex, cached: false, fallback: nil, rule_prefix: '_default') ⇒ Default
Initialize a default value generator for a vertex
:call-seq:
new(graph, vertex) => Dagger::Default
new(*, cached: false)
new(*, rule_prefix: '_default')
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dagger/default.rb', line 23 def initialize(vertex, cached: false, fallback: nil, rule_prefix: '_default') @vertex = vertex @cached = cached @fallback = fallback @rule_prefix = rule_prefix.to_key_path @default_proc = ->(tree, key) { generate(tree, key) } @locks = Set[] end |
Instance Attribute Details
#cached=(value) ⇒ Object (writeonly)
Sets the attribute cached
44 45 46 |
# File 'lib/dagger/default.rb', line 44 def cached=(value) @cached = value end |
#default_proc ⇒ Object (readonly)
Returns the value of attribute default_proc.
35 36 37 |
# File 'lib/dagger/default.rb', line 35 def default_proc @default_proc end |
Class Method Details
.proc(*args) ⇒ Object
13 14 15 |
# File 'lib/dagger/default.rb', line 13 def self.proc(*args) new(*args).default_proc end |
Instance Method Details
#cached? ⇒ Boolean
Verify state of caching of future values
:call-seq:
cached? => bool
41 42 43 |
# File 'lib/dagger/default.rb', line 41 def cached? !(!@cached) end |
#generate(tree, key) ⇒ Object
Generate a default value for a key
, possibly caching the result in the tree
.
:call-seq:
generate(tree, key) => value || KeyError
Raises a KeyError
if the default value cannot be generated. Raises a RuntimeError
in case of a deadlock for key
.
54 55 56 57 |
# File 'lib/dagger/default.rb', line 54 def generate(tree, key) key = key.to_key_path with_locked_key(key) { |locked_key| cached_value(tree, locked_key) } end |