Class: OrderQuery::Space
- Inherits:
-
Object
- Object
- OrderQuery::Space
- Defined in:
- lib/order_query/space.rb
Overview
Order specification and a scope
Instance Attribute Summary collapse
- #columns ⇒ Array<OrderQuery::Column> readonly
Instance Method Summary collapse
- #at(record) ⇒ Point
- #first ⇒ ActiveRecord::Base
-
#initialize(base_scope, order_spec) ⇒ Space
constructor
A new instance of Space.
- #inspect ⇒ Object
- #last ⇒ ActiveRecord::Base
-
#scope ⇒ ActiveRecord::Relation
Scope ordered by columns.
-
#scope_reverse ⇒ ActiveRecord::Relation
Scope ordered by columns in reverse.
Constructor Details
#initialize(base_scope, order_spec) ⇒ Space
Returns a new instance of Space.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/order_query/space.rb', line 15 def initialize(base_scope, order_spec) @base_scope = base_scope @columns = order_spec.map do |cond_spec| Column.new(base_scope, *cond_spec) end # add primary key if columns are not unique unless @columns.last.unique? if @columns.detect(&:unique?) fail ArgumentError, 'Unique column must be last' end @columns << Column.new(base_scope, base_scope.primary_key) end @order_by_sql = SQL::OrderBy.new(@columns) end |
Instance Attribute Details
#columns ⇒ Array<OrderQuery::Column> (readonly)
9 10 11 |
# File 'lib/order_query/space.rb', line 9 def columns @columns end |
Instance Method Details
#at(record) ⇒ Point
32 33 34 |
# File 'lib/order_query/space.rb', line 32 def at(record) Point.new(record, self) end |
#first ⇒ ActiveRecord::Base
48 49 50 |
# File 'lib/order_query/space.rb', line 48 def first scope.first end |
#inspect ⇒ Object
57 58 59 60 |
# File 'lib/order_query/space.rb', line 57 def inspect "#<OrderQuery::Space @columns=#{@columns.inspect} "\ "@base_scope=#{@base_scope.inspect}>" end |
#last ⇒ ActiveRecord::Base
53 54 55 |
# File 'lib/order_query/space.rb', line 53 def last scope_reverse.first end |
#scope ⇒ ActiveRecord::Relation
Returns scope ordered by columns.
37 38 39 |
# File 'lib/order_query/space.rb', line 37 def scope @scope ||= @base_scope.order(Arel.sql(@order_by_sql.build)) end |
#scope_reverse ⇒ ActiveRecord::Relation
Returns scope ordered by columns in reverse.
42 43 44 45 |
# File 'lib/order_query/space.rb', line 42 def scope_reverse @scope_reverse ||= @base_scope .order(Arel.sql(@order_by_sql.build_reverse)) end |