Class: RailsERD::Domain::Entity

Inherits:
Object
  • Object
show all
Extended by:
Inspectable
Defined in:
lib/rails_erd/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, name, model = nil) ⇒ Entity

Returns a new instance of Entity.


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

def initialize(domain, name, model = nil) # @private :nodoc:
  @domain, @name, @model = domain, name, model
end

Instance Attribute Details

#domainObject (readonly)

The domain in which this entity resides.


28
29
30
# File 'lib/rails_erd/domain/entity.rb', line 28

def domain
  @domain
end

#modelObject (readonly)

The Active Record model that this entity corresponds to.


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

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).


35
36
37
# File 'lib/rails_erd/domain/entity.rb', line 35

def name
  @name
end

Class Method Details

.from_models(domain, models) ⇒ Object


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

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

Instance Method Details

#<=>(other) ⇒ Object


103
104
105
# File 'lib/rails_erd/domain/entity.rb', line 103

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

#attributesObject

Returns an array of attributes for this entity.


42
43
44
# File 'lib/rails_erd/domain/entity.rb', line 42

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

#childrenObject

Returns all child entities, if this is a generalized entity.


91
92
93
# File 'lib/rails_erd/domain/entity.rb', line 91

def children
  @children ||= domain.specializations_by_entity_name(name).map(&:specialized)
end

#connected?Boolean

Returns true if this entity has any relationships with other models, false otherwise.

Returns:

  • (Boolean)

54
55
56
# File 'lib/rails_erd/domain/entity.rb', line 54

def connected?
  relationships.any?
end

#disconnected?Boolean

Returns true if this entity has no relationships with any other models, false otherwise. Opposite of connected?.

Returns:

  • (Boolean)

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

def disconnected?
  relationships.none?
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)

70
71
72
# File 'lib/rails_erd/domain/entity.rb', line 70

def generalized?
  !model or !!model.abstract_class?
end

#namespaceObject


95
96
97
# File 'lib/rails_erd/domain/entity.rb', line 95

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.


48
49
50
# File 'lib/rails_erd/domain/entity.rb', line 48

def relationships
  domain.relationships_by_entity_name(name)
end

#specialized?Boolean

Returns true if this entity descends from another entity, and is represented in the same table as its parent. In Rails this concept is referred to as single-table inheritance. In entity-relationship diagrams it is called specialization.

Returns:

  • (Boolean)

78
79
80
# File 'lib/rails_erd/domain/entity.rb', line 78

def specialized?
  !!model and !model.descends_from_active_record?
end

#to_sObject


99
100
101
# File 'lib/rails_erd/domain/entity.rb', line 99

def to_s # @private :nodoc:
  name
end

#virtual?Boolean Also known as: abstract?

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)

85
86
87
# File 'lib/rails_erd/domain/entity.rb', line 85

def virtual?
  generalized? or specialized?
end