Skip to content

Commit

Permalink
Merge pull request #2 from anagudelogu/decorate-a-class
Browse files Browse the repository at this point in the history
Decorate a class
  • Loading branch information
anagudelogu committed Jul 19, 2022
2 parents 065e3cb + fbd949f commit b63d5b4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/base_decorator.rb
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
7 changes: 7 additions & 0 deletions lib/capitalize_decorator.rb
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
5 changes: 5 additions & 0 deletions lib/nameable.rb
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
9 changes: 8 additions & 1 deletion lib/person.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class Person
require_relative './nameable'

class Person < Nameable
attr_reader :id
attr_accessor :name, :age

def initialize(age, name = 'Unknown', parent_permission: true)
super()
@id = Random.rand(1..1000)
@name = name
@age = age
Expand All @@ -24,4 +27,8 @@ def can_use_service?

false
end

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

0 comments on commit b63d5b4

Please sign in to comment.