Class: Synthra::Types::AddressLocation::PostalCode

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/address_location/locations.rb

Overview

PostalCode type

Instance Method Summary collapse

Instance Method Details

#generate_country_format(country_code, rng) ⇒ Object (private)

Generate postal code in country-specific format



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/synthra/types/address_location/locations.rb', line 266

def generate_country_format(country_code, rng)
  case country_code
  when "US"
    "#{rng.int(10000, 99999)}"
  when "DE"
    "#{rng.int(10000, 99999)}"
  when "JP"
    "#{rng.int(100, 999)}-#{rng.int(1000, 9999)}"
  when "AU"
    "#{rng.int(1000, 9999)}"
  when "GB", "UK"
    rng.sample(["SW1A 0AA", "M1 1AA", "B33 8TH", "W1A 0AX"])
  when "CA"
    "#{rng.sample(['A', 'B', 'C', 'E', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'V', 'X', 'Y'])}#{rng.int(0, 9)}#{rng.sample(['A', 'B', 'C', 'E', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'])} #{rng.int(0, 9)}#{rng.sample(['A', 'B', 'C', 'E', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'])}#{rng.int(0, 9)}"
  else
    "#{rng.int(10000, 99999)}"
  end
end

#generate_edge(rng, context, args) ⇒ Object



223
224
225
# File 'lib/synthra/types/address_location/locations.rb', line 223

def generate_edge(rng, context, args)
  ["", "00000", "99999", "A1A 1A1"].sample(random: rng.instance_variable_get(:@random))
end

#generate_for_country(country, rng, adapter) ⇒ Object (private)

Generates a postal code for a specific country, handling Faker locales.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/synthra/types/address_location/locations.rb', line 234

def generate_for_country(country, rng, adapter)
  original_locale = Faker::Config.locale
  begin
    Faker::Config.locale = country.to_sym
    # :nocov:
    # The :us then-arm is unreachable to completion in the bundled Faker
    # version: :us is not a valid I18n locale for Faker::Address, so both
    # zip_code calls raise I18n::InvalidLocale before returning, and the
    # rescue below references an undefined constant. (latent-bug, do not
    # "fix" here)
    if Faker::Config.locale == :us
      adapter&.zip_code || Faker::Address.zip_code
    # :nocov:
    else
      adapter&.postcode || Faker::Address.postcode
    end
  # :nocov:
  # Unreachable in the bundled Faker version: an invalid locale raises
  # I18n::InvalidLocale (not Faker::UnresolvedLocaleError, which is not
  # even defined here), so this rescue clause itself would raise NameError
  # and never bind. See coverage-spec note. (latent-bug, do not "fix" here)
  rescue Faker::UnresolvedLocaleError
    # Fallback to default locale if specific locale is not found
    Faker::Config.locale = original_locale
    adapter&.zip_code || Faker::Address.zip_code
  # :nocov:
  ensure
    Faker::Config.locale = original_locale
  end
end

#generate_invalid(rng, context, args) ⇒ Object



227
228
229
# File 'lib/synthra/types/address_location/locations.rb', line 227

def generate_invalid(rng, context, args)
  ["invalid", nil, 123, "ABCDE"].sample(random: rng.instance_variable_get(:@random))
end

#generate_random(rng, context, args) ⇒ String

Generate a random postal code

Uses Faker::Address.zip_code or postcode for realistic values. Falls back to manual generation if needed.

Examples:

generate_random(rng, { country: "US" })
# => "90210"

Parameters:

  • rng (Generator::RNG)

    random number generator

  • args (Hash)

    type arguments

Options Hash (args):

  • :country (String)

    country code for format hint (US, GB, CA, etc.)

Returns:

  • (String)

    postal code



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/synthra/types/address_location/locations.rb', line 189

def generate_random(rng, context, args)
  country = args[:country]
  adapter = faker_adapter(context)

  # Use Faker with locale switching if country specified
  if country
    result = generate_for_country(country, rng, adapter)
    return result if result && !result.empty?
  end
  
  # Fallback to country-specific format if Faker didn't work
  if country
    return generate_country_format(country.to_s.upcase, rng)
  end
  
  # Default Faker locale
  if adapter
    adapter.zip_code

  else
    Faker::Address.zip_code
  end

rescue StandardError
  # Fallback to a generic format
  country = args[:country]
  if country
    generate_country_format(country.to_s.upcase, rng)
  else
    rng.sample(["12345", "90210", "SW1A 0AA", "M5V 2L9"])
  end

end