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 ⇒ ActiveRecord::Relation
- #before ⇒ ActiveRecord::Relation
-
#initialize(record, space) ⇒ Point
constructor
A new instance of Point.
- #inspect ⇒ Object
- #next(loop = true) ⇒ ActiveRecord::Base
- #position ⇒ Integer
- #previous(loop = true) ⇒ ActiveRecord::Base
- #side(side) ⇒ ActiveRecord::Relation
- #value(cond) ⇒ Object
Constructor Details
#initialize(record, space) ⇒ Point
Returns a new instance of Point.
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
#record ⇒ Object (readonly)
Returns the value of attribute record.
7 8 9 |
# File 'lib/order_query/point.rb', line 7 def record @record end |
#space ⇒ Object (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 ⇒ ActiveRecord::Relation
35 36 37 |
# File 'lib/order_query/point.rb', line 35 def after side :after end |
#before ⇒ ActiveRecord::Relation
40 41 42 |
# File 'lib/order_query/point.rb', line 40 def before side :before end |
#inspect ⇒ Object
64 65 66 |
# File 'lib/order_query/point.rb', line 64 def inspect "#<OrderQuery::Point @record=#{@record.inspect} @space=#{@space.inspect}>" end |
#next(loop = true) ⇒ 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 |
#position ⇒ Integer
30 31 32 |
# File 'lib/order_query/point.rb', line 30 def position space.count - after.count end |
#previous(loop = true) ⇒ 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) ⇒ ActiveRecord::Relation
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/order_query/point.rb', line 46 def side(side) query, query_args = @where_sql.build(side) scope = if side == :after space.scope else space.scope_reverse end if query.present? scope.where(query, *query_args) else scope end end |
#value(cond) ⇒ Object
60 61 62 |
# File 'lib/order_query/point.rb', line 60 def value(cond) record.send(cond.name) end |