Module: Sequel::Plugins::Sequenceable::ClassMethods

Defined in:
lib/cortex_reaver/support/sequenceable.rb

Overview

Class methods

Constant Summary collapse

DEFAULT_SEQUENCE_ORDER =

Which column table to order this sequence by

:created_on
DEFAULT_SEQUENCE_REVERSE =
false
DEFAULT_WINDOW_SIZE =
16

Instance Method Summary collapse

Instance Method Details

#firstObject

Returns the first record in the sequence



26
27
28
# File 'lib/cortex_reaver/support/sequenceable.rb', line 26

def first
  sequence.first
end

#lastObject

Returns the last record in the sequence



31
32
33
# File 'lib/cortex_reaver/support/sequenceable.rb', line 31

def last
  sequence.last
end

#sequence(dataset = self.dataset) ⇒ Object

Returns the sequence dataset (optionally, restricted to dataset)



17
18
19
20
21
22
23
# File 'lib/cortex_reaver/support/sequenceable.rb', line 17

def sequence(dataset = self.dataset)
  if sequence_reverse
    dataset.reverse_order(sequence_order)
  else
    dataset.order(sequence_order)
  end
end

#sequence_orderObject

Returns the table column to order by



36
37
38
# File 'lib/cortex_reaver/support/sequenceable.rb', line 36

def sequence_order
  @sequence_order || DEFAULT_SEQUENCE_ORDER
end

#sequence_order=(order) ⇒ Object

Sets the table column to order by



41
42
43
# File 'lib/cortex_reaver/support/sequenceable.rb', line 41

def sequence_order=(order)
  @sequence_order = order
end

#sequence_reverseObject

Returns whether or not to reverse the order



46
47
48
# File 'lib/cortex_reaver/support/sequenceable.rb', line 46

def sequence_reverse
  @sequence_reverse || DEFAULT_SEQUENCE_REVERSE
end

#sequence_reverse=(reverse) ⇒ Object

Sets whether to reverse the sequence



51
52
53
# File 'lib/cortex_reaver/support/sequenceable.rb', line 51

def sequence_reverse=(reverse)
  @sequence_reverse = reverse
end

#window(page_offset, size = self.window_size) ⇒ Object

Returns an absolute window into this sequence, specified as an offset in window-sized increments from the beginning of the sequence. Hence window(0, 10) returns the first ten records in the sequence, window(1, 10) the next ten, and so on.

One can also specify the special page_offsets :first or :last, which return the first and last available windows.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cortex_reaver/support/sequenceable.rb', line 62

def window(page_offset, size = self.window_size)
  case page_offset
    when :first
      page_offset = 0
    when :last
      page_offset = window_count(size) - 1
    else
      page_offset = page_offset.to_i
  end

  # Don't ask for negative pages!
  page_offset = 0 if page_offset < 0

  # Calculate offset
  offset = page_offset * size

  # Limit dataset
  sequence.limit size, offset
end

#window_count(size = self.window_size) ⇒ Object

Returns the number of windows required to span this sequence



83
84
85
# File 'lib/cortex_reaver/support/sequenceable.rb', line 83

def window_count(size = self.window_size)
  (Float(sequence.count) / size).ceil
end

#window_sizeObject

Returns the size of a window into this sequence



88
89
90
# File 'lib/cortex_reaver/support/sequenceable.rb', line 88

def window_size
  @window_size || DEFAULT_WINDOW_SIZE
end

#window_size=(size) ⇒ Object

Sets the size of a window into this sequence



93
94
95
# File 'lib/cortex_reaver/support/sequenceable.rb', line 93

def window_size=(size)
  @window_size = size
end