Class: TextProtocols::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/text_protocols.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProtocol

Returns a new instance of Protocol.



79
80
81
# File 'lib/text_protocols.rb', line 79

def initialize
  @commands = {}
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



77
78
79
# File 'lib/text_protocols.rb', line 77

def commands
  @commands
end

#paramsObject (readonly)

Returns the value of attribute params.



77
78
79
# File 'lib/text_protocols.rb', line 77

def params
  @params
end

Instance Method Details

#cmd(name, &block) ⇒ Object



83
84
85
# File 'lib/text_protocols.rb', line 83

def cmd name, &block
  commands[name.upcase] = block
end

#handle(command, params) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/text_protocols.rb', line 87

def handle command, params
  block = commands[command.chomp.upcase]
  return "UNKNOW" if block.nil?
  
  if params
    @params = params
  else
    @params = {}
  end
  
  block.call
end