Class: CanvasSync::JobBatches::ContextHash
- Inherits:
-
Object
- Object
- CanvasSync::JobBatches::ContextHash
- Defined in:
- lib/canvas_sync/job_batches/context_hash.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #dirty? ⇒ Boolean
- #flatten ⇒ Object
-
#initialize(bid, hash = nil) ⇒ ContextHash
constructor
A new instance of ContextHash.
- #is_a?(arg) ⇒ Boolean
- #local ⇒ Object
-
#local_bid ⇒ Object
Local is “the nearest batch with a context value” This allows for, for example, SerialBatchJob to have a modifiable context stored on it’s main Batch that can be accessed transparently from one of it’s internal, context-less Batches.
- #own ⇒ Object
- #reload! ⇒ Object
- #save!(force: false) ⇒ Object
- #set_local(new_hash) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(bid, hash = nil) ⇒ ContextHash
Returns a new instance of ContextHash.
6 7 8 9 10 11 12 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 6 def initialize(bid, hash = nil) @bid_stack = [bid] @hash_map = {} @dirty = false @flattened = nil @hash_map[bid] = hash.with_indifferent_access if hash end |
Instance Method Details
#[](key) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 53 def [](key) bid = @bid_stack[-1] while bid.present? bhash = resolve_hash(bid) return bhash[key] if bhash&.key?(key) bid = get_parent_bid(bid) end nil end |
#[]=(key, value) ⇒ Object
47 48 49 50 51 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 47 def []=(key, value) @flattened = nil @dirty = true local[key] = value end |
#clear ⇒ Object
40 41 42 43 44 45 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 40 def clear local.clear @flattened = nil @dirty = true self end |
#dirty? ⇒ Boolean
76 77 78 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 76 def dirty? @dirty end |
#flatten ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 85 def flatten return @flattened if @flattened load_all flattened = {} @bid_stack.compact.each do |bid| flattened.merge!(@hash_map[bid]) if @hash_map[bid] end flattened.freeze @flattened = flattened.with_indifferent_access end |
#is_a?(arg) ⇒ Boolean
80 81 82 83 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 80 def is_a?(arg) return true if Hash <= arg super end |
#local ⇒ Object
27 28 29 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 27 def local @hash_map[local_bid] end |
#local_bid ⇒ Object
Local is “the nearest batch with a context value” This allows for, for example, SerialBatchJob to have a modifiable context stored on it’s main Batch that can be accessed transparently from one of it’s internal, context-less Batches
17 18 19 20 21 22 23 24 25 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 17 def local_bid bid = @bid_stack[-1] while bid.present? bhash = resolve_hash(bid) return bid if bhash bid = get_parent_bid(bid) end nil end |
#own ⇒ Object
31 32 33 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 31 def own resolve_hash(@bid_stack[-1]) || {} end |
#reload! ⇒ Object
63 64 65 66 67 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 63 def reload! @dirty = false @hash_map = {} self end |
#save!(force: false) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 69 def save!(force: false) return unless dirty? || force Batch.redis do |r| r.hset("BID-#{local_bid}", 'context', JSON.unparse(local)) end end |
#set_local(new_hash) ⇒ Object
35 36 37 38 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 35 def set_local(new_hash) @dirty = true local.clear.merge!(new_hash) end |
#to_h ⇒ Object
98 99 100 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 98 def to_h flatten end |