Class: Palm::WabaRecord

Inherits:
Record
  • Object
show all
Defined in:
lib/palm/waba_record.rb

Constant Summary

Constants inherited from Record

Record::RECORD_ATTRIBUTE_CODES

Instance Attribute Summary

Attributes inherited from Record

#archive, #category, #deleted, #dirty, #expunged, #private, #record_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#packed_attributes, #packed_attributes=

Constructor Details

#initializeWabaRecord

Returns a new instance of WabaRecord.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/palm/waba_record.rb', line 5

def initialize
	@write_types = {
		:string=>	lambda{|io,v| io.write_string v},
		:int=>		lambda{|io,v| io.write_int 		v},
		:byte=>		lambda{|io,v| io.write_byte 	v},
		:short=>	lambda{|io,v| io.write_short 	v},
		:boolean=>lambda{|io,v| io.write_bool 	v},
	}

	@read_types = {
		:string=>	lambda{|io| 	io.get_string },
		:int=>		lambda{|io| 	io.get_int 		},
		:byte=>		lambda{|io| 	io.get_byte		},
		:short=>	lambda{|io| 	io.get_short	},
		:boolean=>lambda{|io| 	io.get_bool 	},
	}
end

Class Method Details

.class_id(value = nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/palm/waba_record.rb', line 34

def class_id(value=nil)
	if value
		@class_id = value
	else
		@class_id
	end
end

.field(name, type) ⇒ Object



24
25
26
27
28
# File 'lib/palm/waba_record.rb', line 24

def field(name,type)	
   # Add a new class method to for each trait.
	attr_accessor name
	(@fields ||= []) << WabaField.new(name,type)
end

.fieldsObject



30
31
32
# File 'lib/palm/waba_record.rb', line 30

def fields
	@fields.dup
end

Instance Method Details

#class_idObject



43
44
45
# File 'lib/palm/waba_record.rb', line 43

def class_id
	self.class.class_id
end

#read(waba_io) ⇒ Object

Assumes that the class_id has already been read off the stream



48
49
50
51
52
53
# File 'lib/palm/waba_record.rb', line 48

def read(waba_io)
	self.class.fields.each do |f|
		instance_variable_set "@#{f.name}", @read_types[f.type].call(waba_io)
	end
	self
end

#write(waba_io) ⇒ Object



55
56
57
58
59
60
# File 'lib/palm/waba_record.rb', line 55

def write(waba_io)
	self.class.fields.each do |f|
		@write_types[f.type].call(waba_io, instance_variable_get("@#{f.name}"))
	end
	self
end