Class: CanvasSync::JobBatches::ContextHash

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_sync/job_batches/context_hash.rb

Instance Method Summary collapse

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



49
50
51
52
53
54
55
56
57
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 49

def [](key)
  bid = @bid_stack[-1]
  while bid.present?
    bhash = reolve_hash(bid)
    return bhash[key] if bhash&.key?(key)
    bid = get_parent_bid(bid)
  end
  nil
end

#[]=(key, value) ⇒ Object



43
44
45
46
47
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 43

def []=(key, value)
  @flattened = nil
  @dirty = true
  local[key] = value
end

#clearObject



36
37
38
39
40
41
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 36

def clear
  local.clear
  @flattened = nil
  @dirty = true
  self
end

#dirty?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 72

def dirty?
  @dirty
end

#flattenObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 81

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

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 76

def is_a?(arg)
  return true if Hash <= arg
  super
end

#localObject



27
28
29
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 27

def local
  @hash_map[local_bid]
end

#local_bidObject

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 = reolve_hash(bid)
    return bid if bhash
    bid = get_parent_bid(bid)
  end
  nil
end

#reload!Object



59
60
61
62
63
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 59

def reload!
  @dirty = false
  @hash_map = {}
  self
end

#save!(force: false) ⇒ Object



65
66
67
68
69
70
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 65

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



31
32
33
34
# File 'lib/canvas_sync/job_batches/context_hash.rb', line 31

def set_local(new_hash)
  @dirty = true
  local.clear.merge!(new_hash)
end