lib/dm-reflection/reflection.rb in dm-reflection-0.11.0 vs lib/dm-reflection/reflection.rb in dm-reflection-0.11.1
- old
+ new
@@ -12,11 +12,11 @@
separator = adapter.separator
models = Hash.new
adapter.get_storage_names.each do |storage_name|
namespace_parts = storage_name.split(separator).map do |part|
- Extlib::Inflection.classify(part)
+ ActiveSupport::Inflector.classify(part)
end
model_name = namespace_parts.pop
namespace = if namespace_parts.any?
@@ -37,33 +37,43 @@
#{repository.inspect}
end
RUBY
end
end
-
- models[model_name] = namespace.const_set(model_name, anonymous_model)
+
+ full_name = namespace_parts.length > 0 ? [namespace_parts, model_name].join('::') : model_name
+ models[full_name] = namespace.const_set(model_name, anonymous_model)
end
join_models = Array.new
models.each do |model_name, model|
adapter.get_properties(model.storage_name).each do |attribute|
- if attribute[:type] == DataMapper::Associations::Relationship
- parent = models[attribute[:relationship][:parent]]
- child = models[attribute[:relationship][:child]]
- if parent.nil? or child.nil?
- puts "Reflection Relationship: P: #{parent.inspect} C: #{child.inspect} A: #{attribute[:relationship].inspect}"
- end
- if attribute[:relationship][:many_to_many]
- parent.has(attribute[:relationship][:cardinality], child.name.tableize.pluralize.downcase.to_sym, :through => DataMapper::Resource, :model => child)
- child.has(attribute[:relationship][:cardinality], parent.name.tableize.pluralize.downcase.to_sym, :through => DataMapper::Resource, :model => parent)
- # Remove join model
- join_models << model_name
- else
- child.belongs_to(parent.name.tableize.downcase.to_sym, :model => parent)
- if attribute[:relationship][:bidirectional]
- parent.has(attribute[:relationship][:cardinality], child.name.tableize.pluralize.downcase.to_sym, :model => child)
- end
+ if attribute[:type] == :many_to_many
+ attribute.delete(:type)
+ attribute.delete(:name)
+ relationship = attribute.delete(:relationship)
+ parent = models[relationship.delete(:parent)]
+ child = models[relationship.delete(:child)]
+ cardinality = relationship.delete(:cardinality)
+ parent.has(cardinality, relationship[:child_name].to_sym, attribute.merge({:through => DataMapper::Resource, :model => child}))
+ child.has(cardinality, relationship[:parent_name].to_sym, attribute.merge({:through => DataMapper::Resource, :model => parent}))
+ join_models << model_name
+ elsif attribute[:type] == :has_n
+ attribute.delete(:type)
+ model.has(attribute.delete(:cardinality), attribute.delete(:name).to_sym, attribute)
+ elsif attribute[:type] == :belongs_to
+ attribute.delete(:type)
+ other_side = attribute.delete(:other_side)
+ name = attribute.delete(:name)
+# puts "#{model.name}.belongs_to(#{name}, #{attribute.inspect})"
+ model.belongs_to(name.to_sym, attribute.dup)
+ unless other_side.nil?
+ other_name = other_side.delete(:name)
+ cardinality = other_side.delete(:cardinality)
+ other_side[:model] = ActiveSupport::Inflector.singularize(model)
+# puts "#{models[attribute[:model]]}.has(#{cardinality}, #{other_name}, #{other_side.inspect})"
+ models[attribute[:model]].has(cardinality, other_name.to_sym, other_side)
end
else
attribute.delete_if { |k,v| v.nil? }
model.property(attribute.delete(:name).to_sym, attribute.delete(:type), attribute)
end