forked from tutsplus/ruby-refactoring
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Type Code with Module Extension.
- Loading branch information
1 parent
3b457c1
commit 49e3e1a
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Employee | ||
def initialize(type: :regular) | ||
@type = type | ||
end | ||
|
||
def base_salary | ||
500.0 | ||
end | ||
|
||
def salary | ||
base_salary + bonus | ||
end | ||
|
||
def self.build(type: :regular) | ||
instance = new | ||
instance.extend const_get(type.capitalize) | ||
end | ||
|
||
end | ||
|
||
module Regular | ||
def bonus | ||
0 | ||
end | ||
end | ||
|
||
module Manager | ||
def bonus | ||
800 | ||
end | ||
end | ||
|
||
module Boss | ||
def bonus | ||
1500 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Employee | ||
def initialize(type: :regular) | ||
@type = type | ||
end | ||
|
||
def base_salary | ||
500.0 | ||
end | ||
|
||
def salary | ||
base_salary + bonus | ||
end | ||
|
||
private | ||
|
||
def self.build(type: :regular) | ||
new type: type | ||
end | ||
|
||
def bonus | ||
value = case @type | ||
when :regular then 0 | ||
when :boss then 1500.0 | ||
when :manager then 800.0 | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters