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 30 |
# File 'lib/order_query/space.rb', line 15 def initialize(base_scope, order_spec) @base_scope = base_scope @columns = order_spec.map(&:clone) @columns.map! do |cond_spec| build_column(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
33 34 35 |
# File 'lib/order_query/space.rb', line 33 def at(record) Point.new(record, self) end |
#first ⇒ ActiveRecord::Base
49 50 51 |
# File 'lib/order_query/space.rb', line 49 def first scope.first end |
#inspect ⇒ Object
58 59 60 61 |
# File 'lib/order_query/space.rb', line 58 def inspect "#<OrderQuery::Space @columns=#{@columns.inspect} "\ "@base_scope=#{@base_scope.inspect}>" end |
#last ⇒ ActiveRecord::Base
54 55 56 |
# File 'lib/order_query/space.rb', line 54 def last scope_reverse.first end |
#scope ⇒ ActiveRecord::Relation
Returns scope ordered by columns.
38 39 40 |
# File 'lib/order_query/space.rb', line 38 def scope @scope ||= @base_scope.order(Arel.sql(@order_by_sql.build)) end |
#scope_reverse ⇒ ActiveRecord::Relation
Returns scope ordered by columns in reverse.
43 44 45 46 |
# File 'lib/order_query/space.rb', line 43 def scope_reverse @scope_reverse ||= @base_scope .order(Arel.sql(@order_by_sql.build_reverse)) end |