Skip to content

Instantly share code, notes, and snippets.

@jvanderhoof
Created May 27, 2010 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvanderhoof/415783 to your computer and use it in GitHub Desktop.
Save jvanderhoof/415783 to your computer and use it in GitHub Desktop.
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