Module: USA

Defined in:
lib/usa.rb,
lib/usa/engine.rb,
lib/usa/version.rb,
lib/generators/usa/install/install_generator.rb

Overview

The geography of the United States: the states, counties, cities and ZIPs an address names.

Defined Under Namespace

Modules: Generators, Seeded Classes: Engine, Error, Record

Constant Summary collapse

MODELS =

The file each of this gem's models is defined in, which is the name a host must not take.

%w[city city_county county state zip]
VERSION =

The version of this gem, as RubyGems knows it.

'0.5.0'

Class Method Summary collapse

Class Method Details

.seed ⇒ void

This method returns an undefined value.

Writes every row this release holds, keeping the id of every row the database already has. A table the host never created is passed over, so an app that joins to three of the five installs three and seeds them.



31
32
33
34
35
# File 'lib/usa.rb', line 31

def self.seed
  [ State, County, City, CityCounty, ZIP ].select(&:table_exists?).each(&:seed)
  State.recount_counties
  County.recount_zips
end

.table(name) ⇒ Symbol

The name one of this gem's tables goes by in the host, which is what its migrations create.

Returns:

  • (Symbol) —

    prefixed table name.



25
# File 'lib/usa.rb', line 25

def self.table(name) = :"#{table_name_prefix}#{name}"

.verify_models(dirs = Rails.autoloaders.main.dirs) ⇒ void

This method returns an undefined value.

Refuses a host's own class standing where one of this gem's models should be. Zeitwerk gives an app's file precedence over an engine's, silently, so without this a host holding app/models/city.rb would find every association of this gem's pointing at a class of its own. Asked of the files rather than of the constants: loading a model to find out pulls Active Record in with it, long before an app is ready for either.

Parameters:

  • dirs (Array<String>) (defaults to: Rails.autoloaders.main.dirs) —

    the autoloaded directories to look through.

Raises:



47
48
49
50
51
52
53
54
55
56
# File 'lib/usa.rb', line 47

def self.verify_models(dirs = Rails.autoloaders.main.dirs)
  mine = Engine.root.join('app/models').to_s
  taken = dirs.reject { |dir| dir == mine }.
    flat_map { |dir| MODELS.select { |model| File.exist? File.join(dir, "#{model}.rb") } }
  return if taken.empty?

  raise Error, "The usa gem defines #{taken.uniq.map(&:camelize).to_sentence}, and a file " \
    'of your own takes the name first. Rename yours: Rails gives an app’s file precedence ' \
    'over an engine’s, silently, and the models this gem ships would point at your class.'
end