Module: Binary::Protocol::ClassMethods
- Defined in:
- lib/binary/protocol.rb
Overview
Provides a DSL for defining struct-like fields for building binary messages.
Instance Method Summary collapse
- #deserialize(buffer = nil, &block) ⇒ Object
-
#fields ⇒ Array
The fields defined for this message.
-
#finalize ⇒ Object
Declares the protocol class as complete, and defines its serialization method from the declared fields.
-
#serialization ⇒ Array
The methods to run in order for serialiation.
-
#string(name, options = {}) ⇒ Object
Declare a string field.
-
#stringz(name, options = {}) ⇒ Object
Declare a null terminated string field.
Instance Method Details
#deserialize(buffer = nil, &block) ⇒ Object
214 215 216 217 218 219 220 221 222 |
# File 'lib/binary/protocol.rb', line 214 def deserialize(buffer = nil, &block) if block_given? re_define_method(:deserialize, &block) else = allocate .deserialize(buffer) end end |
#fields ⇒ Array
Returns the fields defined for this message.
77 78 79 |
# File 'lib/binary/protocol.rb', line 77 def fields @fields ||= [] end |
#finalize ⇒ Object
Declares the protocol class as complete, and defines its serialization method from the declared fields.
204 205 206 207 208 209 210 211 212 |
# File 'lib/binary/protocol.rb', line 204 def finalize class_eval <<-RUBY, __FILE__, __LINE__ + 1 def serialize(buffer = "") #{serialization.map { |command| "#{command}(buffer)" }.join("\n")} buffer end alias to_s serialize RUBY end |
#serialization ⇒ Array
Returns the methods to run in order for serialiation.
72 73 74 |
# File 'lib/binary/protocol.rb', line 72 def serialization @serialization ||= [] end |
#string(name, options = {}) ⇒ Object
Declare a string field.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/binary/protocol.rb', line 138 def string(name, = {}) if .key?(:always) __define_always__(name, [:always]) else if .key?(:default) __define_default__(name, [:default]) else attr_accessor name end class_eval <<-RUBY, __FILE__, __LINE__ + 1 def deserialize_#{name}(buffer) raise NotImplementedError end RUBY end class_eval <<-RUBY, __FILE__, __LINE__ + 1 def serialize_#{name}(buffer) buffer << #{name} end RUBY serialization << :"serialize_#{name}" fields << name end |
#stringz(name, options = {}) ⇒ Object
Declare a null terminated string field.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/binary/protocol.rb', line 174 def stringz(name, = {}) if .key?(:always) __define_always__(name, [:always]) else if .key?(:default) __define_default__(name, [:default]) else attr_accessor name end class_eval <<-RUBY, __FILE__, __LINE__ + 1 def deserialize_#{name}(buffer) raise NotImplementedError end RUBY end class_eval <<-RUBY, __FILE__, __LINE__ + 1 def serialize_#{name}(buffer) buffer << #{name} buffer << 0 end RUBY serialization << :"serialize_#{name}" fields << name end |