Module: Moped::Protocol::Message::ClassMethods
- Defined in:
- lib/moped/protocol/message.rb
Overview
Provides a DSL for defining struct-like fields for building messages for the Mongo Wire.
Instance Method Summary collapse
-
#cstring(name) ⇒ Object
Declare a null terminated string field.
-
#document(name, options = {}) ⇒ Object
Declare a BSON Document field.
-
#fields ⇒ Array
The fields defined for this message.
-
#finalize ⇒ Object
Declares the message class as complete, and defines its serialization method from the declared fields.
-
#flags(name, flag_map = {}) ⇒ Object
Declare a flag field (32 bit signed integer).
-
#int32(name) ⇒ Object
Declare a 32 bit signed integer field.
-
#int64(name, options = {}) ⇒ Object
Declare a 64 bit signed integer field.
Instance Method Details
#cstring(name) ⇒ Object
Declare a null terminated string field.
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/moped/protocol/message.rb', line 118 def cstring(name) attr_accessor name class_eval <<-RUBY, __FILE__, __LINE__ + 1 def serialize_#{name}(buffer) buffer << #{name} buffer << 0 end RUBY fields << name end |
#document(name, options = {}) ⇒ Object
Declare a BSON Document field.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/moped/protocol/message.rb', line 153 def document(name, = {}) attr_accessor name if [:optional] class_eval <<-RUBY, __FILE__, __LINE__ + 1 def serialize_#{name}(buffer) buffer << #{name}.to_bson if #{name} end RUBY elsif [:type] == :array class_eval <<-RUBY, __FILE__, __LINE__ + 1 def serialize_#{name}(buffer) #{name}.each do |document| buffer << document.to_bson end end RUBY else class_eval <<-RUBY, __FILE__, __LINE__ + 1 def serialize_#{name}(buffer) buffer << #{name}.to_bson end RUBY end fields << name end |
#fields ⇒ Array
Returns the fields defined for this message.
106 107 108 |
# File 'lib/moped/protocol/message.rb', line 106 def fields @fields ||= [] end |
#finalize ⇒ Object
Declares the message class as complete, and defines its serialization method from the declared fields.
313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/moped/protocol/message.rb', line 313 def finalize class_eval <<-EOS, __FILE__, __LINE__ + 1 def serialize(buffer = "") start = buffer.bytesize #{fields.map { |f| "serialize_#{f}(buffer)" }.join("\n")} self.length = buffer.bytesize - start buffer[start, 4] = serialize_length("") buffer end alias :to_s :serialize EOS end |
#flags(name, flag_map = {}) ⇒ Object
Declare a flag field (32 bit signed integer)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/moped/protocol/message.rb', line 191 def flags(name, flag_map = {}) class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} @#{name} ||= [] end def #{name}=(flags) if flags.is_a? Numeric @#{name} = #{name}_from_int(flags) else @#{name} = flags end end def #{name}_as_int bits = 0 flags = self.#{name} #{flag_map.map { |flag, value| "bits |= #{value} if flags.include? #{flag.inspect}" }.join "\n"} bits end def #{name}_from_int(bits) flags = [] #{flag_map.map { |flag, value| "flags << #{flag.inspect} if #{value} & bits == #{value}" }.join "\n"} flags end def serialize_#{name}(buffer) buffer << [#{name}_as_int].pack(INT32_DECODE_STR) end def deserialize_#{name}(buffer) bits, = buffer.read(4).unpack(INT32_DECODE_STR) self.#{name} = bits end RUBY fields << name end |
#int32(name) ⇒ Object
Declare a 32 bit signed integer field.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/moped/protocol/message.rb', line 240 def int32(name) attr_writer name class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} @#{name} ||= 0 end def serialize_#{name}(buffer) buffer << [#{name}].pack(INT32_DECODE_STR) end def deserialize_#{name}(buffer) self.#{name}, = buffer.read(4).unpack(INT32_DECODE_STR) end RUBY fields << name end |
#int64(name, options = {}) ⇒ Object
Declare a 64 bit signed integer field.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/moped/protocol/message.rb', line 275 def int64(name, = {}) attr_writer name if [:type] == :array class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} @#{name} ||= [] end def serialize_#{name}(buffer) buffer << #{name}.pack(INT64_DECODE_ARRAY_STR) end def deserialize_#{name}(buffer) raise NotImplementedError end RUBY else class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} @#{name} ||= 0 end def serialize_#{name}(buffer) buffer << [#{name}].pack(INT64_DECODE_STR) end def deserialize_#{name}(buffer) self.#{name}, = buffer.read(8).unpack(INT64_DECODE_STR) end RUBY end fields << name end |