Class: RailsERD::Domain

Inherits:
Object
  • Object
show all
Extended by:
Inspectable
Defined in:
lib/rails_erd/domain.rb,
lib/rails_erd/domain/entity.rb,
lib/rails_erd/domain/attribute.rb,
lib/rails_erd/domain/relationship.rb,
lib/rails_erd/domain/specialization.rb,
lib/rails_erd/domain/relationship/cardinality.rb

Overview

The domain describes your Rails domain model. This class is the starting point to get information about your models.

Options

The following options are available:

warn

When set to false, no warnings are printed to the command line while processing the domain model. Defaults to true.

Defined Under Namespace

Classes: Attribute, Entity, NullGeneralized, NullSpecialization, Relationship, Specialization

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspectable

inspection_attributes

Constructor Details

#initialize(models = [], options = {}) ⇒ Domain

Create a new domain model object based on the given array of models. The given models are assumed to be subclasses of ActiveRecord::Base.



45
46
47
# File 'lib/rails_erd/domain.rb', line 45

def initialize(models = [], options = {})
  @source_models, @options = models, RailsERD.options.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

The options that are used to generate this domain model.



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

def options
  @options
end

Class Method Details

.foreign_key_method_nameObject

Returns the method name to retrieve the foreign key from an association reflection object.



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

def foreign_key_method_name # @private :nodoc:
  @foreign_key_method_name ||= ActiveRecord::Reflection::AssociationReflection.method_defined?(:foreign_key) ? :foreign_key : :primary_key_name
end

.generate(options = {}) ⇒ Object

Generates a domain model object based on all loaded subclasses of ActiveRecord::Base. Make sure your models are loaded before calling this method.

The options hash allows you to override the default options. For a list of available options, see RailsERD.



26
27
28
# File 'lib/rails_erd/domain.rb', line 26

def generate(options = {})
  new ActiveRecord::Base.descendants, options
end

Instance Method Details

#entitiesObject

Returns all entities of your domain model.



62
63
64
# File 'lib/rails_erd/domain.rb', line 62

def entities
  @entities ||= Entity.from_models(self, models)
end

#entity_by_name(name) ⇒ Object

Returns a specific entity object for the given Active Record model.



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

def entity_by_name(name) # @private :nodoc:
  entity_mapping[name]
end

#nameObject

Returns the domain model name, which is the name of your Rails application or nil outside of Rails.



51
52
53
54
55
56
57
58
59
# File 'lib/rails_erd/domain.rb', line 51

def name
  return unless defined?(Rails) && Rails.application

  if Rails.application.class.respond_to?(:module_parent)
    Rails.application.class.module_parent.name
  else
    Rails.application.class.parent.name
  end
end

#relationshipsObject

Returns all relationships in your domain model.



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

def relationships
  @relationships ||= Relationship.from_associations(self, associations)
end

#relationships_by_entity_name(name) ⇒ Object

Returns an array of relationships for the given Active Record model.



82
83
84
# File 'lib/rails_erd/domain.rb', line 82

def relationships_by_entity_name(name) # @private :nodoc:
  relationships_mapping[name] or []
end

#specializationsObject

Returns all specializations in your domain model.



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

def specializations
  @specializations ||= Specialization.from_models(self, models)
end

#specializations_by_entity_name(name) ⇒ Object



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

def specializations_by_entity_name(name)
  specializations_mapping[name] or []
end

#warn(message) ⇒ Object



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

def warn(message) # @private :nodoc:
  puts "Warning: #{message}" if options.warn
end