Skip to content
This repository has been archived by the owner on Oct 2, 2019. It is now read-only.

Commit

Permalink
refactor: Make commands know about their keywords
Browse files Browse the repository at this point in the history
Now commands know about their own keywords and now only the Command
class, preventing wrong assumptions of responsability and coupled code.
  • Loading branch information
marceloboeira committed Aug 3, 2016
1 parent 1e70bfd commit 555a0e4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/bojack/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ require "./commands/size"

module Bojack
module Command
REGISTRY = [
Bojack::Commands::Get,
Bojack::Commands::Set,
Bojack::Commands::Delete,
Bojack::Commands::Size,
]

# Factory method for Bojack Commands
#
# It holds the logic to create the command instances
#
# @param param_command [String]
def self.from(param_command) : Bojack::Commands::Command?
case param_command
when "set"
Bojack::Commands::Set.new
when "get"
Bojack::Commands::Get.new
when "delete"
Bojack::Commands::Delete.new
when "size"
Bojack::Commands::Size.new
else
nil
# @param command [String]
def self.from(keyword) : Bojack::Commands::Command?
clazz = REGISTRY.find(nil) do |clazz|
clazz.keyword == keyword
end

return clazz.new if clazz

nil
end
end
end
10 changes: 10 additions & 0 deletions src/bojack/commands/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Bojack
# Represents a Bojack command
#
abstract class Command
@@keyword : String = "none"

# Execute method
#
# Implements the process for command.
Expand All @@ -13,6 +15,14 @@ module Bojack
# @param key [String] for memory retrieve.
# @param value [String] for memory changes.
abstract def execute(memory, key : String?, value : String?) : String

private def self.keyword=(value : String)
@@keyword = value
end

def self.keyword
@@keyword
end
end
end
end
2 changes: 2 additions & 0 deletions src/bojack/commands/delete.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./command"
module Bojack
module Commands
class Delete < Command
self.keyword = "delete"

def execute(memory, key : String?, value : String?) : String
memory.delete(key)
rescue
Expand Down
2 changes: 2 additions & 0 deletions src/bojack/commands/get.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./command"
module Bojack
module Commands
class Get < Command
self.keyword = "get"

def execute(memory, key : String?, value : String?) : String
memory.read(key)
rescue
Expand Down
2 changes: 2 additions & 0 deletions src/bojack/commands/set.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./command"
module Bojack
module Commands
class Set < Command
self.keyword = "set"

def execute(memory, key : String?, value : String?) : String
memory.write(key, value)
end
Expand Down
2 changes: 2 additions & 0 deletions src/bojack/commands/size.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./command"
module Bojack
module Commands
class Size < Command
self.keyword = "size"

def execute(memory, key : String?, value : String?) : String
"#{memory.size}"
end
Expand Down

0 comments on commit 555a0e4

Please sign in to comment.