Class: KJess::Protocol

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

Overview

Protocol is the base class that all Kestrel requests and responses are developed on. it defines the DSL for creating the Request and Response objects that make up the Protocol.

Direct Known Subclasses

Request, Response

Constant Summary collapse

CRLF =
"\r\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Protocol

Returns a new instance of Protocol.



48
49
50
51
# File 'lib/kjess/protocol.rb', line 48

def initialize( opts = {} )
  @raw_args = opts
  @args = parse_options_to_args( opts ) || []
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



45
46
47
# File 'lib/kjess/protocol.rb', line 45

def args
  @args
end

#raw_argsObject (readonly)

Returns the value of attribute raw_args.



46
47
48
# File 'lib/kjess/protocol.rb', line 46

def raw_args
  @raw_args
end

Class Method Details

.arity(a = nil) ⇒ Object

Internal: define or return the arity of this protocol item

arity - the number of args this protocol item has

Returns the arity



30
31
32
33
# File 'lib/kjess/protocol.rb', line 30

def arity( a = nil )
  @arity = a if a
  @arity
end

.keyword(name = nil) ⇒ Object

Internal: The keyword that starts this protocol message

name - the keyword to define this portion of the protocol

Returns the name



16
17
18
19
20
21
22
23
# File 'lib/kjess/protocol.rb', line 16

def keyword( name = nil )
  @keyword = nil unless defined? @keyword
  if name then
    register( name )
    @keyword = name
  end
  @keyword ||= nil
end

.register(name) ⇒ Object

Internal: register this protocol item with its registry

name - the name under which to register the protocol

Returns nothing



40
41
42
# File 'lib/kjess/protocol.rb', line 40

def register( name )
  registry[name] ||= self
end

Instance Method Details

#keywordObject

Internal: return the keyword

Returns a String



73
74
75
# File 'lib/kjess/protocol.rb', line 73

def keyword
  self.class.keyword
end

#parse_options_to_args(opts) ⇒ Object

Internal: callback that child classes may use to further parse the initialization arguments

Returns Array



57
# File 'lib/kjess/protocol.rb', line 57

def parse_options_to_args( opts ); end

#to_protocolObject

Internal: Convert the object to its protocol serialized format.

This may be overridden in child classes

Return a String



64
65
66
67
68
# File 'lib/kjess/protocol.rb', line 64

def to_protocol
  s = keyword
  s += " #{args.join(' ')}" unless args.empty?
  s += CRLF
end