Skip to content
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

Merged
merged 6 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/base_decorator.rb
@@ -0,0 +1,14 @@
require_relative './nameable'

class BaseDecorator < Nameable
attr_accessor :nameable

def initialize(nameable)
super()
Comment on lines +6 to +7
Copy link

@Gambit142 Gambit142 Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • [Optional] You do not need to call the super method here because the Nameable class doesn't have an initialize method (constructor). 馃憤

Copy link
Owner Author

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.

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

@nameable = nameable
end

def correct_name
@nameable.correct_name
end
end
7 changes: 7 additions & 0 deletions lib/capitalize_decorator.rb
@@ -0,0 +1,7 @@
require_relative './base_decorator'

class CapitalizeDecorator < BaseDecorator
def correct_name
@nameable.correct_name.capitalize
end
end
5 changes: 5 additions & 0 deletions lib/nameable.rb
@@ -0,0 +1,5 @@
class Nameable
def correct_name
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
end
11 changes: 10 additions & 1 deletion lib/person.rb
@@ -1,8 +1,13 @@
class Person
require_relative './nameable'
require_relative './capitalize_decorator'
require_relative './trimmer_decorator'

Copy link

@Gambit142 Gambit142 Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Kindly remove these statements here. When I try to create a Person object in other files to test your code, I get errors due to these lines of code. Remember that the only class you need to import in the person.rb file is the Nameable class. Kindly implement this change. 馃憤

Copy link
Owner Author

Choose a reason for hiding this comment

The 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()
Copy link

@Gambit142 Gambit142 Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • [Optional] You do not need to call the super method here because the Nameable class doesn't have an initialize method (constructor). 馃憤

@id = Random.rand(1..1000)
@name = name
@age = age
Expand All @@ -24,4 +29,8 @@ def can_use_service?

false
end

def correct_name
@name
end
end
11 changes: 11 additions & 0 deletions lib/trimmer_decorator.rb
@@ -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