lib/rails_erd/entity.rb in rails-erd-0.2.0 vs lib/rails_erd/entity.rb in rails-erd-0.3.0

- old
+ new

@@ -1,9 +1,15 @@ module RailsERD # Entities represent your Active Record models. Entities may be connected # to other entities. class Entity + class << self + def from_models(domain, models) # @private :nodoc: + models.collect { |model| new domain, model }.sort + end + end + # The domain in which this entity resides. attr_reader :domain # The Active Record model that this entity corresponds to. attr_reader :model @@ -21,13 +27,30 @@ # entities in the domain model. def relationships @domain.relationships_for(@model) end + # Returns the parent entity, if this entity is a descendant. + def parent + @domain.entity_for(@model.superclass) if descendant? + end + # Returns +true+ if this entity has any relationships with other models, # +false+ otherwise. def connected? relationships.any? + end + + # Returns +true+ if this entity has no relationships with any other models, + # +false+ otherwise. Opposite of +connected?+. + def disconnected? + relationships.none? + end + + # Returns +true+ if this entity descends from another entity, and is + # represented in the same table as its parent. + def descendant? + !@model.descends_from_active_record? end # Returns the name of this entity, which is the class name of the # corresponding model. def name