1
0
Fork 0
forked from OpenNeo/impress
impress/app/models/pet_attribute.rb
2010-11-06 11:52:58 -04:00

47 lines
885 B
Ruby

class PetAttribute < StaticResource
def as_json(options={})
{
:id => self.id,
:name => self.human_name
}
end
def human_name
self.name.capitalize
end
def self.find(id)
attribute = super
unless attribute
attribute = new
attribute.id = id
attribute.name = "color \##{id}"
end
attribute
end
def self.find_by_name(name)
@objects_by_name[name.downcase]
end
private
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
def self.fetch_objects!
@objects = []
@objects_by_name = {}
File.open(Rails.root.join('config', data_source)).each do |line|
process_line(line)
end
end
end