module DBDiagram class Domain # Entities represent your Active Record models. Entities may be connected # to other entities. class Entity class << self def from_models(domain, models) # @private :nodoc: concrete_from_models(domain, models).sort end private def concrete_from_models(domain, models) models.collect { |model| new(domain, model) } end end extend Inspectable inspection_attributes :model # The domain in which this entity resides. attr_reader :domain # The Active Record model that this entity corresponds to. attr_reader :model # The name of this entity. Equal to the class name of the corresponding # model (for concrete entities) or given name (for abstract entities). attr_reader :name def initialize(domain, model) # @private :nodoc: @domain, @model = domain, model @name = !!model.abstract_class? ? model.name : model.table_name end # Returns an array of attributes for this entity. def attributes @attributes ||= generalized? ? [] : Attribute.from_model(domain, model) end # Returns an array of all relationships that this entity has with other # entities in the domain model. def relationships domain.relationships_by_entity_name(name) end # Returns +true+ if this entity is a generalization, which does not # correspond with a database table. Generalized entities are either # models that are defined as +abstract_class+ or they are constructed # from polymorphic interfaces. Any +has_one+ or +has_many+ association # that defines a polymorphic interface with :as => :name will # lead to a generalized entity to be created. def generalized? !!model.abstract_class? end # Returns +true+ if this entity does not correspond directly with a # database table (if and only if the entity is specialized or # generalized). def abstract? generalized? end def namespace $1 if name.match(/(.*)::.*/) end def model_name model.name end def to_s # @private :nodoc: name end def <=>(other) # @private :nodoc: self.name <=> other.name end end end end