module RailsERD class Relationship class Cardinality CARDINALITY_NAMES = %w{one_to_one one_to_many many_to_many} #:nodoc: ORDER = {} #:nodoc: class << self # Returns the cardinality as symbol. attr_reader :type def from_macro(macro) #:nodoc: case macro when :has_and_belongs_to_many then ManyToMany when :has_many then OneToMany when :has_one then OneToOne end end def <=>(other) #:nodoc: ORDER[self] <=> ORDER[other] end CARDINALITY_NAMES.each do |cardinality| define_method :"#{cardinality}?" do type == cardinality end end end CARDINALITY_NAMES.each_with_index do |cardinality, i| klass = Cardinality.const_set cardinality.camelize.to_sym, Class.new(Cardinality) { @@type = cardinality } ORDER[klass] = i end end end end