Skip to content

Instantly share code, notes, and snippets.

@forrestwilkins
Created October 29, 2014 04:03
Show Gist options
  • Save forrestwilkins/a1f61a0cb9529797f8de to your computer and use it in GitHub Desktop.
Save forrestwilkins/a1f61a0cb9529797f8de to your computer and use it in GitHub Desktop.
class User
def close_enough(content)
_close_enough = false
zips_in_range = []
for zip in Zip.all
# verifies values and gets zips within constant range
if self.zip_code and self.network_size and content.zip_code
difference = (self.zip_code - zip.zip_code).abs
if difference < Zip.zip_code_range
zips_in_range << zip
end
end
end
# sorts by difference
zips_in_range.sort_by! do |zip|
(self.zip_code - zip.zip_code).abs
end
total_density = 0
for zip in zips_in_range
# adds the density of the closest zips until match or network size reached
combined_density = zip.density + Zip.find_by_zip_code(self.zip_code).density
if combined_density + total_density < self.network_size and not _close_enough
total_density += combined_density
if zip.zip_code == content.zip_code
_close_enough = true
end
else
break
end
end
return zips_in_range
end
end
class Zip
validates_uniqueness_of :zip_code
ZIP_CODE_RANGE = 10
def density
zips_in_range = []
for zip in Zip.all
difference = (self.zip_code - zip.zip_code).abs
if difference < ZIP_CODE_RANGE
zips_in_range << zip.zip_code
end
end
return zips_in_range.size
end
def self.record(zip)
unless self.find_by_zip_code(zip) or zip.nil?
self.create zip_code: zip
end
end
def self.zip_code_range
ZIP_CODE_RANGE
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment