Class: AtlasEngine::CountryProfile

Inherits:
FrozenRecord::Base
  • Object
show all
Extended by:
T::Sig
Defined in:
app/models/atlas_engine/country_profile.rb

Defined Under Namespace

Modules: Backend Classes: CountryNotFoundError

Constant Summary collapse

DEFAULT_PROFILE =
"DEFAULT"
COUNTRIES =
T.let(
  Worldwide::Regions.all.select(&:country?).reject(&:deprecated?).map(&:iso_code).to_set,
  T::Set[String],
)
@@default_paths =

rubocop:disable Style/ClassVars

T.let(
  [
    File.join(AtlasEngine::Engine.root, "db/data/country_profiles/default.yml"),
  ],
  T::Array[String],
)
@@country_paths =
T.let(
  [
    File.join(AtlasEngine::Engine.root, "app/countries/atlas_engine/*/country_profile.yml"),
  ],
  T::Array[String],
)
@@locale_paths =
T.let(
  [
    File.join(AtlasEngine::Engine.root, "app/countries/atlas_engine/*/locales/*/country_profile.yml"),
  ],
  T::Array[String],
)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_country_paths(paths) ⇒ Object



147
148
149
# File 'app/models/atlas_engine/country_profile.rb', line 147

def add_country_paths(paths)
  T.unsafe(@@country_paths).append(*Array(paths))
end

.add_default_paths(paths) ⇒ Object



142
143
144
# File 'app/models/atlas_engine/country_profile.rb', line 142

def add_default_paths(paths)
  T.unsafe(@@default_paths).append(*Array(paths))
end

.add_locale_paths(paths) ⇒ Object



152
153
154
# File 'app/models/atlas_engine/country_profile.rb', line 152

def add_locale_paths(paths)
  T.unsafe(@@locale_paths).append(*Array(paths))
end

.assign_defaults!(record) ⇒ Object



223
224
225
# File 'app/models/atlas_engine/country_profile.rb', line 223

def assign_defaults!(record)
  default_attributes.deep_merge(record)
end

.country_pathsObject



122
123
124
# File 'app/models/atlas_engine/country_profile.rb', line 122

def country_paths
  @@country_paths
end

.country_paths=(paths) ⇒ Object



127
128
129
# File 'app/models/atlas_engine/country_profile.rb', line 127

def country_paths=(paths)
  @@country_paths = paths
end

.default_attributesObject



213
214
215
216
217
218
219
220
# File 'app/models/atlas_engine/country_profile.rb', line 213

def default_attributes
  @default_attributes ||= T.let(
    default_paths.each_with_object({}) do |path, hash|
      hash.deep_merge!(YAML.load_file(path))
    end,
    T.nilable(T::Hash[String, T.untyped]),
  )
end

.default_pathsObject



112
113
114
# File 'app/models/atlas_engine/country_profile.rb', line 112

def default_paths
  @@default_paths
end

.default_paths=(paths) ⇒ Object



117
118
119
# File 'app/models/atlas_engine/country_profile.rb', line 117

def default_paths=(paths)
  @@default_paths = paths
end

.for(country_code, locale = nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/atlas_engine/country_profile.rb', line 188

def for(country_code, locale = nil)
  raise CountryNotFoundError if country_code.blank?

  unless country_code == DEFAULT_PROFILE || COUNTRIES.include?(country_code.upcase)
    raise CountryNotFoundError
  end

  ids = [country_code.upcase]
  ids.push("#{country_code.upcase}_#{locale.upcase}") if locale.present?

  # if a localized profile is not found, fall back to the
  # country's profile before falling back to the default profile
  begin
    id = ids.pop
    find(id)
  rescue FrozenRecord::RecordNotFound
    if ids.present?
      retry
    else
      new(default_attributes.merge("id" => id))
    end
  end
end

.load_records(force: false) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/models/atlas_engine/country_profile.rb', line 169

def load_records(force: false)
  if force || (auto_reloading && file_changed?)
    unload!
  end

  @records ||= begin
    records = backend.load(file_path)
    if attribute_deserializers.any? || default_attributes
      records = records.map { |r| assign_defaults!(deserialize_attributes!(r.dup)).freeze }.freeze
    end
    @attributes = list_attributes(records).freeze
    define_attribute_methods(methods_to_be_created)
    records = FrozenRecord.ignore_max_records_scan { records.map { |r| load(r) }.freeze }
    index_definitions.values.each { |index| index.build(records) }
    records
  end
end

.locale_pathsObject



132
133
134
# File 'app/models/atlas_engine/country_profile.rb', line 132

def locale_paths
  @@locale_paths
end

.locale_paths=(paths) ⇒ Object



137
138
139
# File 'app/models/atlas_engine/country_profile.rb', line 137

def locale_paths=(paths)
  @@locale_paths = paths
end

.partial_zip_allowed_countriesObject



228
229
230
231
232
233
234
235
# File 'app/models/atlas_engine/country_profile.rb', line 228

def partial_zip_allowed_countries
  @partial_zip_allowed_countries ||= T.let(
    where.not(id: DEFAULT_PROFILE).filter do |country_profile|
      country_profile.ingestion.allow_partial_zip?
    end.map(&:id).to_set,
    T.nilable(T::Set[String]),
  )
end

.reset!Object



157
158
159
160
161
162
163
# File 'app/models/atlas_engine/country_profile.rb', line 157

def reset!
  unload!
  @@default_paths = []
  @@country_paths = []
  @@locale_paths = []
  @default_attributes = nil
end

.validation_enabled_countriesObject



238
239
240
241
242
243
244
245
# File 'app/models/atlas_engine/country_profile.rb', line 238

def validation_enabled_countries
  @validation_enabled_countries ||= T.let(
    where.not(id: DEFAULT_PROFILE).filter do |country_profile|
      country_profile.validation.enabled
    end.pluck(:id).to_set,
    T.nilable(T::Set[String]),
  )
end

Instance Method Details

#decompounding_patterns(field) ⇒ Object



271
272
273
# File 'app/models/atlas_engine/country_profile.rb', line 271

def decompounding_patterns(field)
  attributes.dig("decompounding_patterns", field.to_s) || []
end

#ingestionObject



263
264
265
# File 'app/models/atlas_engine/country_profile.rb', line 263

def ingestion
  CountryProfileIngestionSubset.new(hash: attributes["ingestion"])
end

#open_addressObject



268
# File 'app/models/atlas_engine/country_profile.rb', line 268

def open_address = (attributes["open_address"] || {}).with_indifferent_access

#validationObject



258
259
260
# File 'app/models/atlas_engine/country_profile.rb', line 258

def validation
  CountryProfileValidationSubset.new(hash: attributes["validation"] || {})
end