2010-06-09 19:56:47 -07:00
|
|
|
class PetAttribute < StaticResource
|
2010-10-10 19:18:42 -07:00
|
|
|
def as_json(options={})
|
|
|
|
{
|
|
|
|
:id => self.id,
|
2010-11-05 15:45:05 -07:00
|
|
|
:name => self.human_name
|
2010-10-10 19:18:42 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-11-05 15:45:05 -07:00
|
|
|
def human_name
|
|
|
|
self.name.capitalize
|
|
|
|
end
|
|
|
|
|
2010-11-24 18:17:24 -08:00
|
|
|
class << self
|
|
|
|
def all_ordered_by_name
|
|
|
|
@objects_ordered_by_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(id)
|
|
|
|
attribute = super
|
|
|
|
unless attribute
|
|
|
|
attribute = new
|
|
|
|
attribute.id = id
|
|
|
|
attribute.name = "color \##{id}"
|
|
|
|
end
|
|
|
|
attribute
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_name(name)
|
|
|
|
@objects_by_name[name.downcase]
|
2010-11-06 08:52:58 -07:00
|
|
|
end
|
2010-05-16 12:44:32 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2010-06-09 19:56:47 -07:00
|
|
|
def self.data_source
|
|
|
|
"#{to_s.downcase.pluralize}.txt"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.process_line(line)
|
|
|
|
name = line.chomp.downcase
|
|
|
|
@objects << @objects_by_name[name] = species = new
|
|
|
|
species.id = @objects.size
|
|
|
|
species.name = name
|
|
|
|
end
|
|
|
|
|
2010-05-16 12:44:32 -07:00
|
|
|
def self.fetch_objects!
|
|
|
|
@objects = []
|
|
|
|
@objects_by_name = {}
|
2010-06-09 19:56:47 -07:00
|
|
|
File.open(Rails.root.join('config', data_source)).each do |line|
|
|
|
|
process_line(line)
|
2010-05-16 12:44:32 -07:00
|
|
|
end
|
2010-11-24 18:17:24 -08:00
|
|
|
@objects_ordered_by_name = @objects.sort { |a,b| a.name <=> b.name }
|
2010-05-16 12:44:32 -07:00
|
|
|
end
|
|
|
|
end
|