Class: TwitterCldr::Shared::PostalCodeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/shared/postal_code_generator.rb

Constant Summary collapse

SAMPLE_MULTIPLIER =
4

Instance Method Summary collapse

Constructor Details

#initialize(regexp_ast) ⇒ PostalCodeGenerator

Returns a new instance of PostalCodeGenerator.



14
15
16
# File 'lib/twitter_cldr/shared/postal_code_generator.rb', line 14

def initialize(regexp_ast)
  @regexp_generator = TwitterCldr::Utils::RegexpSampler.new(regexp_ast)
end

Instance Method Details

#generateObject



18
19
20
# File 'lib/twitter_cldr/shared/postal_code_generator.rb', line 18

def generate
  clean_result(@regexp_generator.generate)
end

#sample(sample_size = 1) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/twitter_cldr/shared/postal_code_generator.rb', line 22

def sample(sample_size = 1)
  sample_set = Set.new
  counter = 1

  until sample_set.size == sample_size
    sample = generate
    sample_set << sample unless sample.empty?
    counter += 1

    # Stop if the number of attempted generations is
    # n times more than requested. Some territories only
    # have one postal code, so if the user asks for 10
    # they'll get an infinite loop.
    break if counter > sample_size * SAMPLE_MULTIPLIER
  end

  sample_set.to_a
end