Class: Palm::WabaRecord

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

Constant Summary collapse

WRITE_TYPES =
{
	# 	 															Value  || Default
	:string=>	lambda{|io,v| io.write_string(v||''		)},
	:int=>		lambda{|io,v| io.write_int(		v||0		)},
	:byte=>		lambda{|io,v| io.write_byte(	v||0		)},
	:short=>	lambda{|io,v| io.write_short(	v||0		)},
	:boolean=>lambda{|io,v| io.write_bool( 	v||false)},
}
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 	},
}

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

#initialize, #packed_attributes, #packed_attributes=

Constructor Details

This class inherits a constructor from Palm::Record

Class Method Details

.class_id(value = nil) ⇒ Object



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

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

.field(name, type) ⇒ Object



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

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

.fieldsObject



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

def fields
	@fields.dup
end

Instance Method Details

#class_idObject



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

def class_id
	self.class.class_id
end

#read(waba_io) ⇒ Object

Assumes that the class_id has already been read off the stream



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

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



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

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