1 # Used for mapping LWES events in in an ESF file to a Ruby object.
2 # LWES::Event-derived classes are more memory efficient if your event
3 # definitions have many unused fields.
5 # LWES::TypeDB.create_classes! with +:sparse+ set to +true+ will create
6 # classes subclassed from LWES::Event instead of LWES::Struct.
8 # For users unable to use LWES::Listener, LWES::Event.parse may also be
9 # used with UDPSocket (distributed with Ruby) to parse events:
11 # receiver = UDPSocket.new
12 # receiver.bind(nil, port)
13 # buffer, addr = receiver.recvfrom(65536)
14 # event = LWES::Event.parse(buffer)
17 SYM2ATTR = Hash.new { |h,k| h[k] = k.to_s.freeze } # :nodoc:
19 # used to cache classes for LWES::Event.parse
20 CLASSES = {} # :nodoc:
21 extend LWES::ClassMaker
23 # There is no need to call this method directly. use
24 # LWES::TypeDB.create_classes! with the +:sparse+ flag set to +true+.
26 # This takes the same options as LWES::Struct.new.
27 def self.subclass(options, &block)
30 klass, name, event_def = class_for(options, dump)
32 set_constants(tmp, db, klass, name, options)
33 tmp.class_eval(&block) if block_given?
35 meta = dump[:MetaEventInfo] || []
36 methods = meta + event_def
37 methods = methods.inject("") do |str, (k,_)|
38 str << "def #{k}; self[:#{k}]; end\n"
39 str << "def #{k}= val; self[:#{k}] = val; end\n"
41 methods << "def initialize(src = nil); merge!(DEFAULTS); super; end\n"
42 tmp.class_eval methods
46 # returns a human-readable representation of the event
49 if LWES::Event == klass
50 "#<#{klass}:#{to_hash.inspect}>"
52 "#<#{klass}(event:#{klass.const_get(:NAME)}):#{to_hash.inspect}>"
56 # overwrites the values of the existing event with those given in +src+
58 src.to_hash.each { |k,v| self[k] = v }
62 alias_method :initialize_copy, :merge!
64 # duplicates the given event and overwrites the copy with values given
72 # Initializes a new LWES::Event. If +src+ is given, it must be an
73 # LWES::Event or hash which the new event takes initial values from
74 def initialize(src = nil)