diff --git a/app/controllers/swf_assets_controller.rb b/app/controllers/swf_assets_controller.rb index 1649b0fa..f403f8c5 100644 --- a/app/controllers/swf_assets_controller.rb +++ b/app/controllers/swf_assets_controller.rb @@ -8,12 +8,12 @@ class SwfAssetsController < ApplicationController @swf_assets = @swf_assets.fitting_body_id(params[:body_id]) else @swf_assets = @swf_assets.fitting_standard_body_ids - json = @swf_assets.for_json.all.group_by(&:body_id) + json = @swf_assets.all.group_by(&:body_id) end elsif params[:pet_type_id] @swf_assets = PetType.find(params[:pet_type_id]).pet_states.first.swf_assets end - json ||= @swf_assets.for_json.all + json ||= @swf_assets.all render :json => json end end diff --git a/app/models/pet_attribute.rb b/app/models/pet_attribute.rb index f6bb7345..5697333a 100644 --- a/app/models/pet_attribute.rb +++ b/app/models/pet_attribute.rb @@ -1,31 +1,26 @@ -class PetAttribute - attr_accessor :id, :name - - def self.all - @objects - end - - def self.find(id) - @objects[id-1] - end - +class PetAttribute < StaticResource 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 = {} - - 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 + File.open(Rails.root.join('config', data_source)).each do |line| + process_line(line) end end end diff --git a/app/models/static_resource.rb b/app/models/static_resource.rb new file mode 100644 index 00000000..e9407d4a --- /dev/null +++ b/app/models/static_resource.rb @@ -0,0 +1,11 @@ +class StaticResource + attr_accessor :id, :name + + def self.all + @objects + end + + def self.find(id) + @objects[id-1] + end +end diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index cd0d3dc1..23316396 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -1,12 +1,8 @@ class SwfAsset < ActiveRecord::Base set_inheritance_column 'inheritance_type' - belongs_to :zone - delegate :depth, :to => :zone - scope :for_json, includes(:zone) - scope :fitting_body_id, lambda { |body_id| where(arel_table[:body_id].in([body_id, 0])) } @@ -33,4 +29,8 @@ class SwfAsset < ActiveRecord::Base :zone_id => zone_id } end + + def zone + @zone ||= Zone.find(zone_id) + end end diff --git a/app/models/zone.rb b/app/models/zone.rb index f75affde..1f70bc59 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -1,3 +1,18 @@ -class Zone < ActiveRecord::Base - set_inheritance_column 'inheritance_type' +class Zone < StaticResource + AttributeNames = ['id', 'label', 'depth'] + + attr_reader *AttributeNames + + def initialize(attributes) + AttributeNames.each do |name| + instance_variable_set "@#{name}", attributes[name] + end + end + + n = 0 + @objects = YAML.load_file(Rails.root.join('config', 'zones.yml')).map do |a| + a['id'] = (n += 1) + new(a) + end + n = nil end diff --git a/config/zones.yml b/config/zones.yml new file mode 100644 index 00000000..9bc0b8ba --- /dev/null +++ b/config/zones.yml @@ -0,0 +1,208 @@ +- label: Music + depth: 1 + type_id: 4 + type: AUDIO_MUSIC +- label: Sound Effects + depth: 2 + type_id: 4 + type: AUDIO_SOUND_EFFECTS +- label: Background + depth: 3 + type_id: 3 + type: BACKGROUND +- label: Biology Effects + depth: 6 + type_id: 1 + type: BIO_EFFECTS +- label: Hind Biology + depth: 7 + type_id: 1 + type: BIO_HIND +- label: Markings + depth: 8 + type_id: 2 + type: TATTOO +- label: Hind Disease + depth: 9 + type_id: 1 + type: BIO_DISEASE_HIND +- label: Hind Cover + depth: 10 + type_id: 2 + type: COVER_HIND +- label: Hind Transient Biology + depth: 11 + type_id: 1 + type: TRANSIENT_BIO_HIND +- label: Hind Drippings + depth: 12 + type_id: 1 + type: BIO_DRIPPINGS_HIND +- label: Backpack + depth: 13 + type_id: 2 + type: BACKPACK +- label: Wings Transient Biology + depth: 14 + type_id: 1 + type: TRANSIENT_BIO_WINGS +- label: Wings + depth: 15 + type_id: 2 + type: WINGS +- label: Hair Back + depth: 17 + type_id: 1 + type: TRANSIENT_BIO_HAIR_BACK +- label: Body + depth: 18 + type_id: 1 + type: BIO_BODY +- label: Markings + depth: 19 + type_id: 2 + type: TATTOO +- label: Body Disease + depth: 20 + type_id: 1 + type: BIO_DISEASE_BODY +- label: Feet Transient Biology + depth: 21 + type_id: 1 + type: TRANSIENT_BIO_FEET +- label: Shoes + depth: 22 + type_id: 2 + type: SHOES +- label: Lower-body Transient Biology + depth: 23 + type_id: 1 + type: TRANSIENT_BIO_BODY_LOWER +- label: Trousers + depth: 24 + type_id: 2 + type: PANTS +- label: Upper-body Transient Biology + depth: 25 + type_id: 1 + type: TRANSIENT_BIO_BODY_UPPER +- label: Shirt/Dress + depth: 26 + type_id: 2 + type: SHIRT_DRESS +- label: Necklace + depth: 28 + type_id: 2 + type: NECKLACE +- label: Gloves + depth: 29 + type_id: 2 + type: COVER_HAND +- label: Jacket + depth: 30 + type_id: 2 + type: JACKET +- label: Collar + depth: 31 + type_id: 2 + type: COLLAR +- label: Body Drippings + depth: 32 + type_id: 1 + type: BIO_DRIPPINGS_BODY +- label: Ruff + depth: 33 + type_id: 1 + type: BIO_RUFF +- label: Head + depth: 34 + type_id: 1 + type: BIO_HEAD +- label: Markings + depth: 35 + type_id: 2 + type: TATTOO +- label: Head Disease + depth: 36 + type_id: 1 + type: BIO_DISEASE_HEAD +- label: Eyes + depth: 37 + type_id: 1 + type: BIO_EYES +- label: Mouth + depth: 38 + type_id: 1 + type: BIO_MOUTH +- label: Glasses + depth: 41 + type_id: 2 + type: GLASSES +- label: Earrings + depth: 39 + type_id: 2 + type: EARRING +- label: Hair Front + depth: 40 + type_id: 1 + type: TRANSIENT_BIO_HAIR_FRONT +- label: Head Transient Biology + depth: 42 + type_id: 1 + type: TRANSIENT_BIO_HEAD +- label: Head Drippings + depth: 43 + type_id: 1 + type: BIO_DRIPPINGS_HEAD +- label: Hat + depth: 44 + type_id: 2 + type: HAT +- label: Earrings + depth: 45 + type_id: 2 + type: EARRING +- label: Right-hand Item + depth: 46 + type_id: 2 + type: ITEM_RIGHT_HAND +- label: Left-hand Item + depth: 47 + type_id: 2 + type: ITEM_LEFT_HAND +- label: Higher Foreground Item + depth: 49 + type_id: 3 + type: ITEM_FOREGROUND_HIGHER +- label: Lower Foreground Item + depth: 50 + type_id: 3 + type: ITEM_FOREGROUND_LOWER +- label: Static + depth: 48 + type_id: 3 + type: STATIC +- label: Thought Bubble + depth: 51 + type_id: 3 + type: THOUGHT_BUBBLE +- label: Background Item + depth: 4 + type_id: 3 + type: ITEM_BACKGROUND +- label: Right-hand Item + depth: 5 + type_id: 2 + type: ITEM_RIGHT_HAND +- label: Hat + depth: 16 + type_id: 2 + type: HAT +- label: Belt + depth: 27 + type_id: 2 + type: BELT +- label: Foreground + depth: 52 + type_id: 3 + type: TOP_FOREGROUND diff --git a/spec/models/swf_asset_spec.rb b/spec/models/swf_asset_spec.rb index 9a92b7b3..1a705641 100644 --- a/spec/models/swf_asset_spec.rb +++ b/spec/models/swf_asset_spec.rb @@ -2,17 +2,15 @@ require 'spec_helper' describe SwfAsset do it "belongs to a zone" do - zone = Factory.create :zone, :label => 'foo' asset = Factory.create :swf_asset, :zone_id => 1 asset.zone_id.should == 1 asset.zone.id.should == 1 - asset.zone.label.should == 'foo' + asset.zone.label.should == 'Music' end it "delegates depth to zone" do - zone = Factory.create :zone, :depth => 12 asset = Factory.create :swf_asset, :zone_id => 1 - asset.depth.should == 12 + asset.depth.should == 1 end it "converts neopets URL to impress URL" do @@ -21,18 +19,17 @@ describe SwfAsset do end it "should contain id, depth, zone ID, and local_url as JSON" do - zone = Factory.create :zone, :depth => 12 asset = Factory.create :swf_asset, :id => 123, - :zone_id => 1, + :zone_id => 4, :body_id => 234, :url => 'http://images.neopets.com/cp/items/swf/000/000/012/12211_9969430b3a.swf' asset.as_json.should == { :id => 123, - :depth => 12, + :depth => 6, :body_id => 234, :local_url => 'http://impress.openneo.net/assets/swf/outfit/items/000/000/012/12211_9969430b3a.swf', - :zone_id => 1 + :zone_id => 4 } end end diff --git a/spec/models/zone_spec.rb b/spec/models/zone_spec.rb new file mode 100644 index 00000000..5eb9a0dd --- /dev/null +++ b/spec/models/zone_spec.rb @@ -0,0 +1,8 @@ +require 'spec_helper' + +describe Zone do + specify "should find by id, report label" do + Zone.find(1).label.should == 'Music' + Zone.find(3).label.should == 'Background' + end +end diff --git a/test/factories/zone.rb b/test/factories/zone.rb deleted file mode 100644 index 10817e62..00000000 --- a/test/factories/zone.rb +++ /dev/null @@ -1,6 +0,0 @@ -Factory.define :zone do |z| - z.label 'foo' - z.depth 1 - z.add_attribute :type, 'FOO' - z.type_id 1 -end