Skip to content

Commit

Permalink
Fix for #528. Updates how custom types are dumped in Query.
Browse files Browse the repository at this point in the history
* If a query involves a custom type, the property value must be dumped via the custom type API.
* Query's with custom types in a Query::Path are handled as a special case.
  • Loading branch information
benburkert committed Aug 19, 2008
1 parent 4f84ce8 commit 4fbeca8
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/dm-core/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ def initialize(repository, model, options = {})
@links = normalize_links(@links)
@includes = normalize_includes(@includes)

translate_custom_types(@properties, options)

# treat all non-options as conditions
(options.keys - OPTIONS - OPTIONS.map { |option| option.to_s }).each do |k|
append_condition(k, options[k])
Expand Down Expand Up @@ -218,17 +216,6 @@ def initialize_copy(original)
@conditions = original.conditions.map { |tuple| tuple.dup }
end

def translate_custom_types(properties, options)
options.each do |key, value|
case key
when DataMapper::Query::Operator
options[key] = properties[key.target].type.dump(value, properties[key.target]) if properties.has_property?(key.target) && properties[key.target].custom?
when Symbol, String
options[key] = properties[key].type.dump(value, properties[key]) if properties.has_property?(key) && properties[key].custom?
end
end
end

# validate the options
def assert_valid_options(options)
# validate the reload option and unique option
Expand Down Expand Up @@ -440,9 +427,26 @@ def append_condition(clause, bind_value)
raise ArgumentError, "Clause #{clause.inspect} does not map to a DataMapper::Property", caller(2)
end

bind_value = dump_custom_value(property, bind_value)

@conditions << [ operator, property, bind_value ]
end

def dump_custom_value(property_or_path, bind_value)
case property_or_path
when DataMapper::Query::Path
dump_custom_value(property_or_path.property, bind_value)
when Property
if property_or_path.custom?
property_or_path.type.dump(bind_value, property_or_path)
else
bind_value
end
else
bind_value
end
end

# TODO: check for other mutually exclusive operator + property
# combinations. For example if self's conditions were
# [ :gt, :amount, 5 ] and the other's condition is [ :lt, :amount, 2 ]
Expand Down

0 comments on commit 4fbeca8

Please sign in to comment.