Class: PackedModel::List
- Inherits:
-
Object
- Object
- PackedModel::List
- Includes:
- Enumerable
- Defined in:
- lib/packed_model/list.rb
Instance Method Summary collapse
- #<<(row) ⇒ Object
-
#[](idx) ⇒ Object
the data in each row is only unpacked when it is accessed from here.
- #[]=(idx, row) ⇒ Object
- #bytesize ⇒ Object
- #each ⇒ Object
-
#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.
-
#initialize(row_class, values = nil) ⇒ List
constructor
A new instance of List.
- #pack ⇒ Object
- #remove(idx) ⇒ Object
-
#repack ⇒ Object
repacks the buffer.
- #size ⇒ Object
Constructor Details
#initialize(row_class, values = nil) ⇒ List
Returns a new instance of List.
5 6 7 8 9 |
# 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 @buffer = values end |
Instance Method Details
#<<(row) ⇒ Object
24 25 26 27 |
# File 'lib/packed_model/list.rb', line 24 def <<(row) row.changed! rows << row end |
#[](idx) ⇒ Object
the data in each row is only unpacked when it is accessed from here
12 13 14 15 16 17 |
# File 'lib/packed_model/list.rb', line 12 def [](idx) if (row = rows[idx]).is_a?(String) row = rows[idx] = @row_class.new(row) end row end |
#[]=(idx, row) ⇒ Object
19 20 21 22 |
# File 'lib/packed_model/list.rb', line 19 def []=(idx, row) row.changed! rows[idx] = row end |
#bytesize ⇒ Object
83 84 85 |
# File 'lib/packed_model/list.rb', line 83 def bytesize row_bytesize * size end |
#each ⇒ Object
87 88 89 90 91 |
# File 'lib/packed_model/list.rb', line 87 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
69 70 71 72 73 74 75 76 77 |
# File 'lib/packed_model/list.rb', line 69 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 |
#pack ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/packed_model/list.rb', line 34 def pack return repack if @must_repack return @buffer unless @rows @buffer ||= '' 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
29 30 31 32 |
# File 'lib/packed_model/list.rb', line 29 def remove(idx) @must_repack = true rows[idx] = nil end |
#repack ⇒ Object
repacks the buffer
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/packed_model/list.rb', line 51 def repack @buffer = '' 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 |
#size ⇒ Object
79 80 81 |
# File 'lib/packed_model/list.rb', line 79 def size rows.size end |