diff --git a/app/models/color.rb b/app/models/color.rb new file mode 100644 index 00000000..84f619ab --- /dev/null +++ b/app/models/color.rb @@ -0,0 +1,3 @@ +class Color < PetAttribute + fetch_objects! +end diff --git a/app/models/pet_attribute.rb b/app/models/pet_attribute.rb new file mode 100644 index 00000000..f6bb7345 --- /dev/null +++ b/app/models/pet_attribute.rb @@ -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 diff --git a/app/models/species.rb b/app/models/species.rb index ce91eada..6144ca99 100644 --- a/app/models/species.rb +++ b/app/models/species.rb @@ -1,16 +1,5 @@ -require 'yaml' - -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 +class Species < PetAttribute + fetch_objects! StandardColors = %w(blue green yellow red) StandardHashes = YAML::load_file(Rails.root.join('config', 'standard_type_hashes.yml')) @@ -18,16 +7,4 @@ class Species def hash_for_color(color) StandardHashes[name][color] 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 diff --git a/config/colors.txt b/config/colors.txt new file mode 100644 index 00000000..07d1df3f --- /dev/null +++ b/config/colors.txt @@ -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 diff --git a/spec/models/color_spec.rb b/spec/models/color_spec.rb new file mode 100644 index 00000000..7c083198 --- /dev/null +++ b/spec/models/color_spec.rb @@ -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