Class: Feiku::Unit
- Inherits:
-
Object
- Object
- Feiku::Unit
- Defined in:
- lib/feiku/unit.rb
Overview
Basic unit for generating fake data
Constant Summary collapse
- RANDOM_STRING =
[*"a".."z", *"A".."Z"].freeze
Instance Method Summary collapse
- #generate(max_attempt: 1000, &block) ⇒ Object
-
#initialize(data_type: nil, length: nil) ⇒ Unit
constructor
A new instance of Unit.
- #try_until_success(length, max_attempt, &block) ⇒ Object
- #unique ⇒ Object
Constructor Details
#initialize(data_type: nil, length: nil) ⇒ Unit
Returns a new instance of Unit.
10 11 12 13 14 15 |
# File 'lib/feiku/unit.rb', line 10 def initialize(data_type: nil, length: nil) @data_type = data_type @length = length @unique = false @uniqueness_pool = [] end |
Instance Method Details
#generate(max_attempt: 1000, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/feiku/unit.rb', line 17 def generate(max_attempt: 1000, &block) length = @length.is_a?(Integer) ? @length : @length.to_a.sample generated = _generate(length) if check_success_with(generated, block) && check_uniqueness(generated) @uniqueness_pool << generated return generated end try_until_success(length, max_attempt, &block) end |
#try_until_success(length, max_attempt, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/feiku/unit.rb', line 28 def try_until_success(length, max_attempt, &block) attempt_count = 0 while attempt_count <= max_attempt item = _generate(length) if check_success_with(item, block) && check_uniqueness(item) @uniqueness_pool << item return item end attempt_count += 1 end raise Feiku::Error, "Cannot generate correct data within given attempt times: #{max_attempt}" end |
#unique ⇒ Object
42 43 44 45 |
# File 'lib/feiku/unit.rb', line 42 def unique @unique = true self end |