Class: DBDiagram::Domain::Entity

Inherits:
Object
  • Object
show all
Extended by:
Inspectable
Defined in:
lib/db_diagram/domain/entity.rb

Overview

Entities represent your Active Record models. Entities may be connected to other entities.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspectable

inspection_attributes

Constructor Details

#initialize(domain, model) ⇒ Entity

Returns a new instance of Entity.



31
32
33
34
# File 'lib/db_diagram/domain/entity.rb', line 31

def initialize(domain, model) # @private :nodoc:
  @domain, @model = domain, model
  @name = !!model.abstract_class? ? model.name : model.table_name
end

Instance Attribute Details

#domainObject (readonly)

The domain in which this entity resides.



22
23
24
# File 'lib/db_diagram/domain/entity.rb', line 22

def domain
  @domain
end

#modelObject (readonly)

The Active Record model that this entity corresponds to.



25
26
27
# File 'lib/db_diagram/domain/entity.rb', line 25

def model
  @model
end

#nameObject (readonly)

The name of this entity. Equal to the class name of the corresponding model (for concrete entities) or given name (for abstract entities).



29
30
31
# File 'lib/db_diagram/domain/entity.rb', line 29

def name
  @name
end

Class Method Details

.from_models(domain, models) ⇒ Object



7
8
9
# File 'lib/db_diagram/domain/entity.rb', line 7

def from_models(domain, models) # @private :nodoc:
  concrete_from_models(domain, models).sort
end

Instance Method Details

#<=>(other) ⇒ Object



76
77
78
# File 'lib/db_diagram/domain/entity.rb', line 76

def <=>(other) # @private :nodoc:
  self.name <=> other.name
end

#abstract?Boolean

Returns true if this entity does not correspond directly with a database table (if and only if the entity is specialized or generalized).

Returns:

  • (Boolean)


60
61
62
# File 'lib/db_diagram/domain/entity.rb', line 60

def abstract?
  generalized?
end

#attributesObject

Returns an array of attributes for this entity.



37
38
39
# File 'lib/db_diagram/domain/entity.rb', line 37

def attributes
  @attributes ||= generalized? ? [] : Attribute.from_model(domain, model)
end

#generalized?Boolean

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.

Returns:

  • (Boolean)


53
54
55
# File 'lib/db_diagram/domain/entity.rb', line 53

def generalized?
  !!model.abstract_class?
end

#model_nameObject



68
69
70
# File 'lib/db_diagram/domain/entity.rb', line 68

def model_name
  model.name
end

#namespaceObject



64
65
66
# File 'lib/db_diagram/domain/entity.rb', line 64

def namespace
  $1 if name.match(/(.*)::.*/)
end

#relationshipsObject

Returns an array of all relationships that this entity has with other entities in the domain model.



43
44
45
# File 'lib/db_diagram/domain/entity.rb', line 43

def relationships
  domain.relationships_by_entity_name(name)
end

#to_sObject



72
73
74
# File 'lib/db_diagram/domain/entity.rb', line 72

def to_s # @private :nodoc:
  name
end