Permalink
Browse files

EquipCategoryモデルを追加した。細分化されたItemTypeモデルは、装備箇所を分類したEquipCategoryモデルを持つ。

  • Loading branch information...
sakomoko committed Jan 3, 2013
1 parent fee9831 commit 5ff8edb6323f2059b6d41d0a474f2344b6e9314b
@@ -0,0 +1,10 @@
+class EquipCategory
+ include Mongoid::Document
+ include Mongoid::Slug
+
+ field :key, type: String
+ field :name, type: String
+ field :equip, type: Boolean, default: 1
+
+ slug :key
+end
@@ -13,8 +13,8 @@ class InventoryItem
field :sort, :type => Integer, :default => 0
def equip
- return false unless self.item.item_type.equip?
- category = self.item.item_type.category
+ return false unless self.item.item_type.equip_category.equip?
+ category = self.item.item_type.equip_category.key
if category == 'weapon' && self.item.two_handed?
self.character.equip.shield = nil
elsif category == 'shield' && self.character.equip.weapon && self.character.equip.weapon.item.two_handed?
@@ -25,8 +25,8 @@ def equip
end
def unequip
- return false if !self.item.item_type.equip? || !self.equipping?
- category = self.item.item_type.category
+ return false if !self.item.item_type.equip_category.equip? || !self.equipping?
+ category = self.item.item_type.equip_category.key
self.character.equip.send category + '=', nil
self.character.unmemoize_all
true
@@ -44,7 +44,7 @@ def remove(num = 1)
def equipping?
equip = self.character.equip
- category = self.item.item_type.category
+ category = self.item.item_type.equip_category.key
if equip.respond_to? category
equipping = equip.send(category)
return true if equipping && equipping.id == self.id
View
@@ -1,10 +1,12 @@
class ItemType
include Mongoid::Document
+ include Mongoid::Slug
+
field :key, :type => String
field :name, :type => String
- field :category, :type => String
field :range, :type => Integer, :default => 0
- field :equip, :type => Boolean, :default => false
+ slug :key
+ belongs_to :equip_category, index: true
belongs_to :attribute, index: true
end
View
@@ -100,38 +100,33 @@
factory :item_type do
key 'item'
name 'Item'
- category 'item'
range 0
- equip false
+ association :equip_category, factory: :item_category
factory :sword_type do
key 'sword'
name 'Sword'
- category 'weapon'
range 2
- equip true
+ association :equip_category, factory: :weapon_category
end
factory :spear_type do
key 'spear'
name 'Spear'
- category 'weapon'
range 2
- equip true
+ association :equip_category, factory: :weapon_category
end
factory :cloth_armor_type do
key 'cloth_armor'
name 'ClothArmor'
- category 'armor'
- equip true
+ association :equip_category, factory: :armor_category
end
factory :shield_type do
key 'shield'
name 'Shield'
- category 'shield'
- equip true
+ association :equip_category, factory: :shield_category
end
end
@@ -0,0 +1,41 @@
+# -*- coding: utf-8 -*-
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :equip_category do
+ equip true
+
+ factory :item_category do
+ key 'item'
+ name '道具'
+ equip false
+ end
+
+ factory :weapon_category do
+ key 'weapon'
+ name '武器'
+ end
+
+ factory :bullet_category do
+ key 'bullet'
+ name '矢弾'
+ end
+
+ factory :armor_category do
+ key 'armor'
+ name ''
+ end
+
+ factory :shield_category do
+ key 'shield'
+ name ''
+ end
+
+ factory :material_category do
+ key 'material'
+ name '素材'
+ equip false
+ end
+
+ end
+end
View
@@ -1,11 +1,12 @@
# -*- coding: utf-8 -*-
describe Action do
+
let(:skill) { FactoryGirl.create :skill }
let(:inventory_item) { FactoryGirl.create :inventory_item }
it 'SkillクラスがActionモジュールをインクルードしていること' do
skill.is_a?(Action).should be_true
end
- it 'InventoryItemクラスがACtionモジュールをインクルードしていること' do
+ it 'InventoryItemクラスがActionモジュールをインクルードしていること' do
inventory_item.is_a?(Action).should be_true
end
end
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+require 'spec_helper'
+
+describe EquipCategory do
+ let(:weapon_category) { FactoryGirl.create(:weapon_category) }
+ describe 'keyからドキュメントを探すことができること' do
+ before do
+ weapon_category
+ end
+ it { EquipCategory.find('weapon').should eq weapon_category }
+ end
+
+ describe '装備品かどうかを真偽できること' do
+ subject { weapon_category }
+ it { should be_equip }
+
+ context '対象が素材のとき' do
+ subject { FactoryGirl.create(:material_category) }
+ it { should_not be_equip }
+ end
+ end
+end
@@ -2,17 +2,11 @@
require 'spec_helper'
describe ItemType do
- context 'アイテムAを装備可能かどうか診断したとき' do
- subject { item }
- context '装備可能なアイテムであれば' do
- let(:item) { FactoryGirl.create :item, :item_type => FactoryGirl.create(:sword_type) }
- its(:item_type) { should be_equip }
+ describe 'keyからアイテムタイプを探すことができる' do
+ let(:sword_type) { FactoryGirl.create :sword_type }
+ before do
+ sword_type
end
-
- context '装備可能なアイテムでなければ' do
- let(:item) { FactoryGirl.create :item, :item_type => FactoryGirl.create(:item_type) }
- its(:item_type) { should_not be_equip }
- end
-
+ it { ItemType.find('sword').should eq sword_type }
end
end

0 comments on commit 5ff8edb

Please sign in to comment.