Class: Specwrk::PendingStore

Inherits:
Store
  • Object
show all
Defined in:
lib/specwrk/store/pending_store.rb

Constant Summary collapse

RUN_TIME_BUCKET_MAXIMUM_KEY =
:____run_time_bucket_maximum
MAX_RETRIES_KEY =
:____max_retries
BUCKET_IDS_KEY =
:____bucket_ids

Constants inherited from Store

Store::LockUnavailableError

Instance Method Summary collapse

Methods inherited from Store

#[], #[]=, adapter_klass, #any?, #delete, #empty?, #initialize, #inspect, #multi_read, #to_h, with_lock

Constructor Details

This class inherits a constructor from Specwrk::Store

Instance Method Details

#bucket_idsObject



51
52
53
# File 'lib/specwrk/store/pending_store.rb', line 51

def bucket_ids
  @bucket_ids ||= self[BUCKET_IDS_KEY] || []
end

#bucket_ids=(val) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/specwrk/store/pending_store.rb', line 41

def bucket_ids=(val)
  @bucket_ids = nil

  self[BUCKET_IDS_KEY] = if val.nil? || val.length.zero?
    nil
  else
    val
  end
end

#bucket_store_for(bucket_id) ⇒ Object



94
95
96
# File 'lib/specwrk/store/pending_store.rb', line 94

def bucket_store_for(bucket_id)
  BucketStore.new(uri.to_s, File.join(scope, "buckets", bucket_id))
end

#clearObject



65
66
67
68
69
70
# File 'lib/specwrk/store/pending_store.rb', line 65

def clear
  bucket_ids.each { |bucket_id| delete_bucket(bucket_id) }
  @bucket_ids = nil

  super
end

#delete_bucket(bucket_id) ⇒ Object



98
99
100
# File 'lib/specwrk/store/pending_store.rb', line 98

def delete_bucket(bucket_id)
  bucket_store_for(bucket_id).clear
end

#keysObject



102
103
104
# File 'lib/specwrk/store/pending_store.rb', line 102

def keys
  bucket_ids
end

#lengthObject



106
107
108
# File 'lib/specwrk/store/pending_store.rb', line 106

def length
  bucket_ids.length
end

#max_retriesObject



37
38
39
# File 'lib/specwrk/store/pending_store.rb', line 37

def max_retries
  @max_retries ||= self[MAX_RETRIES_KEY] || 0
end

#max_retries=(val) ⇒ Object



33
34
35
# File 'lib/specwrk/store/pending_store.rb', line 33

def max_retries=(val)
  @max_retries = self[MAX_RETRIES_KEY] = val
end

#merge!(hash) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/specwrk/store/pending_store.rb', line 55

def merge!(hash)
  return self if hash.nil? || hash.empty?

  buckets = grouped_examples(hash.values)
  new_bucket_ids = buckets.map { |examples| write_bucket(examples) }

  self.bucket_ids = bucket_ids + new_bucket_ids
  self
end

#push_examples(examples) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/specwrk/store/pending_store.rb', line 86

def push_examples(examples)
  return self if examples.nil? || examples.empty?

  new_bucket_id = write_bucket(examples)
  self.bucket_ids = bucket_ids + [new_bucket_id]
  self
end

#reloadObject



72
73
74
75
76
# File 'lib/specwrk/store/pending_store.rb', line 72

def reload
  @max_retries = nil
  @bucket_ids = nil
  super
end

#run_time_bucket_maximumObject



18
19
20
# File 'lib/specwrk/store/pending_store.rb', line 18

def run_time_bucket_maximum
  @run_time_bucket_maximum ||= self[RUN_TIME_BUCKET_MAXIMUM_KEY]
end

#run_time_bucket_maximum=(val) ⇒ Object



14
15
16
# File 'lib/specwrk/store/pending_store.rb', line 14

def run_time_bucket_maximum=(val)
  @run_time_bucket_maximum = self[RUN_TIME_BUCKET_MAXIMUM_KEY] = val
end

#shift_bucketObject



78
79
80
81
82
83
84
# File 'lib/specwrk/store/pending_store.rb', line 78

def shift_bucket
  return nil if bucket_ids.empty?

  bucket_id = bucket_ids.first
  self.bucket_ids = bucket_ids.drop(1)
  bucket_id
end

#update_run_time_bucket_maximum(calculated_run_time_bucket_maximum, target_bucket_timing_duration: 0) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/specwrk/store/pending_store.rb', line 22

def update_run_time_bucket_maximum(calculated_run_time_bucket_maximum, target_bucket_timing_duration: 0)
  target_bucket_timing_duration = target_bucket_timing_duration.to_f

  if target_bucket_timing_duration.positive?
    self.run_time_bucket_maximum = target_bucket_timing_duration
  else
    run_time_bucket_maximums = [run_time_bucket_maximum, calculated_run_time_bucket_maximum.to_f].compact
    self.run_time_bucket_maximum = run_time_bucket_maximums.sum.to_f / run_time_bucket_maximums.length.to_f
  end
end