Permalink
Browse files

CharacterクラスからInventoryで所持品にアクセスできるように。

  • Loading branch information...
1 parent 4a3fc00 commit ac6176fd516689429cc7ed1328bc69cfcc4d9279 @sakomoko committed Dec 31, 2012
View
@@ -16,7 +16,7 @@ class Character
embeds_one :position
embeds_many :assigned_skills
- embeds_many :belongings do
+ embeds_many :inventory, class_name: "InventoryItem" do
def have?(item, num = 1)
count = 0
where(item_id: item.id, :num.gt => 0).each do |b|
@@ -37,7 +37,7 @@ def add(item, num = 1)
doc.num += num
doc.save!
else
- self << Belonging.new(item: item, num: num, color: item.color)
+ self << InventoryItem.new(item: item, num: num, color: item.color)
end
true
end
@@ -46,9 +46,9 @@ def removed()
@removed||=[]
end
- def removed=(belonging_id)
+ def removed=(inventory_item_id)
@removed||=[]
- @removed << belonging_id
+ @removed << inventory_item_id
end
def remove(item, num = 1)
@@ -101,7 +101,7 @@ def remove(item, num = 1)
validates_presence_of :name
validates_uniqueness_of :name, :case_sensitive => true
- accepts_nested_attributes_for :candy, :equip, :position, :assigned_skills, :belongings
+ accepts_nested_attributes_for :candy, :equip, :position, :assigned_skills, :inventory
def action=(action)
if action.instance_of? Skill
@@ -110,7 +110,7 @@ def action=(action)
raise RuntimeError, 'character not assinged the skill.'
end
end
- elsif action.instance_of? Belonging
+ elsif action.instance_of? InventoryItem
unless self.id == action.character.id
raise RuntimeError, 'character has not this item.'
end
View
@@ -1,14 +1,14 @@
class Equip
include Mongoid::Document
embedded_in :character
- belongs_to :weapon, class_name: 'Belonging'
- belongs_to :shield, class_name: 'Belonging'
- belongs_to :head, class_name: 'Belonging'
- belongs_to :armor, class_name: 'Belonging'
- belongs_to :groob, class_name: 'Belonging'
- belongs_to :boots, class_name: 'Belonging'
- belongs_to :accessory, class_name: 'Belonging'
- belongs_to :arrow, class_name: 'Belonging'
+ belongs_to :weapon, class_name: 'InventoryItem'
+ belongs_to :shield, class_name: 'InventoryItem'
+ belongs_to :head, class_name: 'InventoryItem'
+ belongs_to :armor, class_name: 'InventoryItem'
+ belongs_to :groob, class_name: 'InventoryItem'
+ belongs_to :boots, class_name: 'InventoryItem'
+ belongs_to :accessory, class_name: 'InventoryItem'
+ belongs_to :arrow, class_name: 'InventoryItem'
def each
@equips ||= [:weapon, :shield, :head, :armor, :groob, :boots, :accessory, :arrow]
@@ -1,4 +1,4 @@
-class Belonging
+class InventoryItem
include Mongoid::Document
include Mongoid::Timestamps
@@ -37,7 +37,7 @@ def remove(num = 1)
return false if self.equipping?
self.num -= num
if self.num == 0
- self.character.belongings.removed = self.id
+ self.character.inventory.removed = self.id
self.delete
end
end
View
@@ -52,7 +52,7 @@
end
end
- factory :belonging do
+ factory :inventory_item do
character
item
num 1
View
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
describe Action do
let(:skill) { FactoryGirl.create :skill }
- let(:belonging) { FactoryGirl.create :belonging }
+ let(:inventory_item) { FactoryGirl.create :inventory_item }
it 'SkillクラスがActionモジュールをインクルードしていること' do
skill.is_a?(Action).should be_true
end
- it 'BelongingクラスがACtionモジュールをインクルードしていること' do
- belonging.is_a?(Action).should be_true
+ it 'InventoryItemクラスがACtionモジュールをインクルードしていること' do
+ inventory_item.is_a?(Action).should be_true
end
end
@@ -4,8 +4,8 @@
let(:character) { FactoryGirl.create :character, equip: FactoryGirl.create(:equip) }
let(:enemy) { FactoryGirl.create :enemy }
- let(:sword) { FactoryGirl.create :belonging, item: FactoryGirl.create(:sword_item, speed: -1, add_dex: 1), character: character}
- let(:armor) { FactoryGirl.create :belonging, item: FactoryGirl.create(:armor_item, add_vit: 1), character: character}
+ let(:sword) { FactoryGirl.create :inventory_item, item: FactoryGirl.create(:sword_item, speed: -1, add_dex: 1), character: character}
+ let(:armor) { FactoryGirl.create :inventory_item, item: FactoryGirl.create(:armor_item, add_vit: 1), character: character}
before do
sword.equip
armor.equip
@@ -3,8 +3,8 @@
let(:formula) { Eretheal::Formula.new }
let(:character) { FactoryGirl.create :character, equip: FactoryGirl.create(:equip) }
let(:enemy) { FactoryGirl.create :enemy }
- let(:sword) { FactoryGirl.create :belonging, item: FactoryGirl.create(:sword_item, speed: -1, add_dex: 1), character: character}
- let(:armor) { FactoryGirl.create :belonging, item: FactoryGirl.create(:armor_item, add_vit: 1), character: character}
+ let(:sword) { FactoryGirl.create :inventory_item, item: FactoryGirl.create(:sword_item, speed: -1, add_dex: 1), character: character}
+ let(:armor) { FactoryGirl.create :inventory_item, item: FactoryGirl.create(:armor_item, add_vit: 1), character: character}
before do
sword.equip
armor.equip
@@ -7,119 +7,119 @@
before do
@pc = FactoryGirl.create :character, equip: FactoryGirl.create(:equip)
end
- subject { @pc.belongings }
+ subject { @pc.inventory }
context '既にアイテムAを所持している場合' do
before do
- @pc.belongings << FactoryGirl.create(:belonging)
- @item = @pc.belongings.first.item
- @num = @pc.belongings.first.num
+ @pc.inventory << FactoryGirl.create(:inventory_item)
+ @item = @pc.inventory.first.item
+ @num = @pc.inventory.first.num
end
it { should be_have @item }
it 'アイテムAを参照できること' do
- belonging = @pc.belongings.where(item_id: @item.id).first
- belonging.item.id.should eq @item.id
+ inventory_item = @pc.inventory.where(item_id: @item.id).first
+ inventory_item.item.id.should eq @item.id
end
context 'アイテムを1個入手したのなら' do
before do
- @pc.belongings.add @item
+ @pc.inventory.add @item
end
it 'スタック個数が1増えること' do
- @pc.belongings.first.num.should eq @num + 1
+ @pc.inventory.first.num.should eq @num + 1
end
end
context 'アイテムを3個を入手したのなら' do
before do
- @pc.belongings.add @item, 3
+ @pc.inventory.add @item, 3
end
it 'スタック個数が3増えること' do
- @pc.belongings.first.num.should eq @num + 3
+ @pc.inventory.first.num.should eq @num + 3
end
end
end
context 'アイテムAを所持していない場合' do
before do
- @pc.belongings << FactoryGirl.create(:belonging)
+ @pc.inventory << FactoryGirl.create(:inventory_item)
@item = FactoryGirl.create(:item)
end
it { should_not be_have @item }
- it { @pc.belongings.add(@item).should be_true }
+ it { @pc.inventory.add(@item).should be_true }
context 'バッグに余裕があれば' do
before do
- @size = @pc.belongings.size
- @pc.belongings.add @item, 3
+ @size = @pc.inventory.size
+ @pc.inventory.add @item, 3
end
- it 'belongingsにアイテムが追加されていること' do
- @pc.belongings.size.should eq @size + 1
+ it 'inventoryにアイテムが追加されていること' do
+ @pc.inventory.size.should eq @size + 1
end
- it { @pc.belongings.should be_have @item, 3 }
- it { @pc.belongings.where(item_id: @item.id).first.color.should eq @item.color }
+ it { @pc.inventory.should be_have @item, 3 }
+ it { @pc.inventory.where(item_id: @item.id).first.color.should eq @item.color }
end
context 'バッグが他のアイテムでいっぱいだったら' do
before do
- 5.times { @pc.belongings << FactoryGirl.create(:belonging) }
+ 5.times { @pc.inventory << FactoryGirl.create(:inventory_item) }
@pc.bag_size = 6
end
- it { @pc.belongings.add(@item).should be_false }
+ it { @pc.inventory.add(@item).should be_false }
end
end
context 'アイテムAを2個所持している場合' do
before do
- @pc.belongings << FactoryGirl.create(:belonging, :num => 2)
- @item = @pc.belongings.first.item
+ @pc.inventory << FactoryGirl.create(:inventory_item, :num => 2)
+ @item = @pc.inventory.first.item
end
it { should be_have @item, 2 }
end
context 'アイテムAを2スタックに分けて合計11個所持しているとき' do
before do
@item = FactoryGirl.create :item
- @pc.belongings << FactoryGirl.create(:belonging, :item => @item, :num => 6)
- @pc.belongings << FactoryGirl.create(:belonging, :item => @item, :num => 5)
+ @pc.inventory << FactoryGirl.create(:inventory_item, :item => @item, :num => 6)
+ @pc.inventory << FactoryGirl.create(:inventory_item, :item => @item, :num => 5)
end
it { should be_have @item, 11 }
end
end
- describe 'BelongingExtension#remove' do
+ describe 'Inventory_ItemExtension#remove' do
let(:pc) { FactoryGirl.create :character, equip: FactoryGirl.create(:equip) }
context 'スタック可能アイテムの場合' do
let(:item) { FactoryGirl.create :item }
- let(:belonging) { FactoryGirl.create(:belonging, item: item, num: 10) }
+ let(:inventory_item) { FactoryGirl.create(:inventory_item, item: item, num: 10) }
before do
- pc.belongings << belonging
+ pc.inventory << inventory_item
end
- it { pc.belongings.remove(item, 5).should be_true }
+ it { pc.inventory.remove(item, 5).should be_true }
it '残り個数が5減っていること' do
- pc.belongings.remove(item, 5)
- pc.belongings.first.num.should eq 5
+ pc.inventory.remove(item, 5)
+ pc.inventory.first.num.should eq 5
end
context 'スタックを分けて所持しているとき' do
before do
3.times do
- pc.belongings << FactoryGirl.create(:belonging, item: item, num: 5)
+ pc.inventory << FactoryGirl.create(:inventory_item, item: item, num: 5)
end
- pc.belongings.remove item, 20
+ pc.inventory.remove item, 20
end
- it { pc.belongings.should have(1).items }
+ it { pc.inventory.should have(1).items }
end
end
context 'スタック不可能アイテムの場合' do
let(:item) { FactoryGirl.create :unstacked_item }
- subject { pc.belongings }
+ subject { pc.inventory }
before do
3.times do
- pc.belongings << FactoryGirl.create(:belonging, item: item)
+ pc.inventory << FactoryGirl.create(:inventory_item, item: item)
end
subject.remove item, 2
end
@@ -131,37 +131,37 @@
let(:item) { FactoryGirl.create :sword_item }
before do
10.times do
- pc.belongings << FactoryGirl.create(:belonging, item: item)
+ pc.inventory << FactoryGirl.create(:inventory_item, item: item)
end
- pc.belongings.first.equip
- @belonging_id = pc.belongings.first.id
- pc.belongings.remove item, 8
+ pc.inventory.first.equip
+ @inventory_item_id = pc.inventory.first.id
+ pc.inventory.remove item, 8
end
it '装備中のアイテムは削除されないこと' do
- pc.belongings.find(@belonging_id).should_not be_nil
+ pc.inventory.find(@inventory_item_id).should_not be_nil
end
end
end
- describe 'BelongingExtension#have?' do
+ describe 'Inventory_ItemExtension#have?' do
let(:pc) { FactoryGirl.create :character, equip: FactoryGirl.create(:equip) }
let(:item) { FactoryGirl.create :sword_item }
context '所持品の内一つが装備中のとき' do
before do
3.times do
- pc.belongings << FactoryGirl.create(:belonging, item: item)
+ pc.inventory << FactoryGirl.create(:inventory_item, item: item)
end
- pc.belongings.first.equip
+ pc.inventory.first.equip
end
- it { pc.belongings.should_not be_have item, 3 }
- it { pc.belongings.should be_have item, 2 }
+ it { pc.inventory.should_not be_have item, 3 }
+ it { pc.inventory.should be_have item, 2 }
end
end
describe 'Character#action=' do
let(:pc) { FactoryGirl.create :character }
let(:skill) { FactoryGirl.create :skill }
- let(:belonging) { FactoryGirl.create :belonging }
+ let(:inventory_item) { FactoryGirl.create :inventory_item }
context 'アサインされたスキルをセットするとき' do
before do
pc.assigned_skills << FactoryGirl.create(:assigned_skill, skill: skill)
@@ -190,17 +190,17 @@
context '所持品をセットするとき' do
before do
- pc.belongings << belonging
- pc.action = belonging
+ pc.inventory << inventory_item
+ pc.action = inventory_item
end
it 'アクションがセットされていること' do
- pc.action.should eq belonging
+ pc.action.should eq inventory_item
end
end
context '所持品でないものをセットするとき' do
it '例外が発生すること' do
- expect { pc.action = belonging }.to raise_error
+ expect { pc.action = inventory_item }.to raise_error
end
end
end
@@ -6,11 +6,11 @@
before do
@pc = FactoryGirl.create :character
@pc.equip = FactoryGirl.create :equip
- @pc.belongings << FactoryGirl.create(:belonging)
- @pc.equip.weapon = @pc.belongings.first
+ @pc.inventory << FactoryGirl.create(:inventory_item)
+ @pc.equip.weapon = @pc.inventory.first
end
it '武器と装備品は同一であること' do
- @pc.equip.weapon.id.should eq @pc.belongings.first.id
+ @pc.equip.weapon.id.should eq @pc.inventory.first.id
end
end
Oops, something went wrong.

0 comments on commit ac6176f

Please sign in to comment.