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.



39
40
41
# File 'lib/rails_erd/domain/entity.rb', line 39

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.



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

def domain
  @domain
end

#modelObject (readonly)

The Active Record model that this entity corresponds to.



33
34
35
# File 'lib/rails_erd/domain/entity.rb', line 33

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



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

def name
  @name
end

Class Method Details

.from_models(domain, models) ⇒ Object



9
10
11
# File 'lib/rails_erd/domain/entity.rb', line 9

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

Instance Method Details

#<=>(other) ⇒ Object



109
110
111
# File 'lib/rails_erd/domain/entity.rb', line 109

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

#attributesObject

Returns an array of attributes for this entity.



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

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

#childrenObject

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



97
98
99
# File 'lib/rails_erd/domain/entity.rb', line 97

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)


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

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)


66
67
68
# File 'lib/rails_erd/domain/entity.rb', line 66

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)


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

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

#labelObject



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

def label
  RailsERD.options[:table_names] ? model.table_name : name
end

#namespaceObject



101
102
103
# File 'lib/rails_erd/domain/entity.rb', line 101

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.



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

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)


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

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

#to_sObject



105
106
107
# File 'lib/rails_erd/domain/entity.rb', line 105

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)


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

def virtual?
  generalized? or specialized?
end