Class: PEROBS::IDListPage
- Inherits:
-
Object
- Object
- PEROBS::IDListPage
- Defined in:
- lib/perobs/IDListPage.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
Returns the value of attribute record.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #check ⇒ Object
- #delete(max_id) ⇒ Object
- #include?(id) ⇒ Boolean
-
#initialize(page_file, record, uid, values = []) ⇒ IDListPage
constructor
A new instance of IDListPage.
- #insert(id) ⇒ Object
- #is_full? ⇒ Boolean
- #length ⇒ Object
- #save ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(page_file, record, uid, values = []) ⇒ IDListPage
Returns a new instance of IDListPage.
35 36 37 38 39 40 41 |
# File 'lib/perobs/IDListPage.rb', line 35 def initialize(page_file, record, uid, values = []) @page_file = page_file @record = record @uid = uid @values = values @record.page_entries = @values.length end |
Instance Attribute Details
#record ⇒ Object
Returns the value of attribute record.
33 34 35 |
# File 'lib/perobs/IDListPage.rb', line 33 def record @record end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
32 33 34 |
# File 'lib/perobs/IDListPage.rb', line 32 def uid @uid end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
32 33 34 |
# File 'lib/perobs/IDListPage.rb', line 32 def values @values end |
Class Method Details
.load(page_file, uid, ref) ⇒ Object
43 44 45 |
# File 'lib/perobs/IDListPage.rb', line 43 def IDListPage::load(page_file, uid, ref) page_file.load(uid, ref) end |
Instance Method Details
#check ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/perobs/IDListPage.rb', line 89 def check last_value = nil @values.each_with_index do |v, i| if last_value && last_value >= v raise RuntimeError, "The values #{last_value} and #{v} must be " + "strictly ascending: #{@values.inspect}" end last_value = v end end |
#delete(max_id) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/perobs/IDListPage.rb', line 77 def delete(max_id) a = [] @values.delete_if { |v| v > max_id ? a << v : false } unless a.empty? @record.page_entries = @values.length @page_file.mark_page_as_modified(self) end a end |
#include?(id) ⇒ Boolean
73 74 75 |
# File 'lib/perobs/IDListPage.rb', line 73 def include?(id) !(v = @values.bsearch { |v| v >= id }).nil? && v == id end |
#insert(id) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/perobs/IDListPage.rb', line 59 def insert(id) if is_full? raise ArgumentError, "IDListPage is already full" end index = @values.bsearch_index { |v| v >= id } || @values.length # If the value isn't stored already, insert it. if @values[index] != id @values.insert(index, id) @record.page_entries = @values.length @page_file.mark_page_as_modified(self) end end |
#is_full? ⇒ Boolean
47 48 49 |
# File 'lib/perobs/IDListPage.rb', line 47 def is_full? @values.length >= @page_file.page_size end |
#length ⇒ Object
51 52 53 |
# File 'lib/perobs/IDListPage.rb', line 51 def length @values.length end |
#save ⇒ Object
55 56 57 |
# File 'lib/perobs/IDListPage.rb', line 55 def save @page_file.save_page(self) end |
#to_s ⇒ Object
100 101 102 |
# File 'lib/perobs/IDListPage.rb', line 100 def to_s "[ #{@values.join(', ')} ]" end |