Class: PackedModel::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/packed_model/list.rb

Instance Method Summary collapse

Constructor Details

#initialize(row_class, values = nil) ⇒ List

Returns a new instance of List.



5
6
7
8
9
10
# File 'lib/packed_model/list.rb', line 5

def initialize(row_class, values=nil)
  raise InvalidPackedModelException.new("row_class must be fixed width") unless row_class.fixed_width?
  @row_class = row_class
  values.force_encoding(Encoding::BINARY) if values.respond_to?(:force_encoding)
  @buffer = values
end

Instance Method Details

#<<(row) ⇒ Object



25
26
27
28
# File 'lib/packed_model/list.rb', line 25

def <<(row)
  row.changed!
  rows << row
end

#[](idx) ⇒ Object

the data in each row is only unpacked when it is accessed from here



13
14
15
16
17
18
# File 'lib/packed_model/list.rb', line 13

def [](idx)
  if (row = rows[idx]).is_a?(String)
    row = rows[idx] = @row_class.new(row)
  end
  row
end

#[]=(idx, row) ⇒ Object



20
21
22
23
# File 'lib/packed_model/list.rb', line 20

def []=(idx, row)
  row.changed!
  rows[idx] = row
end

#bytesizeObject



84
85
86
# File 'lib/packed_model/list.rb', line 84

def bytesize
  row_bytesize * size
end

#eachObject



88
89
90
91
92
# File 'lib/packed_model/list.rb', line 88

def each
  self.size.times do |idx|
    yield self[idx]
  end
end

#find_in_buffer(val) ⇒ Object

to search the buffer the first field must be a marker and the 2nd is the what you are searching for Example: class TestSearchableModel < PackedModel::Base

  attribute :magic, :type => :marker, :value => 20130502
  attribute :id, :type => :integer
  attribute :name, :type => :char, :size => 20
end


70
71
72
73
74
75
76
77
78
# File 'lib/packed_model/list.rb', line 70

def find_in_buffer(val)
  return nil unless @buffer
  @results ||= {}
  return @results[val] if @results.has_key?(val)
  index = @buffer.index search_string(val)
  return (@results[val] = nil) unless index
  index = index / row_bytesize
  @results[val] = self[index]
end

#packObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/packed_model/list.rb', line 35

def pack
  return repack if @must_repack
  return @buffer unless @rows
  @buffer ||= ''.force_encoding(Encoding::BINARY)
  start = 0
  @rows.each_with_index do |row, idx|
    # only updated dirty rows
    if ! row.is_a?(String) && row.changed?
      @buffer[start...(start+row_bytesize)] = row.pack
      row.not_changed!
    end
    start += row_bytesize
  end
  @buffer
end

#remove(idx) ⇒ Object



30
31
32
33
# File 'lib/packed_model/list.rb', line 30

def remove(idx)
  @must_repack = true
  rows[idx] = nil
end

#repackObject

repacks the buffer



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/packed_model/list.rb', line 52

def repack
  @buffer = ''.force_encoding(Encoding::BINARY)
  self.each do |row|
    next unless row
    @buffer << row.pack
  end
  remove_instance_variable "@rows"
  remove_instance_variable("@must_repack") if defined?(@must_repack)
  remove_instance_variable("@results") if defined?(@results)
  @buffer
end

#sizeObject



80
81
82
# File 'lib/packed_model/list.rb', line 80

def size
  rows.size
end