Class: InitialTestData::SequenceEnumerator
- Inherits:
-
Object
- Object
- InitialTestData::SequenceEnumerator
- Defined in:
- lib/initial-test-data/sequence_enumerator.rb
Class Method Summary collapse
- .clear ⇒ Object
- .enumerators ⇒ Object
- .initial_value_for(name, index) ⇒ Object
- .register(name, enumerator) ⇒ Object
- .reset ⇒ Object
- .save ⇒ Object
Instance Method Summary collapse
-
#initialize(name) ⇒ SequenceEnumerator
constructor
A new instance of SequenceEnumerator.
- #next ⇒ Object
- #peek ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize(name) ⇒ SequenceEnumerator
Returns a new instance of SequenceEnumerator.
3 4 5 6 7 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 3 def initialize(name) @name = name @index = self.class.register(name, self) @value = self.class.initial_value_for(name, @index) end |
Class Method Details
.clear ⇒ Object
45 46 47 48 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 45 def clear @enumerators.clear @cached_values = nil end |
.enumerators ⇒ Object
22 23 24 25 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 22 def enumerators @enumerators ||= {} @enumerators end |
.initial_value_for(name, index) ⇒ Object
33 34 35 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 33 def initial_value_for(name, index) cached_values_for(name)[index] || 1 end |
.register(name, enumerator) ⇒ Object
27 28 29 30 31 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 27 def register(name, enumerator) enumerators[name.to_s] ||= [] enumerators[name.to_s] << enumerator enumerators[name.to_s].size - 1 end |
.reset ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 37 def reset enumerators.each_value do |enums| enums.each do |enum| enum.reset end end end |
.save ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 50 def save hash = {} enumerators.each do |name, enums| hash[name] = enums.map(&:peek) end File.open(data_path, 'w') do |f| f.write hash.to_yaml end end |
Instance Method Details
#next ⇒ Object
13 14 15 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 13 def next @value = @value.next end |
#peek ⇒ Object
9 10 11 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 9 def peek @value end |
#reset ⇒ Object
17 18 19 |
# File 'lib/initial-test-data/sequence_enumerator.rb', line 17 def reset @value = 1 end |