Class: QML::QueryModel

Inherits:
ListModel show all
Defined in:
lib/qml/data/query_model.rb

Overview

QueryModel provides a list model implementation with database backends like ActiveRecord.

Defined Under Namespace

Classes: Cache

Instance Attribute Summary collapse

Attributes inherited from ListModel

#columns

Instance Method Summary collapse

Methods inherited from ListModel

#each, #inserting, #moving, #removing, #resetting, #to_qml

Constructor Details

#initialize(*columns) ⇒ QueryModel

Returns a new instance of QueryModel.

Parameters:



7
8
9
10
11
12
# File 'lib/qml/data/query_model.rb', line 7

def initialize(*columns)
  super
  @count = 0
  @caches = []
  update
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



4
5
6
# File 'lib/qml/data/query_model.rb', line 4

def count
  @count
end

Instance Method Details

#[](index) ⇒ Object



14
15
16
17
18
# File 'lib/qml/data/query_model.rb', line 14

def [](index)
  block_index = index / CACHE_SIZE
  cache = @caches.find { |c| c.block_index == block_index } || add_cache(block_index)
  cache.items[index % CACHE_SIZE]
end

#query(offset, count) ⇒ Array

This method is abstract.

Queries a block of records. The results are chached.

Parameters:

  • offset (Integer)
  • count (Integer)

Returns:



41
42
43
# File 'lib/qml/data/query_model.rb', line 41

def query(offset, count)
  fail ::NotImplementedError
end

#query_countInteger

This method is abstract.

Queries the count of the records. Called when #update is called and the result is set as the #count of the model.

Returns:

  • (Integer)


32
33
34
# File 'lib/qml/data/query_model.rb', line 32

def query_count
  fail ::NotImplementedError
end

#updateObject

Updates the model.



21
22
23
24
25
26
# File 'lib/qml/data/query_model.rb', line 21

def update
  @caches = []
  resetting do
    @count = query_count
  end
end