-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decorate a class #2
Changes from 5 commits
ec07700
eb536c8
9cde468
f1f114f
6dcb97f
fbd949f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require_relative './nameable' | ||
|
||
class BaseDecorator < Nameable | ||
attr_accessor :nameable | ||
|
||
def initialize(nameable) | ||
super() | ||
@nameable = nameable | ||
end | ||
|
||
def correct_name | ||
@nameable.correct_name | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require_relative './base_decorator' | ||
|
||
class CapitalizeDecorator < BaseDecorator | ||
def correct_name | ||
@nameable.correct_name.capitalize | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Nameable | ||
def correct_name | ||
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
class Person | ||
require_relative './nameable' | ||
require_relative './capitalize_decorator' | ||
require_relative './trimmer_decorator' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Gambit142 Sure, I tested the functionality and forgot to remove those, thank you for your feedback! |
||
class Person < Nameable | ||
attr_reader :id | ||
attr_accessor :name, :age | ||
|
||
def initialize(age, name = 'Unknown', parent_permission: true) | ||
super() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
@id = Random.rand(1..1000) | ||
@name = name | ||
@age = age | ||
|
@@ -24,4 +29,8 @@ def can_use_service? | |
|
||
false | ||
end | ||
|
||
def correct_name | ||
@name | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require_relative './base_decorator' | ||
|
||
class TrimmerDecorator < BaseDecorator | ||
def correct_name | ||
name = @nameable.correct_name | ||
size = name.size | ||
return name.slice!(0..9) if size > 10 | ||
|
||
name | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super
method here because theNameable
class doesn't have an initialize method (constructor). 馃憤There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Gambit142! thank you for your review, I had to add
super
because it throws an offense in rubocop if not added.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @anagudelogu, It is alright, I forgot there has been a debate on this issue since the year 2020. 馃槃 You can get the full story here