Skip to content

Commit

Permalink
Refactor attack methods and NullWeapon
Browse files Browse the repository at this point in the history
  • Loading branch information
szoroh committed Apr 17, 2019
1 parent a1a2262 commit b92faf9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lecture_4/homework/app/models/warrior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ class Warrior < ApplicationRecord

scope :alive, -> { where('death_date IS NULL') }
scope :dead, -> { where('death_date IS NOT NULL') }

def attack
"#{type.demodulize} #{name} attacked with #{weapon_null_check.type.demodulize}"
end

def null_weapon_check
weapon || NullWeapon.new
end
end
4 changes: 0 additions & 4 deletions lecture_4/homework/app/models/warriors/hussar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
module Warriors
class Hussar < Warrior
attribute :preferred_weapon_kind, :string, default: :ranged

def attack
"Hussar #{name} charged while yielding #{weapon.type.demodulize}"
end
end
end
4 changes: 0 additions & 4 deletions lecture_4/homework/app/models/warriors/samurai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
module Warriors
class Samurai < Warrior
attribute :preferred_weapon_kind, :string, default: :melee

def attack
"Samurai #{name} attacked with #{weapon.type.demodulize}"
end
end
end
15 changes: 15 additions & 0 deletions lecture_4/homework/app/nulls/null_weapon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_literal_string: true

class NullWeapon
def type
'Oops, no weapon.'
end

def range
nil
end

def damage
nil
end
end

0 comments on commit b92faf9

Please sign in to comment.