Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:sam/dm-core
Browse files Browse the repository at this point in the history
  • Loading branch information
david committed Apr 8, 2008
2 parents 7edfb19 + 89e7063 commit 861efa9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 30 deletions.
4 changes: 3 additions & 1 deletion lib/data_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@
require __DIR__ + 'data_mapper/adapters/abstract_adapter'
require __DIR__ + 'data_mapper/cli'
require __DIR__ + 'data_mapper/scope'
require __DIR__ + 'data_mapper/query'
require __DIR__ + 'data_mapper/query'
require __DIR__ + 'data_mapper/query_path'

1 change: 1 addition & 0 deletions lib/data_mapper/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def initialize(model, name, type, options)

create_getter!
create_setter!
#create_class_shadow_getter!

# Auto validation has moved to dm-more
# auto_generate_validations_for_property is mixed in from
Expand Down
57 changes: 33 additions & 24 deletions lib/data_mapper/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,32 +289,40 @@ def normalize_includes
# TODO: normalize Array of Symbol, String, DM::Property 1-jump-away or DM::Query::Path
end

def normalize_property_chain(property)
#def normalize_property_chain(property)
# DM::Query.new(Zoo, 'Zoo.displays.name' => 'foo')

relationships = []
model = @model
result = nil
property.to_s.split('.').map do |part|
next if DataMapper::Inflection.classify(part) == model.to_s

if model.properties(model.repository.name)[part] != nil
result = model.properties(model.repository.name)[part]
elsif model.relationships.has_key?(part.to_sym)
relationship = model.relationships[part.to_sym]
model = relationship.child_model == model ? relationship.parent_model : relationship.child_model
relationships << relationship
else
raise ArgumentError, "Could not normalize property chain for #{property.inspect}"
end
end
#relationships = []
#model = @model
#result = nil
#property.to_s.split('.').map do |part|
# next if DataMapper::Inflection.classify(part) == model.to_s
#
# if model.properties(model.repository.name)[part] != nil
# result = model.properties(model.repository.name)[part]
# elsif model.relationships.has_key?(part.to_sym)
# relationship = model.relationships[part.to_sym]
# model = relationship.child_model == model ? relationship.parent_model : relationship.child_model
# relationships << relationship
# else
# raise ArgumentError, "Could not normalize property chain for #{property.inspect}"
# end
#end

# Add joins if not already joined
relationships.map do |relationship|
@links << relationship if !@links.include?(relationship) && !@includes.include?(relationship)
end
#relationships.map do |relationship|
# @links << relationship if !@links.include?(relationship) && !@includes.include?(relationship)
#end

result
#result
#end

# validate that all the links or includes are present for the given DM::QueryPath
#
def validate_query_path_links(path)
path.relationships.map do |relationship|
@links << relationship unless (@links.include?(relationship) || @includes.include?(relationship))
end
end

def append_condition(property, value)
Expand All @@ -323,13 +331,14 @@ def append_condition(property, value)
property = case property
when DataMapper::Property
property
when DataMapper::QueryPath
validate_query_path_links(property)
property
when Operator
operator = property.type
@properties[property.to_sym]
when Symbol, String
prop = @properties[property]
prop = normalize_property_chain(property) unless prop
prop
@properties[property]
else
raise ArgumentError, "Condition type #{property.inspect} not supported"
end
Expand Down
36 changes: 36 additions & 0 deletions lib/data_mapper/query_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module DataMapper
class QueryPath

attr_reader :relationships, :model, :property

def initialize(relationships, model_name ,property_name=nil)
@relationships = relationships
@model = DataMapper::Inflection.classify(model_name.to_s).to_class
@property = @model.properties(@model.repository.name)[model_name] if property_name
end

alias_method :_method_missing, :method_missing

def method_missing(method, *args)
if @model.relationships.has_key?(method)
relations = []
relations.concat(@relationships)
relations << @model.relationships[method]
return DataMapper::QueryPath.new(relations,method)
end

if @model.properties(@model.repository.name)[method]
@property = @model.properties(@model.repository.name)[method]
return self
end

_method_missing(method,args)
end

# duck type the QueryPath to act like a DM::Property
def field
@property ? @property.field : nil
end

end # class QueryPath
end # module DataMapper
11 changes: 11 additions & 0 deletions lib/data_mapper/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ def private_attributes=(values_hash)
end

module ClassMethods

alias_method :_method_missing, :method_missing
def method_missing(method, *args)
if relationships.has_key?(method)
return DataMapper::QueryPath.new([relationships[method]],method)
end
result = properties(repository.name)[method]
return result if result
_method_missing(method,args)
end


def repository(name = default_repository_name)
DataMapper::repository(name)
Expand Down
6 changes: 1 addition & 5 deletions spec/integration/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,14 @@ class Vehicle

it 'should accept a property chain as the key to a condition' do
repository(:sqlite3) do
vehicle = Vehicle.first('Vehicle.factory.region.name' => 'North West')
vehicle = Vehicle.first(Vehicle.factory.region.name => 'North West')
vehicle.name.should == '10 ton delivery truck'
end

end

it 'should auto generate the link if a DM::Property from a different resource is in the :fields option'

# it 'should take properties of associations in the conditions clause' do
# query = DataMapper::Query.new(Vehicle, 'Vehicle.factory.region.name' => 'foo')
# end

it 'should create links with composite keys'

after do
Expand Down

0 comments on commit 861efa9

Please sign in to comment.