Created
May 27, 2010 13:14
-
-
Save jvanderhoof/415783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenericHashLookup | |
attr_accessor :state_hash, :office_hash, :agent_hash | |
def state(abbr) | |
generic_getter(Mls::State, 'state_hash', 'abbreviation', abbr) | |
end | |
def office_hash(mls_id) | |
generic_getter(Mls::Office, 'office_hash', 'mls_id', mls_id) | |
end | |
def agent_hash(mls_id) | |
generic_getter(Mls::Agent, 'agent_hash', 'mls_id', mls_id) | |
end | |
def generic_getter(klass, class_variable, method_name, key) | |
if self.send(class_variable).class.to_s == 'NilClass' | |
tmp = {} | |
klass.all.each{|i| tmp[i.send(method_name)] = i.id} | |
self.send("#{class_variable}=", tmp) | |
end | |
unless self.send(class_variable).has_key?(key) | |
tmp_class = klass.create({method_name.to_sym => key}) | |
self.send(class_variable)[key] = tmp_class.id | |
end | |
self.send(class_variable)[key] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment