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.
5 6 7 8 9 10 11 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 5 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
52 53 54 55 56 57 58 59 60 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 52 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
46 47 48 49 50 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 46 def []=(key, value) @flattened = nil @dirty = true local[key] = value end |
#clear ⇒ Object
39 40 41 42 43 44 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 39 def clear local.clear @flattened = nil @dirty = true self end |
#dirty? ⇒ Boolean
75 76 77 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 75 def dirty? @dirty end |
#flatten ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 84 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
79 80 81 82 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 79 def is_a?(arg) return true if Hash <= arg super end |
#local ⇒ Object
26 27 28 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 26 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
16 17 18 19 20 21 22 23 24 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 16 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
30 31 32 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 30 def own resolve_hash(@bid_stack[-1]) || {} end |
#reload! ⇒ Object
62 63 64 65 66 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 62 def reload! @dirty = false @hash_map = {} self end |
#save!(force: false) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 68 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
34 35 36 37 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 34 def set_local(new_hash) @dirty = true local.clear.merge!(new_hash) end |
#to_h ⇒ Object
97 98 99 |
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 97 def to_h flatten end |