Class: Iata::Registry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/iata/registry.rb

Overview

In-memory, lazily-indexed registry over a set of Entry instances.

The default registry is loaded from the vendored dataset bundled with the gem (see Registry.load_default). Callers can also construct a registry from any other source via Registry.from_entries or Registry.load_file.

Constant Summary collapse

SCALAR_FILTERS =

Map of #where filter keys to the Entry attribute they read. name is intentionally NOT in this map — it gets routed through filter_name so Regexp / substring matching works (see apply_filter).

{
  code: :code,
  country: :country_iso2,
  country_iso2: :country_iso2,
  wikidata_id: :wikidata_id
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries = []) ⇒ Registry

Returns a new instance of Registry.



30
31
32
# File 'lib/iata/registry.rb', line 30

def initialize(entries = [])
  @entries = entries.freeze
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



16
17
18
# File 'lib/iata/registry.rb', line 16

def entries
  @entries
end

Class Method Details

.from_entries(entries) ⇒ Registry

Build a registry from an existing list of entries.

Parameters:

Returns:



55
56
57
# File 'lib/iata/registry.rb', line 55

def from_entries(entries)
  new(entries)
end

.load_defaultRegistry

Load the bundled dataset shipped inside the gem.

Returns:



41
42
43
# File 'lib/iata/registry.rb', line 41

def load_default
  from_entries(Loader.load_file(default_data_path))
end

.load_file(path) ⇒ Registry

Load a specific JSON file from disk.

Parameters:

  • path (String)

Returns:



48
49
50
# File 'lib/iata/registry.rb', line 48

def load_file(path)
  from_entries(Loader.load_file(path))
end

Instance Method Details

#countriesArray<String>

All distinct country codes present in the registry, sorted.

Returns:

  • (Array<String>)


92
93
94
# File 'lib/iata/registry.rb', line 92

def countries
  entries.map(&:country_iso2).compact.uniq.sort
end

#counts_by_countryHash{String=>Integer}

Count of entries per country.

Returns:

  • (Hash{String=>Integer})


98
99
100
# File 'lib/iata/registry.rb', line 98

def counts_by_country
  entries.each_with_object(Hash.new(0)) { |e, h| h[e.country_iso2] += 1 if e.country_iso2 }
end

#eachObject



34
35
36
# File 'lib/iata/registry.rb', line 34

def each(&)
  @entries.each(&)
end

#find(code) ⇒ Iata::Entry? Also known as: []

Exact-code lookup.

Parameters:

  • code (String)

    3-letter IATA code (case-insensitive)

Returns:



69
70
71
72
73
# File 'lib/iata/registry.rb', line 69

def find(code)
  return nil if code.nil?

  by_code[code.to_s.upcase]
end

#where(filters) ⇒ Array<Iata::Entry>

Filter entries by one or more predicates. Scalar filters accept either a single value or an array (any-of). name accepts a String (case-insensitive equality) or a Regexp.

Examples:

registry.where(country: 'CN')
registry.where(country: %w[CN HK], name: /international/i)

Returns:



86
87
88
# File 'lib/iata/registry.rb', line 86

def where(filters)
  filters.reduce(entries) { |scope, (key, value)| apply_filter(scope, key, value) }
end