Class: StrictLazy::Loader
- Inherits:
-
Object
- Object
- StrictLazy::Loader
- Defined in:
- lib/strict_lazy/loader.rb
Overview
Immutable definition produced by a lazy_load declaration.
It holds the reader name, the resolver (a from: method symbol or a block), whether resolution is eager (sync:), and the default used for records the resolver does not fulfill.
Resolution always goes through #resolve, which is handed the model class so a from: symbol can be dispatched as a class method. A block resolver is instance_exec‘d on the model class for the same lookup semantics.
Instance Attribute Summary collapse
-
#batch_ivar ⇒ Object
readonly
Returns the value of attribute batch_ivar.
-
#default ⇒ Object
readonly
Internal ivar names are reserved per reader: @lazy<reader> holds the resolved value, @batch<reader> holds the shared Batch reference.
-
#reader ⇒ Object
readonly
Internal ivar names are reserved per reader: @lazy<reader> holds the resolved value, @batch<reader> holds the shared Batch reference.
-
#sync ⇒ Object
readonly
Internal ivar names are reserved per reader: @lazy<reader> holds the resolved value, @batch<reader> holds the shared Batch reference.
-
#value_ivar ⇒ Object
readonly
Returns the value of attribute value_ivar.
Instance Method Summary collapse
-
#default_for(record) ⇒ Object
Compute the default for a record the resolver did not fulfill.
-
#initialize(reader:, sync:, default:, from: nil, block: nil) ⇒ Loader
constructor
A new instance of Loader.
-
#resolve(model, records, loader) ⇒ Object
Invoke the resolver once over
records. - #sync? ⇒ Boolean
Constructor Details
#initialize(reader:, sync:, default:, from: nil, block: nil) ⇒ Loader
Returns a new instance of Loader.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/strict_lazy/loader.rb', line 18 def initialize(reader:, sync:, default:, from: nil, block: nil) @reader = reader @sync = sync @default = default @from = from @block = block # The ivar name may not contain `?`, so a predicate reader is encoded # (not stripped): +commented?+ and +commented+ get distinct ivars. (A bare # reader literally named +commented_pred+ would collide, but reader names # are validated to a bare-name/`?` form, making that pairing a non-idiom.) ivar_key = reader.to_s.sub(/\?\z/, "_pred") @value_ivar = :"@_lazy_#{ivar_key}" @batch_ivar = :"@_batch_#{ivar_key}" end |
Instance Attribute Details
#batch_ivar ⇒ Object (readonly)
Returns the value of attribute batch_ivar.
35 36 37 |
# File 'lib/strict_lazy/loader.rb', line 35 def batch_ivar @batch_ivar end |
#default ⇒ Object (readonly)
Internal ivar names are reserved per reader: @lazy<reader> holds the resolved value, @batch<reader> holds the shared Batch reference.
16 17 18 |
# File 'lib/strict_lazy/loader.rb', line 16 def default @default end |
#reader ⇒ Object (readonly)
Internal ivar names are reserved per reader: @lazy<reader> holds the resolved value, @batch<reader> holds the shared Batch reference.
16 17 18 |
# File 'lib/strict_lazy/loader.rb', line 16 def reader @reader end |
#sync ⇒ Object (readonly)
Internal ivar names are reserved per reader: @lazy<reader> holds the resolved value, @batch<reader> holds the shared Batch reference.
16 17 18 |
# File 'lib/strict_lazy/loader.rb', line 16 def sync @sync end |
#value_ivar ⇒ Object (readonly)
Returns the value of attribute value_ivar.
35 36 37 |
# File 'lib/strict_lazy/loader.rb', line 35 def value_ivar @value_ivar end |
Instance Method Details
#default_for(record) ⇒ Object
Compute the default for a record the resolver did not fulfill. A callable default acts as a per-record factory so mutable values ([], {}) are never shared; arity 1 receives the record.
50 51 52 53 54 |
# File 'lib/strict_lazy/loader.rb', line 50 def default_for(record) return @default unless @default.respond_to?(:call) @default.arity.zero? ? @default.call : @default.call(record) end |
#resolve(model, records, loader) ⇒ Object
Invoke the resolver once over records. loader is the loader.call(record, value) callable supplied by the Batch.
39 40 41 42 43 44 45 |
# File 'lib/strict_lazy/loader.rb', line 39 def resolve(model, records, loader) if @from model.public_send(@from, records, loader) else model.instance_exec(records, loader, &@block) end end |
#sync? ⇒ Boolean
33 |
# File 'lib/strict_lazy/loader.rb', line 33 def sync? = @sync |