add color model, have it and species descend from PetAttribute

This commit is contained in:
Emi Matchu 2010-05-16 15:44:32 -04:00
parent f69f64f18f
commit f6dce65dfb
5 changed files with 142 additions and 25 deletions

3
app/models/color.rb Normal file
View file

@ -0,0 +1,3 @@
class Color < PetAttribute
fetch_objects!
end

View file

@ -0,0 +1,31 @@
class PetAttribute
attr_accessor :id, :name
def self.all
@objects
end
def self.find(id)
@objects[id-1]
end
def self.find_by_name(name)
@objects_by_name[name.downcase]
end
private
def self.fetch_objects!
@objects = []
@objects_by_name = {}
filename = "#{to_s.downcase.pluralize}.txt"
File.open(Rails.root.join('config', filename)).each do |line|
name = line.chomp.downcase
@objects << @objects_by_name[name] = species = new
species.id = @objects.size
species.name = name
end
end
end

View file

@ -1,16 +1,5 @@
require 'yaml' class Species < PetAttribute
fetch_objects!
class Species
attr_accessor :id, :name
@objects = []
@objects_by_name = {}
File.open(Rails.root.join('config', 'species.txt')).each do |line|
name = line.chomp.downcase
@objects << @objects_by_name[name] = species = Species.new
species.id = @objects.size
species.name = name
end
StandardColors = %w(blue green yellow red) StandardColors = %w(blue green yellow red)
StandardHashes = YAML::load_file(Rails.root.join('config', 'standard_type_hashes.yml')) StandardHashes = YAML::load_file(Rails.root.join('config', 'standard_type_hashes.yml'))
@ -18,16 +7,4 @@ class Species
def hash_for_color(color) def hash_for_color(color)
StandardHashes[name][color] StandardHashes[name][color]
end end
def self.all
@objects
end
def self.find(id)
@objects[id-1]
end
def self.find_by_name(name)
@objects_by_name[name.downcase]
end
end end

88
config/colors.txt Normal file
View file

@ -0,0 +1,88 @@
Alien
Apple
Asparagus
Aubergine
Avocado
Baby
Biscuit
Blue
Blueberry
Brown
Camouflage
Carrot
Checkered
Chocolate
Chokato
Christmas
Clay
Cloud
Coconut
Custard
Darigan
Desert
Disco
Durian
Electric
Faerie
Fire
Garlic
Ghost
Glowing
Gold
Gooseberry
Grape
Green
Grey
Halloween
Ice
Invisible
Island
Jelly
Lemon
Lime
Mallow
Maraquan
Msp
Mutant
Orange
Pea
Peach
Pear
Pepper
Pineapple
Pink
Pirate
Plum
Plushie
Purple
Quigukiboy
Quigukigirl
Rainbow
Red
Robot
Royalboy
Royalgirl
Shadow
Silver
Sketch
Skunk
Snot
Snow
Speckled
Split
Sponge
Spotted
Starry
Strawberry
Striped
Thornberry
Tomato
Tyrannian
Usukiboy
Usukigirl
White
Yellow
Zombie
Onion
Magma
Relic

18
spec/models/color_spec.rb Normal file
View file

@ -0,0 +1,18 @@
require 'spec_helper'
describe Color do
specify "should find by id, report name" do
Color.find(1).name.should == 'alien'
Color.find(2).name.should == 'apple'
end
specify "should find by name, report id" do
Color.find_by_name('alien').id.should == 1
Color.find_by_name('apple').id.should == 2
end
specify "name should be case-insensitive" do
Color.find_by_name('Alien').id.should == 1
Color.find_by_name('alien').id.should == 1
end
end