Class: Rotulus::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/rotulus/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, values = {}) ⇒ Record

Creates a new Record instance representing the first or last record of a :page wherein the :values include the ordered column values of the AR record. This instance serves as the reference point in generating the SQL query to fetch the the page’s previous or next page’s records. That is, the first record’s values of a page are used in the WHERE condition to fetch the previous page’s records and the last record’s values are used to fetch next page’s records.

Parameters:

  • page (Rotulus::Page)

    the Page instance

  • values (Hash) (defaults to: {})

    the ordered column values of an AR record



14
15
16
17
# File 'lib/rotulus/record.rb', line 14

def initialize(page, values = {})
  @page = page
  @values = normalize_values(values || {})
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



3
4
5
# File 'lib/rotulus/record.rb', line 3

def page
  @page
end

#valuesObject (readonly)

Returns the value of attribute values.



3
4
5
# File 'lib/rotulus/record.rb', line 3

def values
  @values
end

Instance Method Details

#sql_seek_condition(direction) ⇒ String

Get the records preceding or succeeding this record in respect to the sort direction of the ordered columns.

Parameters:

  • direction (Symbol)

    ‘:next` to fetch succeeding records otherwise, `:prev`.

Returns:

  • (String)

    SQL ‘where’ condition



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rotulus/record.rb', line 25

def sql_seek_condition(direction)
  page.order_columns.reverse.reduce(nil) do |sql, column|
    column_seek_sql = ColumnConditionBuilder.new(
      column,
      values[column.prefixed_name],
      direction,
      sql
    ).build

    parenthesize = !sql.nil? && !column.leftmost?
    parenthesize ? "(#{column_seek_sql})" : column_seek_sql
  end.squish
end

#stateString

Generate a ‘state’ so we can detect whether the record values changed/ for integrity check. e.g. Record values prior to encoding in the cursor vs. the decoded values from an encoded cursor token.

Returns:

  • (String)

    the hashed state



44
45
46
# File 'lib/rotulus/record.rb', line 44

def state
  Digest::MD5.hexdigest(values.map { |k, v| "#{k}:#{v}" }.join('~'))
end