Class: OrderQuery::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/order_query/point.rb

Overview

Search around a record in an order space

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, space) ⇒ Point

Returns a new instance of Point.

Parameters:



15
16
17
18
19
# File 'lib/order_query/point.rb', line 15

def initialize(record, space)
  @record    = record
  @space     = space
  @where_sql = SQL::Where.new(self)
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



10
11
12
# File 'lib/order_query/point.rb', line 10

def record
  @record
end

#spaceObject (readonly)

Returns the value of attribute space.



10
11
12
# File 'lib/order_query/point.rb', line 10

def space
  @space
end

Instance Method Details

#after(strict = true) ⇒ ActiveRecord::Relation

Parameters:

  • strict (true, false) (defaults to: true)

    if false, the given scope will include the record at this point.

Returns:

  • (ActiveRecord::Relation)


41
42
43
# File 'lib/order_query/point.rb', line 41

def after(strict = true)
  side :after, strict
end

#before(strict = true) ⇒ ActiveRecord::Relation

Parameters:

  • strict (true, false) (defaults to: true)

    if false, the given scope will include the record at this point.

Returns:

  • (ActiveRecord::Relation)


48
49
50
# File 'lib/order_query/point.rb', line 48

def before(strict = true)
  side :before, strict
end

#inspectObject



77
78
79
# File 'lib/order_query/point.rb', line 77

def inspect
  "#<OrderQuery::Point @record=#{@record.inspect} @space=#{@space.inspect}>"
end

#next(loop = true) ⇒ ActiveRecord::Base

Parameters:

  • loop (true, false) (defaults to: true)

    if true, loops as if the last and the first records were adjacent, unless there is only one record.

Returns:

  • (ActiveRecord::Base)


24
25
26
# File 'lib/order_query/point.rb', line 24

def next(loop = true)
  unless_record_eq after.first || (first if loop)
end

#positionInteger

Returns counting from 1.

Returns:

  • (Integer)

    counting from 1



34
35
36
# File 'lib/order_query/point.rb', line 34

def position
  space.count - after.count
end

#previous(loop = true) ⇒ ActiveRecord::Base

Returns:

  • (ActiveRecord::Base)


29
30
31
# 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) ⇒ ActiveRecord::Relation

Parameters:

  • side (:before, :after)
  • strict (true, false) (defaults to: true)

    if false, the given scope will include the record at this point.

Returns:

  • (ActiveRecord::Relation)


56
57
58
59
60
61
62
63
64
# 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) ⇒ Object

Parameters:



67
68
69
70
71
72
73
74
75
# 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.inspect}. "\
         'Set the `nulls` option to :first or :last.'
  end
  v
end