db_diagram 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d57803fb20594bda33f3c18821172c211c407298f77195370437f96bc51f517
4
+ data.tar.gz: a155532c9d7a8afbb019f3a29187cf0a35b44ea2dc60d47590749f4f1b0b917d
5
+ SHA512:
6
+ metadata.gz: a12348fb80939bbd79c82426c999fb7c71609fd8b095ecd972776212612edd928acd8fb0b5acdb457337e2017bc899d62a9b7abf7818bbe00b861e5f8915c187
7
+ data.tar.gz: aa20629614f8bec8195e3d1bd79cfe738b6a2ba6268e0138edec1afd70c34ec9430ffc629edfefd61b8a2fe673760e3589bb6c83d98e86483cdee1d2b1cb4af0
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ /.idea/
data/.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="rbenv: 2.3.8" project-jdk-type="RUBY_SDK" />
4
+ </project>
@@ -0,0 +1,65 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ChangeListManager">
4
+ <list default="true" id="7f350ad4-1bb3-4a04-86f5-62b2a5e4d821" name="Default Changelist" comment="">
5
+ <change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
6
+ <change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
7
+ <change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
8
+ <change afterPath="$PROJECT_DIR$/.rspec" afterDir="false" />
9
+ <change afterPath="$PROJECT_DIR$/.travis.yml" afterDir="false" />
10
+ <change afterPath="$PROJECT_DIR$/CODE_OF_CONDUCT.md" afterDir="false" />
11
+ <change afterPath="$PROJECT_DIR$/Gemfile" afterDir="false" />
12
+ <change afterPath="$PROJECT_DIR$/LICENSE.txt" afterDir="false" />
13
+ <change afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
14
+ <change afterPath="$PROJECT_DIR$/Rakefile" afterDir="false" />
15
+ <change afterPath="$PROJECT_DIR$/bin/console" afterDir="false" />
16
+ <change afterPath="$PROJECT_DIR$/bin/setup" afterDir="false" />
17
+ <change afterPath="$PROJECT_DIR$/db_diagram.gemspec" afterDir="false" />
18
+ <change afterPath="$PROJECT_DIR$/lib/db_diagram.rb" afterDir="false" />
19
+ <change afterPath="$PROJECT_DIR$/lib/db_diagram/version.rb" afterDir="false" />
20
+ <change afterPath="$PROJECT_DIR$/spec/db_diagram_spec.rb" afterDir="false" />
21
+ <change afterPath="$PROJECT_DIR$/spec/spec_helper.rb" afterDir="false" />
22
+ </list>
23
+ <option name="SHOW_DIALOG" value="false" />
24
+ <option name="HIGHLIGHT_CONFLICTS" value="true" />
25
+ <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
26
+ <option name="LAST_RESOLUTION" value="IGNORE" />
27
+ </component>
28
+ <component name="Git.Settings">
29
+ <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
30
+ </component>
31
+ <component name="ProjectId" id="1vFIxP5HDlxrTweBONN1ektn8eE" />
32
+ <component name="ProjectLevelVcsManager" settingsEditedManually="true" />
33
+ <component name="ProjectViewState">
34
+ <option name="hideEmptyMiddlePackages" value="true" />
35
+ <option name="showLibraryContents" value="true" />
36
+ </component>
37
+ <component name="PropertiesComponent">
38
+ <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
39
+ <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
40
+ <property name="WebServerToolWindowFactoryState" value="false" />
41
+ <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
42
+ <property name="nodejs_npm_path_reset_for_default_project" value="true" />
43
+ <property name="settings.editor.selected.configurable" value="preferences.externalDependencies" />
44
+ <property name="vue.rearranger.settings.migration" value="true" />
45
+ </component>
46
+ <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
47
+ <component name="SpringUtil" SPRING_PRE_LOADER_OPTION="true" RAKE_SPRING_PRE_LOADER_OPTION="true" RAILS_SPRING_PRE_LOADER_OPTION="true" />
48
+ <component name="SvnConfiguration">
49
+ <configuration>$USER_HOME$/.subversion</configuration>
50
+ </component>
51
+ <component name="TaskManager">
52
+ <task active="true" id="Default" summary="Default task">
53
+ <changelist id="7f350ad4-1bb3-4a04-86f5-62b2a5e4d821" name="Default Changelist" comment="" />
54
+ <created>1626151656269</created>
55
+ <option name="number" value="Default" />
56
+ <option name="presentableId" value="Default" />
57
+ <updated>1626151656269</updated>
58
+ <workItem from="1626151657542" duration="73000" />
59
+ </task>
60
+ <servers />
61
+ </component>
62
+ <component name="TypeScriptGeneratedFilesManager">
63
+ <option name="version" value="3" />
64
+ </component>
65
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.8
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at shuh_jx@163.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in db_diagram.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ db_diagram (0.1.0)
5
+ activerecord (>= 4.2)
6
+ activesupport (>= 4.2)
7
+ ruby-graphviz (~> 1.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (6.1.4)
13
+ activesupport (= 6.1.4)
14
+ activerecord (6.1.4)
15
+ activemodel (= 6.1.4)
16
+ activesupport (= 6.1.4)
17
+ activesupport (6.1.4)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 1.6, < 2)
20
+ minitest (>= 5.1)
21
+ tzinfo (~> 2.0)
22
+ zeitwerk (~> 2.3)
23
+ concurrent-ruby (1.1.9)
24
+ diff-lcs (1.4.4)
25
+ i18n (1.8.10)
26
+ concurrent-ruby (~> 1.0)
27
+ minitest (5.14.4)
28
+ rake (10.5.0)
29
+ rexml (3.2.5)
30
+ rspec (3.10.0)
31
+ rspec-core (~> 3.10.0)
32
+ rspec-expectations (~> 3.10.0)
33
+ rspec-mocks (~> 3.10.0)
34
+ rspec-core (3.10.1)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-expectations (3.10.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.10.0)
39
+ rspec-mocks (3.10.2)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.10.0)
42
+ rspec-support (3.10.2)
43
+ ruby-graphviz (1.2.5)
44
+ rexml
45
+ tzinfo (2.0.4)
46
+ concurrent-ruby (~> 1.0)
47
+ zeitwerk (2.4.2)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ bundler (~> 1.17)
54
+ db_diagram!
55
+ rake (~> 10.0)
56
+ rspec (~> 3.0)
57
+
58
+ BUNDLED WITH
59
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 suhu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # DBDiagram
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/db_diagram`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'db_diagram'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install db_diagram
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/db_diagram. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the DBDiagram project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/db_diagram/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "db_diagram"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "db_diagram/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "db_diagram"
8
+ spec.version = DBDiagram::VERSION
9
+ spec.authors = ["suhu"]
10
+ spec.email = ["shuh_jx@163.com"]
11
+
12
+ spec.summary = 'DBDiagram'
13
+ spec.description = 'DBDiagram'
14
+ spec.homepage = "https://github.com/shuhjx/db_diagram"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+ spec.required_ruby_version = '>= 2.2'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.17"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+
31
+ spec.add_runtime_dependency "activerecord", ">= 4.2"
32
+ spec.add_runtime_dependency "activesupport", ">= 4.2"
33
+ spec.add_runtime_dependency "ruby-graphviz", "~> 1.2"
34
+ end
data/lib/db_diagram.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "db_diagram/version"
2
+ require "active_support/ordered_options"
3
+
4
+ # DBDiagram.options.orientation = :vertical
5
+ # DBDiagram.options.title = "My model diagram"
6
+ module DBDiagram
7
+ class Error < StandardError; end
8
+
9
+ class << self
10
+ # Access to default options. Any instance of DBDiagram::Domain and
11
+ # DBDiagram::Diagram will use these options unless overridden.
12
+ attr_accessor :options
13
+
14
+ def default_options
15
+ ActiveSupport::OrderedOptions[
16
+ :attributes, :true, #显示全部字段,调用Attribute#true?
17
+ :filename, nil, #nil时文件名为数据库名
18
+ :filetype, :png,
19
+ :markup, true,
20
+ :notation, :simple, #显示式样
21
+ :orientation, :horizontal, #排列方向,横向
22
+ :sort, false, #字段不排序
23
+ :warn, true,
24
+ :title, true,
25
+ :exclude, nil, #不显示的表名,例:[:admins, users]
26
+ :only, nil, #只显示的表名,例:[:admins, users]
27
+ :only_recursion_depth, nil,
28
+ :prepend_primary, true, #把主键放在最前面
29
+ :cluster, false,
30
+ ]
31
+ end
32
+ end
33
+
34
+ module Inspectable # @private :nodoc:
35
+ def inspection_attributes(*attributes)
36
+ attribute_inspection = attributes.collect { |attribute|
37
+ " @#{attribute}=\#{[Symbol, String].include?(#{attribute}.class) ? #{attribute}.inspect : #{attribute}}"
38
+ }.join
39
+ class_eval <<-RUBY
40
+ def inspect
41
+ "#<\#{self.class}:0x%.14x#{attribute_inspection}>" % (object_id << 1)
42
+ end
43
+ RUBY
44
+ end
45
+ end
46
+
47
+ self.options = default_options #.merge(Config.load)
48
+ end
@@ -0,0 +1,184 @@
1
+ require "db_diagram/domain"
2
+
3
+ module DBDiagram
4
+ # This class is an abstract class that will process a domain model and
5
+ # allows easy creation of diagrams. To implement a new diagram type, derive
6
+ # from this class and override +process_entity+, +process_relationship+,
7
+ # and (optionally) +save+.
8
+ #
9
+ # As an example, a diagram class that generates code that can be used with
10
+ # yUML (https://yuml.me) can be as simple as:
11
+ #
12
+ # require "db_diagram/diagram"
13
+ #
14
+ # class YumlDiagram < DBDiagram::Diagram
15
+ # setup { @edges = [] }
16
+ #
17
+ # each_relationship do |relationship|
18
+ #
19
+ # arrow = case
20
+ # when relationship.one_to_one? then "1-1>"
21
+ # when relationship.one_to_many? then "1-*>"
22
+ # when relationship.many_to_many? then "*-*>"
23
+ # end
24
+ #
25
+ # @edges << "[#{relationship.source}] #{arrow} [#{relationship.destination}]"
26
+ # end
27
+ #
28
+ # save { @edges * "\n" }
29
+ # end
30
+ #
31
+ # Then, to generate the diagram (example based on the domain model of Gemcutter):
32
+ #
33
+ # YumlDiagram.create
34
+ # #=> "[Rubygem] 1-*> [Ownership]
35
+ # # [Rubygem] 1-*> [Subscription]
36
+ # # [Rubygem] 1-*> [Version]
37
+ # # [Rubygem] 1-1> [Linkset]
38
+ # # [Rubygem] 1-*> [Dependency]
39
+ # # [Version] 1-*> [Dependency]
40
+ # # [User] 1-*> [Ownership]
41
+ # # [User] 1-*> [Subscription]
42
+ # # [User] 1-*> [WebHook]"
43
+ #
44
+ # For another example implementation, see Diagram::Graphviz, which is the
45
+ # default (and currently only) diagram type that is used by Rails ERD.
46
+ #
47
+ # === Options
48
+ #
49
+ # The following options are available and will by automatically used by any
50
+ # diagram generator inheriting from this class.
51
+ #
52
+ # attributes:: Selects which attributes to display. Can be any combination of
53
+ # +:content+, +:primary_keys+, +:foreign_keys+, +:timestamps+, or
54
+ # +:inheritance+.
55
+ # warn:: When set to +false+, no warnings are printed to the
56
+ # command line while processing the domain model. Defaults
57
+ # to +true+.
58
+ class Diagram
59
+ class << self
60
+ # Generates a new domain model based on all <tt>ActiveRecord::Base</tt>
61
+ # subclasses, and creates a new diagram. Use the given options for both
62
+ # the domain generation and the diagram generation.
63
+ def create(options = {})
64
+ new(Domain.generate(options), options).create
65
+ end
66
+
67
+ protected
68
+
69
+ def setup(&block)
70
+ callbacks[:setup] = block
71
+ end
72
+
73
+ def each_entity(&block)
74
+ callbacks[:each_entity] = block
75
+ end
76
+
77
+ def each_relationship(&block)
78
+ callbacks[:each_relationship] = block
79
+ end
80
+
81
+ def save(&block)
82
+ callbacks[:save] = block
83
+ end
84
+
85
+ private
86
+
87
+ def callbacks
88
+ @callbacks ||= Hash.new { proc {} }
89
+ end
90
+ end
91
+
92
+ # The options that are used to create this diagram.
93
+ attr_reader :options
94
+
95
+ # The domain that this diagram represents.
96
+ attr_reader :domain
97
+
98
+ # Create a new diagram based on the given domain.
99
+ def initialize(domain, options = {})
100
+ @domain, @options = domain, DBDiagram.options.merge(options)
101
+ end
102
+
103
+ # Generates and saves the diagram, returning the result of +save+.
104
+ def create
105
+ generate
106
+ save
107
+ end
108
+
109
+ # Generates the diagram, but does not save the output. It is called
110
+ # internally by Diagram#create.
111
+ def generate
112
+ instance_eval(&callbacks[:setup])
113
+ if options.only_recursion_depth.present?
114
+ depth = options.only_recursion_depth.to_s.to_i
115
+ options[:only].dup.each do |class_name|
116
+ options[:only]+= recurse_into_relationships(@domain.entity_by_name(class_name), depth)
117
+ end
118
+ options[:only].uniq!
119
+ end
120
+
121
+ filtered_entities.each do |entity|
122
+ instance_exec entity, filtered_attributes(entity), &callbacks[:each_entity]
123
+ end
124
+
125
+ filtered_relationships.each do |relationship|
126
+ instance_exec relationship, &callbacks[:each_relationship]
127
+ end
128
+ end
129
+
130
+ def recurse_into_relationships(entity, max_level, current_level = 0)
131
+ return [] unless entity
132
+ return [] if max_level == current_level
133
+
134
+ relationships = entity.relationships.reject{|r| r.recursive?}
135
+
136
+ relationships.map do |relationship|
137
+ other_entity = if relationship.source == entity
138
+ relationship.destination
139
+ else
140
+ relationship.source
141
+ end
142
+ if other_entity and !other_entity.generalized?
143
+ [other_entity.name] + recurse_into_relationships(other_entity, max_level, current_level + 1)
144
+ else
145
+ []
146
+ end
147
+ end.flatten.uniq
148
+ end
149
+
150
+ def save
151
+ instance_eval(&callbacks[:save])
152
+ end
153
+
154
+ private
155
+
156
+ def callbacks
157
+ @callbacks ||= self.class.send(:callbacks)
158
+ end
159
+
160
+ def filtered_entities
161
+ @domain.entities.reject { |entity|
162
+ options.exclude.present? && [options.exclude].flatten.map(&:to_sym).include?(entity.name.to_sym) or
163
+ options[:only].present? && entity.model && ![options[:only]].flatten.map(&:to_sym).include?(entity.name.to_sym)
164
+ }.compact.tap do |entities|
165
+ raise "No entities found; create your models first!" if entities.empty?
166
+ end
167
+ end
168
+
169
+ def filtered_relationships
170
+ @domain.relationships
171
+ end
172
+
173
+ def filtered_attributes(entity)
174
+ entity.attributes.reject { |attribute|
175
+ # Select attributes that satisfy the conditions in the :attributes option.
176
+ [*options.attributes].none? { |type| attribute.send(:"#{type.to_s.chomp('s')}?") }
177
+ }
178
+ end
179
+
180
+ def warn(message)
181
+ puts "Warning: #{message}" if options.warn
182
+ end
183
+ end
184
+ end