Module: Bones::RPC::Protocol::AdapterHelper::ClassMethods

Defined in:
lib/bones/rpc/protocol/adapter_helper.rb

Overview

Provides a DSL for defining struct-like fields for building messages for the Mongo Wire.

Examples:

class Command
  extend Message::ClassMethods

  int32 :length
end

Command.fields # => [:length]
command = Command.new
command.length = 12
command.serialize_length("") # => "\f\x00\x00\x00"

Instance Method Summary collapse

Instance Method Details

#any(name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bones/rpc/protocol/adapter_helper.rb', line 76

def any(name)
  attr_accessor name

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def process_#{name}
      #{name}
    end
  RUBY

  fields << name
end

#binary(name) ⇒ Object

Declare a binary field.

Examples:

class Query < Message
  binary :collection
end

Parameters:

  • name (String)

    the name of this field



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bones/rpc/protocol/adapter_helper.rb', line 96

def binary(name)
  attr_accessor name

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def process_#{name}
      #{name}
    end
  RUBY

  fields << name
end

#deserialize(adapter, buffer = "") ⇒ Object



64
65
66
67
68
69
# File 'lib/bones/rpc/protocol/adapter_helper.rb', line 64

def deserialize(adapter, buffer="")
  adapter = Adapter.get(adapter)
  message = adapter.deserialize(buffer)
  message.shift
  new(adapter, *message)
end

#fieldsArray

Returns the fields defined for this message.

Returns:

  • (Array)

    the fields defined for this message



72
73
74
# File 'lib/bones/rpc/protocol/adapter_helper.rb', line 72

def fields
  @fields ||= []
end

#finalizeObject

Declares the message class as complete, and defines its serialization method from the declared fields.



142
143
144
145
146
147
148
149
150
# File 'lib/bones/rpc/protocol/adapter_helper.rb', line 142

def finalize
  class_eval <<-EOS, __FILE__, __LINE__ + 1
    def process
      list = []
      #{fields.map { |f| "list << process_#{f}" }.join("\n")}
      list
    end
  EOS
end

#integer(name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bones/rpc/protocol/adapter_helper.rb', line 108

def integer(name)
  attr_accessor name

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      @#{name} ||= 0
    end

    def process_#{name}
      #{name}
    end
  RUBY

  fields << name
end

#list(name) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bones/rpc/protocol/adapter_helper.rb', line 124

def list(name)
  attr_accessor name

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      @#{name} ||= []
    end

    def process_#{name}
      #{name}
    end
  RUBY

  fields << name
end