class USGeo::BaseRecord
Base class that all models inherit from.
Constants
- STATUS_IMPORTED
- STATUS_MANUAL
- STATUS_REMOVED
Public Class Methods
load!(location = nil, gzipped: true)
click to toggle source
# File lib/us_geo/base_record.rb, line 29 def load!(location = nil, gzipped: true) raise NotImplementedError end
Protected Class Methods
area_meters_to_miles(square_meters)
click to toggle source
Convert square meters to square miles
# File lib/us_geo/base_record.rb, line 86 def area_meters_to_miles(square_meters) (square_meters.to_f / (1609.34 ** 2)).round(6) end
data_uri(path)
click to toggle source
# File lib/us_geo/base_record.rb, line 52 def data_uri(path) path = path.to_s if path if path.start_with?("/") || path.include?(":") path elsif USGeo.base_data_uri.include?(":") "#{USGeo.base_data_uri}/#{path}" else File.join(USGeo.base_data_uri, path) end end
import!() { || ... }
click to toggle source
Mark the status of any records not updated in the block as being no longer imported.
# File lib/us_geo/base_record.rb, line 45 def import!(&block) start_time = Time.at(Time.now.to_i.floor) yield raise LoadError.new("No data found") unless where("updated_at >= ?", start_time).exists? where("updated_at < ?", start_time).imported.update_all(status: STATUS_REMOVED) end
load_data_file(location, &block)
click to toggle source
# File lib/us_geo/base_record.rb, line 63 def load_data_file(location, &block) file = nil if location.include?(":") file = URI.parse(location).open(read_timeout: 5, open_timeout: 5) else file = File.open(location) end begin rows = [] CSV.new(file, headers: true).each do |row| rows << row if rows.size >= 50 transaction { rows.each(&block) } rows.clear end end transaction { rows.each(&block) } unless rows.empty? ensure file.close if file && !file.closed? end end
load_record!(criteria) { |record| ... }
click to toggle source
Insert or update a record given the unique criteria for finding it.
# File lib/us_geo/base_record.rb, line 36 def load_record!(criteria, &block) record = find_or_initialize_by(criteria) record.status = STATUS_IMPORTED record.updated_at = Time.now yield(record) record.save! end
Public Instance Methods
imported?()
click to toggle source
# File lib/us_geo/base_record.rb, line 91 def imported? status == STATUS_IMPORTED end
manual?()
click to toggle source
# File lib/us_geo/base_record.rb, line 99 def manual? status == STATUS_MANUAL end
removed?()
click to toggle source
# File lib/us_geo/base_record.rb, line 95 def removed? status == STATUS_REMOVED end