Class: Rotulus::Record
- Inherits:
-
Object
- Object
- Rotulus::Record
- Defined in:
- lib/rotulus/record.rb
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
-
#initialize(page, values = {}) ⇒ Record
constructor
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.
-
#sql_seek_condition(direction) ⇒ String
Get the records preceding or succeeding this record in respect to the sort direction of the ordered columns.
-
#state ⇒ String
Generate a ‘state’ so we can detect whether the record values changed/ for integrity check.
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.
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
#page ⇒ Object (readonly)
Returns the value of attribute page.
3 4 5 |
# File 'lib/rotulus/record.rb', line 3 def page @page end |
#values ⇒ Object (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.
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 |
#state ⇒ String
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.
44 45 46 |
# File 'lib/rotulus/record.rb', line 44 def state Digest::MD5.hexdigest(values.map { |k, v| "#{k}:#{v}" }.join('~')) end |