Class: OrderQuery::OrderSpace
- Inherits:
-
Object
- Object
- OrderQuery::OrderSpace
- Includes:
- Enumerable
- Defined in:
- lib/order_query/order_space.rb
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Instance Method Summary collapse
-
#initialize(scope, order) ⇒ OrderSpace
constructor
A new instance of OrderSpace.
- #order_by_reverse_sql ⇒ Object
- #order_by_sql ⇒ Object
- #reverse_scope ⇒ Object
- #scope ⇒ Object
- #to_order_by_sql ⇒ Object
Constructor Details
#initialize(scope, order) ⇒ OrderSpace
Returns a new instance of OrderSpace.
9 10 11 12 |
# File 'lib/order_query/order_space.rb', line 9 def initialize(scope, order) @scope = scope @order = order.map { |line| OrderCondition.new(scope, line) } end |
Instance Attribute Details
#order ⇒ Object (readonly)
Returns the value of attribute order.
5 6 7 |
# File 'lib/order_query/order_space.rb', line 5 def order @order end |
Instance Method Details
#order_by_reverse_sql ⇒ Object
35 36 37 38 |
# File 'lib/order_query/order_space.rb', line 35 def order_by_reverse_sql swap = {'DESC' => 'ASC', 'ASC' => 'DESC'} to_order_by_sql.map { |s| s.gsub(/DESC|ASC/) { |m| swap[m] } } * ', ' end |
#order_by_sql ⇒ Object
40 41 42 |
# File 'lib/order_query/order_space.rb', line 40 def order_by_sql to_order_by_sql * ', ' end |
#reverse_scope ⇒ Object
18 19 20 |
# File 'lib/order_query/order_space.rb', line 18 def reverse_scope @scope.order(order_by_reverse_sql) end |
#scope ⇒ Object
14 15 16 |
# File 'lib/order_query/order_space.rb', line 14 def scope @scope.order(order_by_sql) end |
#to_order_by_sql ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/order_query/order_space.rb', line 22 def to_order_by_sql @order.map { |spec| ord = spec.order if ord == :asc || ord == :desc "#{spec.col_name_sql} #{ord.to_s.upcase}" elsif ord.respond_to?(:map) ord.map { |v| "#{spec.col_name_sql}=#{@scope.connection.quote v} #{spec.order_order.to_s.upcase}" } * ', ' else raise "Unknown order #{spec.order.inspect} (#{spec.inspect})" end } end |