Class: TestIds::Allocator
- Inherits:
-
Object
- Object
- TestIds::Allocator
- Defined in:
- lib/test_ids/allocator.rb
Overview
The allocator is responsible for assigning new numbers and keeping a record of existing assignments.
There is one allocator instance per configuration, and each has its own database file.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#allocate(instance, options) ⇒ Object
Main method to inject generated bin and test numbers, the given options instance is modified accordingly.
-
#file ⇒ Object
Returns a path to the file that will be used to store the allocated bins/numbers, returns nil if remote storage not enabled.
- #id ⇒ Object
-
#initialize(configuration) ⇒ Allocator
constructor
A new instance of Allocator.
-
#save ⇒ Object
Saves the current allocator state to the repository.
- #store ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Allocator
Returns a new instance of Allocator.
10 11 12 |
# File 'lib/test_ids/allocator.rb', line 10 def initialize(configuration) @config = configuration end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/test_ids/allocator.rb', line 8 def config @config end |
Instance Method Details
#allocate(instance, options) ⇒ Object
Main method to inject generated bin and test numbers, the given options instance is modified accordingly
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/test_ids/allocator.rb', line 16 def allocate(instance, ) clean() @callbacks = [] name = extract_test_name(instance, ) name = "#{name}_#{[:index]}" if [:index] store['tests'][name] ||= {} t = store['tests'][name] # If the user has supplied any of these, that number should be used # and reserved so that it is not automatically generated later if [:bin] t['bin'] = [:bin] store['manually_assigned']['bin'][[:bin].to_s] = true # Regenerate the bin if the original allocation has since been applied # manually elsewhere elsif store['manually_assigned']['bin'][t['bin'].to_s] t['bin'] = nil # Also regenerate these as they could be a function of the bin t['softbin'] = nil if config.softbins.function? t['number'] = nil if config.numbers.function? end if [:softbin] t['softbin'] = [:softbin] store['manually_assigned']['softbin'][[:softbin].to_s] = true elsif store['manually_assigned']['softbin'][t['softbin'].to_s] t['softbin'] = nil # Also regenerate the number as it could be a function of the softbin t['number'] = nil if config.numbers.function? end if [:number] t['number'] = [:number] store['manually_assigned']['number'][[:number].to_s] = true elsif store['manually_assigned']['number'][t['number'].to_s] t['number'] = nil end # Otherwise generate the missing ones t['bin'] ||= allocate_bin t['softbin'] ||= allocate_softbin(t['bin']) t['number'] ||= allocate_number(t['bin'], t['softbin']) # Record that there has been a reference to the final numbers time = Time.now.to_f store['references']['bin'][t['bin'].to_s] = time if t['bin'] store['references']['softbin'][t['softbin'].to_s] = time if t['softbin'] store['references']['number'][t['number'].to_s] = time if t['number'] # Update the supplied options hash that will be forwarded to the # program generator [:bin] = t['bin'] [:softbin] = t['softbin'] [:number] = t['number'] end |
#file ⇒ Object
Returns a path to the file that will be used to store the allocated bins/numbers, returns nil if remote storage not enabled
96 97 98 |
# File 'lib/test_ids/allocator.rb', line 96 def file TestIds.database_file(id) end |
#id ⇒ Object
100 101 102 |
# File 'lib/test_ids/allocator.rb', line 100 def id config.id end |
#save ⇒ Object
Saves the current allocator state to the repository
86 87 88 89 90 91 92 |
# File 'lib/test_ids/allocator.rb', line 86 def save if file p = Pathname.new(file) FileUtils.mkdir_p(p.dirname) File.open(p, 'w') { |f| f.puts JSON.pretty_generate(store) } end end |
#store ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/test_ids/allocator.rb', line 67 def store @store ||= begin s = JSON.load(File.read(file)) if file && File.exist?(file) if s @last_bin = s['pointers']['bin'] @last_softbin = s['pointers']['softbin'] @last_number = s['pointers']['number'] s else { 'tests' => {}, 'manually_assigned' => { 'bin' => {}, 'softbin' => {}, 'number' => {} }, 'pointers' => { 'bin' => nil, 'softbin' => nil, 'number' => nil }, 'references' => { 'bin' => {}, 'softbin' => {}, 'number' => {} } } end end end |