Module: Rumai::IXP::Struct
Overview
A serializable 9P2000 data structure.
Defined Under Namespace
Classes: ClassField, Field, Integer8Field, StringField
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
-
.included(target) ⇒ Object
Provides a convenient DSL (for defining fields) to all objects which include this module.
Instance Method Summary collapse
-
#initialize(field_values = {}) ⇒ Object
Allows field values to be initialized via the constructor.
-
#load_9p(stream) ⇒ Object
Populates this object with information from the given 9P2000 byte stream.
-
#to_9p ⇒ Object
Transforms this object into a string of 9P2000 bytes.
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
46 47 48 |
# File 'lib/rumai/ixp/message.rb', line 46 def fields @fields end |
Class Method Details
.included(target) ⇒ Object
Provides a convenient DSL (for defining fields) to all objects which include this module.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rumai/ixp/message.rb', line 80 def self.included target class << target ## # Returns a list of fields which compose this Struct. # def fields @fields ||= if superclass.respond_to? :fields superclass.fields.dup else [] end end ## # Defines a new field in this Struct. # # @param args # arguments for {Field.new} # def field name, format = nil, *args klass = Field.factory(format) field = klass.new(name.to_sym, format, *args) # register field as being part of this structure fields << field # provide accessor methods to field values self.class_eval <<-EOS, __FILE__, __LINE__ def #{field.name} @values[#{field.name.inspect}] end def #{field.name}= value @values[#{field.name.inspect}] = value end EOS field end ## # Creates a new instance of this class from the # given 9P2000 byte stream and returns the instance. # def from_9p stream, msg_class = self msg = msg_class.new msg.load_9p(stream) msg end end end |
Instance Method Details
#initialize(field_values = {}) ⇒ Object
Allows field values to be initialized via the constructor.
54 55 56 57 |
# File 'lib/rumai/ixp/message.rb', line 54 def initialize field_values = {} @fields = self.class.fields @values = field_values end |
#load_9p(stream) ⇒ Object
Populates this object with information from the given 9P2000 byte stream.
70 71 72 73 74 |
# File 'lib/rumai/ixp/message.rb', line 70 def load_9p stream @fields.each do |f| f.load_9p stream, @values end end |
#to_9p ⇒ Object
Transforms this object into a string of 9P2000 bytes.
62 63 64 |
# File 'lib/rumai/ixp/message.rb', line 62 def to_9p @fields.map {|f| f.to_9p(@values) }.join end |