class OrderQuery::Point

Search around a record in an order space

Attributes

record[R]
space[R]

Public Class Methods

new(record, space) click to toggle source

@param [ActiveRecord::Base] record @param [OrderQuery::Space] space

# File lib/order_query/point.rb, line 15
def initialize(record, space)
  @record    = record
  @space     = space
  @where_sql = SQL::Where.new(self)
end

Public Instance Methods

after(strict = true) click to toggle source

@param [true, false] strict if false, the given scope will include the

record at this point.

@return [ActiveRecord::Relation]

# File lib/order_query/point.rb, line 41
def after(strict = true)
  side :after, strict
end
before(strict = true) click to toggle source

@param [true, false] strict if false, the given scope will include the

record at this point.

@return [ActiveRecord::Relation]

# File lib/order_query/point.rb, line 48
def before(strict = true)
  side :before, strict
end
inspect() click to toggle source
# File lib/order_query/point.rb, line 77
def inspect
  "#<OrderQuery::Point @record=#{@record.send(inspect_method)} @space=#{@space.inspect}>"
end
next(loop = true) click to toggle source

@param [true, false] loop if true, loops as if the last and the first

records were adjacent, unless there is only one record.

@return [ActiveRecord::Base]

# File lib/order_query/point.rb, line 24
def next(loop = true)
  unless_record_eq after.first || (first if loop)
end
position() click to toggle source

@return [Integer] counting from 1

# File lib/order_query/point.rb, line 34
def position
  space.count - after.count
end
previous(loop = true) click to toggle source

@return [ActiveRecord::Base]

# File lib/order_query/point.rb, line 29
def previous(loop = true)
  unless_record_eq before.first || (last if loop)
end
side(side, strict = true) click to toggle source

@param [:before, :after] side @param [true, false] strict if false, the given scope will include the

record at this point.

@return [ActiveRecord::Relation]

# File lib/order_query/point.rb, line 56
def side(side, strict = true)
  query, query_args = @where_sql.build(side, strict)
  scope = if side == :after
            space.scope
          else
            space.scope_reverse
          end
  scope.where(query, *query_args)
end
value(column) click to toggle source

@param column [Column]

# File lib/order_query/point.rb, line 67
def value(column)
  v = record.send(column.name)
  if v.nil? && !column.nullable?
    fail Errors::NonNullableColumnIsNullError,
         "Column #{column.inspect} is NULL on record #{@record.send(inspect_method)}. "\
         'Set the `nulls` option to :first or :last.'
  end
  v
end

Protected Instance Methods

unless_record_eq(rec) click to toggle source

@param [ActiveRecord::Base] rec @return [ActiveRecord::Base, nil] rec unless rec == @record

# File lib/order_query/point.rb, line 85
def unless_record_eq(rec)
  rec unless rec == @record
end

Private Instance Methods

inspect_method() click to toggle source
# File lib/order_query/point.rb, line 90
def inspect_method
  Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("7.2.0") ? :full_inspect
                                                                               : :inspect
end