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:



12
13
14
15
16
# File 'lib/order_query/point.rb', line 12

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.



7
8
9
# File 'lib/order_query/point.rb', line 7

def record
  @record
end

#spaceObject (readonly)

Returns the value of attribute space.



7
8
9
# File 'lib/order_query/point.rb', line 7

def space
  @space
end

Instance Method Details

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

Parameters:

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

    choose if the given scope should include or not the record, default not to include it (strict true)

Returns:

  • (ActiveRecord::Relation)


36
37
38
# File 'lib/order_query/point.rb', line 36

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

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

Parameters:

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

    choose if the given scope should include or not the record, default not to include it (strict true)

Returns:

  • (ActiveRecord::Relation)


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

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

#inspectObject



63
64
65
# File 'lib/order_query/point.rb', line 63

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

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

Returns:

  • (ActiveRecord::Base)


20
21
22
# File 'lib/order_query/point.rb', line 20

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

#positionInteger

Returns counting from 1.

Returns:

  • (Integer)

    counting from 1



30
31
32
# File 'lib/order_query/point.rb', line 30

def position
  space.count - after.count
end

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

Returns:

  • (ActiveRecord::Base)


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

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)

    choose if the given scope should include or not the record, default not to include it (strict true)

Returns:

  • (ActiveRecord::Relation)


49
50
51
52
53
54
55
56
57
# File 'lib/order_query/point.rb', line 49

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(cond) ⇒ Object



59
60
61
# File 'lib/order_query/point.rb', line 59

def value(cond)
  record.send(cond.name)
end