Class: OrderQuery::Point
- Inherits:
-
Object
- Object
- OrderQuery::Point
- Defined in:
- lib/order_query/point.rb
Overview
Search around a record in an order space
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#space ⇒ Object
readonly
Returns the value of attribute space.
Instance Method Summary collapse
- #after(strict = true) ⇒ ActiveRecord::Relation
- #before(strict = true) ⇒ ActiveRecord::Relation
-
#initialize(record, space) ⇒ Point
constructor
A new instance of Point.
- #inspect ⇒ Object
- #next(loop = true) ⇒ ActiveRecord::Base
-
#position ⇒ Integer
Counting from 1.
- #previous(loop = true) ⇒ ActiveRecord::Base
- #side(side, strict = true) ⇒ ActiveRecord::Relation
- #value(column) ⇒ Object
Constructor Details
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
10 11 12 |
# File 'lib/order_query/point.rb', line 10 def record @record end |
#space ⇒ Object (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
41 42 43 |
# File 'lib/order_query/point.rb', line 41 def after(strict = true) side :after, strict end |
#before(strict = true) ⇒ ActiveRecord::Relation
48 49 50 |
# File 'lib/order_query/point.rb', line 48 def before(strict = true) side :before, strict end |
#inspect ⇒ Object
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
24 25 26 |
# File 'lib/order_query/point.rb', line 24 def next(loop = true) unless_record_eq after.first || (first if loop) end |
#position ⇒ Integer
Returns 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
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
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
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 |