Class: DBDiagram::Domain
- Inherits:
-
Object
- Object
- DBDiagram::Domain
- Extended by:
- Inspectable
- Defined in:
- lib/db_diagram/domain.rb,
lib/db_diagram/domain/entity.rb,
lib/db_diagram/domain/attribute.rb,
lib/db_diagram/domain/relationship.rb,
lib/db_diagram/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 totrue
.
Defined Under Namespace
Classes: Attribute, Entity, Relationship
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
The options that are used to generate this domain model.
Class Method Summary collapse
-
.foreign_key_method_name ⇒ Object
Returns the method name to retrieve the foreign key from an association reflection object.
-
.generate(options = {}) ⇒ Object
Generates a domain model object based on all loaded subclasses of
ActiveRecord::Base
.
Instance Method Summary collapse
-
#app_name ⇒ Object
Returns the domain model name, which is the name of your Rails application or
nil
outside of Rails. - #current_migration_version ⇒ Object
-
#entities ⇒ Object
Returns all entities of your domain model.
-
#entity_by_name(name) ⇒ Object
Returns a specific entity object for the given Active Record model.
-
#initialize(models = [], options = {}) ⇒ Domain
constructor
Create a new domain model object based on the given array of models.
- #name ⇒ Object
-
#relationships ⇒ Object
Returns all relationships in your domain model.
-
#relationships_by_entity_name(name) ⇒ Object
Returns an array of relationships for the given Active Record model.
- #warn(message) ⇒ Object
Methods included from Inspectable
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/db_diagram/domain.rb', line 45 def initialize(models = [], = {}) @source_models, @options = models, DBDiagram..merge() end |
Instance Attribute Details
#options ⇒ Object (readonly)
The options that are used to generate this domain model.
41 42 43 |
# File 'lib/db_diagram/domain.rb', line 41 def @options end |
Class Method Details
.foreign_key_method_name ⇒ Object
Returns the method name to retrieve the foreign key from an association reflection object.
32 33 34 |
# File 'lib/db_diagram/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 DBDiagram.
25 26 27 28 |
# File 'lib/db_diagram/domain.rb', line 25 def generate( = {}) base_klass = .delete(:base_klass) || ActiveRecord::Base new base_klass.descendants, end |
Instance Method Details
#app_name ⇒ Object
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/db_diagram/domain.rb', line 51 def app_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 |
#current_migration_version ⇒ Object
68 69 70 71 72 73 |
# File 'lib/db_diagram/domain.rb', line 68 def current_migration_version raise '' if @source_models.empty? @source_models.first.connection.migration_context.current_version rescue '0001' end |
#entities ⇒ Object
Returns all entities of your domain model.
76 77 78 |
# File 'lib/db_diagram/domain.rb', line 76 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.
86 87 88 |
# File 'lib/db_diagram/domain.rb', line 86 def entity_by_name(name) # @private :nodoc: entity_mapping[name] end |
#name ⇒ Object
61 62 63 64 65 66 |
# File 'lib/db_diagram/domain.rb', line 61 def name return app_name if @source_models.empty? @source_models.first.connection.current_database.presence || app_name rescue app_name end |
#relationships ⇒ Object
Returns all relationships in your domain model.
81 82 83 |
# File 'lib/db_diagram/domain.rb', line 81 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.
91 92 93 |
# File 'lib/db_diagram/domain.rb', line 91 def relationships_by_entity_name(name) # @private :nodoc: relationships_mapping[name] or [] end |
#warn(message) ⇒ Object
95 96 97 |
# File 'lib/db_diagram/domain.rb', line 95 def warn() # @private :nodoc: puts "Warning: #{}" if .warn end |