Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rearranged case statements in Query putting most common options first

  • Loading branch information...
commit d4377a9ee407b481b5c8cfeaccc3aeb1789b194f 1 parent 25f26b1
Dan Kubb authored
Showing with 38 additions and 40 deletions.
  1. +38 −40 lib/dm-core/query.rb
View
78 lib/dm-core/query.rb
@@ -214,6 +214,7 @@ def update(other)
assert_valid_other(other)
@options.merge(other.options)
+
when Hash
if other.empty?
return self
@@ -492,6 +493,7 @@ def initialize(repository, model, options = {})
case conditions
when Hash
conditions.each { |kv| append_condition(*kv) }
+
when Array
statement, *bind_values = *conditions
@conditions << [ :raw, statement, bind_values ]
@@ -554,6 +556,11 @@ def assert_valid_fields(fields, unique)
fields.each do |field|
case field
+ when Symbol, String
+ unless @properties.named?(field)
+ raise ArgumentError, "+options[:fields]+ entry #{field.inspect} does not map to a property"
+ end
+
when Property
unless @properties.include?(field)
raise ArgumentError, "+options[:field]+ entry #{field.name.inspect} does not map to a property"
@@ -567,11 +574,6 @@ def assert_valid_fields(fields, unique)
# raise ArgumentError, "+options[:fields]+ entry #{target.inspect} does not map to a property"
# end
- when Symbol, String
- unless @properties.named?(field)
- raise ArgumentError, "+options[:fields]+ entry #{field.inspect} does not map to a property"
- end
-
else
raise ArgumentError, "+options[:fields]+ entry #{field.inspect} of an unsupported object #{field.class}"
end
@@ -589,17 +591,17 @@ def assert_valid_links(links)
links.each do |link|
case link
+ when Symbol, String
+ unless @relationships.key?(link.to_sym)
+ raise ArgumentError, "+options[:links]+ entry #{link.inspect} does not map to a relationship"
+ end
+
when Associations::Relationship
# TODO: figure out how to validate links from other models
#unless @relationships.value?(link)
# raise ArgumentError, "+options[:links]+ entry #{link.name.inspect} does not map to a relationship"
#end
- when Symbol, String
- unless @relationships.key?(link.to_sym)
- raise ArgumentError, "+options[:links]+ entry #{link.inspect} does not map to a relationship"
- end
-
else
raise ArgumentError, "+options[:links]+ entry #{link.inspect} of an unsupported object #{link.class}"
end
@@ -651,16 +653,6 @@ def assert_valid_order(order, fields)
order.each do |order|
case order
- when Direction
- unless @properties.include?(order.property)
- raise ArgumentError, "+options[:order]+ entry #{order.property.name.inspect} does not map to a property"
- end
-
- when Property
- unless @properties.include?(order)
- raise ArgumentError, "+options[:order]+ entry #{order.name.inspect} does not map to a property"
- end
-
when Operator
unless order.operator == :asc || order.operator == :desc
raise ArgumentError, "+options[:order]+ entry #{order.inspect} used an invalid operator #{order.operator}"
@@ -673,6 +665,16 @@ def assert_valid_order(order, fields)
raise ArgumentError, "+options[:order]+ entry #{order.inspect} does not map to a property"
end
+ when Property
+ unless @properties.include?(order)
+ raise ArgumentError, "+options[:order]+ entry #{order.name.inspect} does not map to a property"
+ end
+
+ when Direction
+ unless @properties.include?(order.property)
+ raise ArgumentError, "+options[:order]+ entry #{order.property.name.inspect} does not map to a property"
+ end
+
else
raise ArgumentError, "+options[:order]+ entry #{order.inspect} of an unsupported object #{order.class}"
end
@@ -710,12 +712,6 @@ def normalize_order
# should probably be normalized to a Direction object
@order = @order.map do |order|
case order
- when Direction
- order
-
- when Property
- Direction.new(order)
-
when Operator
target = order.target
property = target.kind_of?(Property) ? target : @properties[target]
@@ -725,6 +721,11 @@ def normalize_order
when Symbol, String
Direction.new(@properties[order])
+ when Property
+ Direction.new(order)
+
+ when Direction
+ order
end
end
end
@@ -738,15 +739,15 @@ def normalize_order
def normalize_fields
@fields = @fields.map do |field|
case field
+ when Symbol, String
+ @properties[field]
+
when Property
field
# TODO: mix-in Operator normalization for fields in dm-aggregates
#when Operator
# field
-
- when Symbol, String
- @properties[field]
end
end
@@ -763,11 +764,8 @@ def normalize_fields
def normalize_links
@links.map! do |link|
case link
- when Associations::Relationship
- link
-
- when Symbol, String
- @relationships[link]
+ when Symbol, String then @relationships[link]
+ when Associations::Relationship then link
end
end
@@ -791,9 +789,6 @@ def normalize_links
# @api private
def append_condition(subject, bind_value, operator = :eql)
property = case subject
- when Property
- subject
-
when Symbol
@properties[subject]
@@ -813,6 +808,9 @@ def append_condition(subject, bind_value, operator = :eql)
@links.concat(subject.relationships)
subject
+ when Property
+ subject
+
else
# TODO: move into assert_valid_conditions
raise ArgumentError, "Condition type #{subject.inspect} not supported"
@@ -849,13 +847,13 @@ def normalize_bind_value(property_or_path, bind_value)
end
case property_or_path
- when Path
- bind_value = normalize_bind_value(property_or_path.property, bind_value)
-
when Property
if property_or_path.custom?
bind_value = property_or_path.type.dump(bind_value, property_or_path)
end
+
+ when Path
+ bind_value = normalize_bind_value(property_or_path.property, bind_value)
end
bind_value.kind_of?(Array) && bind_value.size == 1 ? bind_value.first : bind_value
Please sign in to comment.
Something went wrong with that request. Please try again.