Class: QML::Data::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, #qt_models

Instance Method Summary collapse

Methods inherited from ListModel

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

Methods included from QML::Dispatchable

#later

Methods included from Wrappable

#create_wrapper

Constructor Details

#initialize(*columns) ⇒ QueryModel

Returns a new instance of QueryModel.

Parameters:

  • columns (Array<Symbol|String>)


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

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

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



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

def count
  @count
end

Instance Method Details

#[](index) ⇒ Object



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

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:

  • (Array)


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

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)


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

def query_count
  fail ::NotImplementedError
end

#updateObject

Updates the model.



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

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