Module: Rumai::IXP::Struct

A serializable 9P2000 data structure.

Attributes

Instance Attributes

fields [RW] public

Returns the value of attribute fields.

Public Visibility

Public Class Method Summary

included(aTarget)

Provides a convenient DSL (for defining fields) to all objects which include this module.

Public Instance Method Summary

#load_9p(aStream)

Populates this object with information from the given 9P2000 byte stream.

#to_9p

Transforms this object into a string of 9P2000 bytes.

Public Class Method Details

included

public included(aTarget)

Provides a convenient DSL (for defining fields) to all objects which include this module.

[View source]


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
# File 'lib/rumai/ixp/message.rb', line 63

def self.included aTarget
  class << aTarget
    # 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.
    # aArgs:: arguments for Field.new()
    def field aName, aFormat = nil, *aArgs
      c = Field.factory(aFormat)
      f = c.new(aName.to_sym, aFormat, *aArgs)
      fields << f # register field as being part of this structure

      # provide accessor methods to field values
      self.class_eval %{
        def #{f.name}
          @values[#{f.name.inspect}]
        end

        def #{f.name}= aValue
          @values[#{f.name.inspect}] = aValue
        end
      }

      return f
    end

    # Creates a new instance of this class from the
    # given 9P2000 byte stream and returns the instance.
    def from_9p aStream, aMsgClass = self
      msg = aMsgClass.new
      msg.load_9p(aStream)
      msg
    end
  end
end

Public Instance Method Details

load_9p

public load_9p(aStream)

Populates this object with information from the given 9P2000 byte stream.

[View source]


55
56
57
58
59
# File 'lib/rumai/ixp/message.rb', line 55

def load_9p aStream
  @fields.each do |f|
    f.load_9p aStream, @values
  end
end

to_9p

public to_9p

Transforms this object into a string of 9P2000 bytes.

[View source]


49
50
51
# File 'lib/rumai/ixp/message.rb', line 49

def to_9p
  @fields.inject('') {|s,f| s << f.to_9p(@values) }
end