lib/dm-reflection/reflection.rb in dm-reflection-0.0.5 vs lib/dm-reflection/reflection.rb in dm-reflection-0.10.2

- old
+ new

@@ -8,11 +8,11 @@ # @return [DataMapper::Model Array] the reflected models. # def self.reflect(repository, namespace = Object, overwrite = false) adapter = DataMapper.repository(repository).adapter - models = [] + models = Hash.new adapter.get_storage_names.each do |storage_name| namespace_parts = storage_name.split('__').map do |part| Extlib::Inflection.classify(part) end @@ -38,19 +38,29 @@ end RUBY end end - model = namespace.const_set(model_name, anonymous_model) + models[model_name] = namespace.const_set(model_name, anonymous_model) + end - adapter.get_properties(storage_name).each do |attribute| - attribute.delete_if { |k,v| v.nil? } - model.property(attribute.delete(:name).to_sym, attribute.delete(:type), attribute) + models.values.each do |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]] + child.belongs_to(parent.name.downcase.to_sym) + if attribute[:relationship][:bidirectional] + parent.has(attribute[:relationship][:cardinality], child.name.pluralize.downcase.to_sym) + end + else + attribute.delete_if { |k,v| v.nil? } + model.property(attribute.delete(:name).to_sym, attribute.delete(:type), attribute) + end end - - models << model end - models + + models.to_a end end # module Reflection module Adapters extendable do