Skip to content

Commit

Permalink
Simplified Query::Direction to inherit from Query::Operator
Browse files Browse the repository at this point in the history
  • Loading branch information
dkubb committed Jun 3, 2009
1 parent ce3961e commit de9da83
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 77 deletions.
2 changes: 1 addition & 1 deletion lib/dm-core.rb
Expand Up @@ -50,8 +50,8 @@
require dir / 'query'
require dir / 'query' / 'conditions' / 'operation'
require dir / 'query' / 'conditions' / 'comparison'
require dir / 'query' / 'direction'
require dir / 'query' / 'operator'
require dir / 'query' / 'direction'
require dir / 'query' / 'path'
require dir / 'query' / 'sort'
require dir / 'repository'
Expand Down
6 changes: 3 additions & 3 deletions lib/dm-core/adapters/data_objects_adapter.rb
Expand Up @@ -481,9 +481,9 @@ def conditions_statement(conditions, qualify = false)
#
# @api private
def order_statement(order, qualify)
statements = order.map do |order|
statement = property_to_column_name(order.property, qualify)
statement << ' DESC' if order.direction == :desc
statements = order.map do |direction|
statement = property_to_column_name(direction.target, qualify)
statement << ' DESC' if direction.operator == :desc
statement
end

Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/query.rb
Expand Up @@ -400,7 +400,7 @@ def match_records(records)
#
# @api semipublic
def sort_records(records)
sort_order = order.map { |i| [ i.property, i.direction == :asc ] }
sort_order = order.map { |direction| [ direction.target, direction.operator == :asc ] }

records.sort_by do |record|
sort_order.map do |(property, ascending)|
Expand Down
74 changes: 7 additions & 67 deletions lib/dm-core/query/direction.rb
@@ -1,84 +1,24 @@
module DataMapper
class Query
class Direction
include Extlib::Assertions
class Direction < Operator
extend Deprecate

# TODO: document
# @api private
attr_reader :property

# TODO: document
# @api private
attr_reader :direction
deprecate :property, :target
deprecate :direction, :operator

# TODO: document
# @api private
def reverse!
@direction = @direction == :asc ? :desc : :asc
@operator = @operator == :asc ? :desc : :asc
self
end

# TODO: document
# @api private
def ==(other)
if equal?(other)
return true
end

unless other.respond_to?(:property)
return false
end

unless other.respond_to?(:direction)
return false
end

cmp?(other, :==)
end

# TODO: document
# @api private
def eql?(other)
if equal?(other)
return true
end

unless self.class.equal?(other.class)
return false
end

cmp?(other, :eql?)
end

# TODO: document
# @api private
def hash
property.hash
end

# TODO: document
# @api private
def inspect
"#<#{self.class.name} @property=#{property.inspect} @direction=#{direction.inspect}>"
end

private

# TODO: document
# @api private
def initialize(property, direction = :asc)
assert_kind_of 'property', property, Property
assert_kind_of 'direction', direction, Symbol

@property = property
@direction = direction
end

# TODO: document
# @api private
def cmp?(other, operator)
property.send(operator, other.property) &&
direction.send(operator, other.direction)
def initialize(target, operator = :asc)
super
end
end # class Direction
end # class Query
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/query/operator.rb
Expand Up @@ -46,7 +46,7 @@ def eql?(other)
# TODO: document
# @api private
def hash
target.hash
@target.hash
end

# TODO: document
Expand Down
6 changes: 3 additions & 3 deletions lib/dm-core/resource.rb
Expand Up @@ -444,9 +444,9 @@ def <=>(other)
raise ArgumentError, "Cannot compare a #{other.model} instance with a #{model} instance"
end
cmp = 0
model.default_order(repository_name).map do |i|
cmp = i.property.get!(self) <=> i.property.get!(other)
cmp *= -1 if i.direction == :desc
model.default_order(repository_name).map do |direction|
cmp = direction.target.get!(self) <=> direction.target.get!(other)
cmp *= -1 if direction.operator == :desc
break if cmp != 0
end
cmp
Expand Down
2 changes: 1 addition & 1 deletion spec/semipublic/query_spec.rb
Expand Up @@ -1267,7 +1267,7 @@ class ::User
@fields=[#<DataMapper::Property @model=User @name=:name>, #<DataMapper::Property @model=User @name=:referrer_name>]
@links=[]
@conditions=#<DataMapper::Query::Conditions::AndOperation @operands=[]>
@order=[#<DataMapper::Query::Direction @property=#<DataMapper::Property @model=User @name=:name> @direction=:asc>]
@order=[#<DataMapper::Query::Direction @target=#<DataMapper::Property @model=User @name=:name> @operator=:asc>]
@limit=3
@offset=0
@reload=false
Expand Down

0 comments on commit de9da83

Please sign in to comment.