Created
January 13, 2011 03:22
-
-
Save eclubb/777334 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DataMapper | |
module Reflection | |
def self.included(model) | |
model.extend ClassMethods | |
end | |
module ClassMethods | |
def reflect_on_association(association) | |
relationship = relationships[association] | |
MacroReflection.new(relationship, relationship.name, {}) | |
end | |
end | |
class MacroReflection | |
attr_reader :macro, :name, :options | |
def initialize(relationship, name, options) | |
@relationship = relationship | |
@macro = ar_macro_name(relationship) | |
@name = name | |
@options = options | |
end | |
def class_name | |
case @macro | |
when :belongs_to then @relationship.parent_model_name | |
when :has_many then @relationship.child_model_name | |
when :has_and_belongs_to_many then @relationship.child_model_name | |
end | |
end | |
def klass | |
case @macro | |
when :belongs_to then @relationship.parent_model | |
when :has_many then @relationship.child_model | |
when :has_and_belongs_to_many then @relationship.child_model | |
end | |
end | |
private | |
def ar_macro_name(relationship) | |
klass = relationship.class.parent | |
case | |
when klass == DataMapper::Associations::ManyToOne then :belongs_to | |
when klass == DataMapper::Associations::OneToMany then :has_many | |
when klass == DataMapper::Associations::ManyToMany then :has_and_belongs_to_many | |
else "#{relationship.class} not implemented" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment