Class: Iata::Registry
- Inherits:
-
Object
- Object
- Iata::Registry
- 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
#wherefilter keys to the Entry attribute they read.nameis 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
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Class Method Summary collapse
-
.from_entries(entries) ⇒ Registry
Build a registry from an existing list of entries.
-
.load_default ⇒ Registry
Load the bundled dataset shipped inside the gem.
-
.load_file(path) ⇒ Registry
Load a specific JSON file from disk.
Instance Method Summary collapse
-
#countries ⇒ Array<String>
All distinct country codes present in the registry, sorted.
-
#counts_by_country ⇒ Hash{String=>Integer}
Count of entries per country.
- #each ⇒ Object
-
#find(code) ⇒ Iata::Entry?
(also: #[])
Exact-code lookup.
-
#initialize(entries = []) ⇒ Registry
constructor
A new instance of Registry.
-
#where(filters) ⇒ Array<Iata::Entry>
Filter entries by one or more predicates.
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
#entries ⇒ Object (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.
55 56 57 |
# File 'lib/iata/registry.rb', line 55 def from_entries(entries) new(entries) end |
Instance Method Details
#countries ⇒ Array<String>
All distinct country codes present in the registry, sorted.
92 93 94 |
# File 'lib/iata/registry.rb', line 92 def countries entries.map(&:country_iso2).compact.uniq.sort end |
#counts_by_country ⇒ Hash{String=>Integer}
Count of entries per country.
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 |
#each ⇒ Object
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.
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.
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 |