Class: DataMapper::Visualizer::Project
- Inherits:
-
Object
- Object
- DataMapper::Visualizer::Project
- Defined in:
- lib/dm-visualizer/project.rb
Overview
Defines the paths and directories to load for a DataMapper project.
Instance Attribute Summary collapse
-
#bundle ⇒ Object
readonly
Specifies which Bundler groups to activate.
-
#include_dirs ⇒ Object
readonly
The directories to include.
-
#require_globs ⇒ Object
readonly
The path glob patterns to require.
-
#require_paths ⇒ Object
readonly
The paths to require.
Instance Method Summary collapse
-
#activate! ⇒ true
Activates the project by adding it's include directories to the
$LOAD_PATHglobal variable. -
#bundle! ⇒ true
Activates dependencies of the project using Bundler.
-
#deactivate! ⇒ true
De-activates the project by removing it's include directories to the
$LOAD_PATHglobal variable. -
#each_foreign_key(model) {|foreign_key, foreign_model| ... } ⇒ Enumerator
Enumerates over every foreign-key in a given model.
-
#each_model {|model| ... } ⇒ Enumerator
Enumerates over each DataMapper Model loaded from the project.
-
#each_model_inheritence {|model, direct_ancestor| ... } ⇒ Enumerator
Enumerates over each DataMapper Model loaded from the project, and their direct ancestors.
-
#each_property(model) {|property| ... } ⇒ Enumerator
Enumerates over each DataMapper property from a given model.
-
#each_relationship {|relationship, model| ... } ⇒ Enumerator
Enumerates over each DataMapper relationship between each model.
-
#initialize(options = {}) ⇒ Project
constructor
Creates a new project.
-
#load! ⇒ true
Attempts to load all of the projects files.
-
#log(message) ⇒ Object
protected
Prints a message to
STDERR.
Constructor Details
#initialize(options = {}) ⇒ Project
Creates a new project.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dm-visualizer/project.rb', line 41 def initialize(={}) @bundle = Set[] @include_dirs = Set[] @require_paths = Set[] @require_globs = Set[] if [:include] [:include].each do |dir| @include_dirs << File.(dir) end end case [:bundle] when String, Symbol @bundle << [:bundle].to_sym when Enumerable [:bundle].each do |group| @bundle << group.to_sym end when true @bundle << :default end if [:require] @require_paths += [:require] end if [:require_all] @require_globs += [:require_all] end end |
Instance Attribute Details
#bundle ⇒ Object (readonly)
Specifies which Bundler groups to activate.
12 13 14 |
# File 'lib/dm-visualizer/project.rb', line 12 def bundle @bundle end |
#include_dirs ⇒ Object (readonly)
The directories to include
15 16 17 |
# File 'lib/dm-visualizer/project.rb', line 15 def include_dirs @include_dirs end |
#require_globs ⇒ Object (readonly)
The path glob patterns to require
21 22 23 |
# File 'lib/dm-visualizer/project.rb', line 21 def require_globs @require_globs end |
#require_paths ⇒ Object (readonly)
The paths to require
18 19 20 |
# File 'lib/dm-visualizer/project.rb', line 18 def require_paths @require_paths end |
Instance Method Details
#activate! ⇒ true
Activates the project by adding it's include directories to the
$LOAD_PATH global variable.
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/dm-visualizer/project.rb', line 106 def activate! @include_dirs.each do |dir| $LOAD_PATH << dir if File.directory?(dir) end # use Bundler if a Gemfile is present bundle! unless @bundle.empty? return true end |
#bundle! ⇒ true
Activates dependencies of the project using Bundler.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dm-visualizer/project.rb', line 78 def bundle! unless File.file?('Gemfile') log "Gemfile is missing or not a valid file." end begin require 'bundler' rescue LoadError log "Gemfile exists, but bundler is not installed" log "Run `gem install bundler` to install bundler." end begin Bundler.setup(*@bundle) rescue Bundler::BundleError => error log error. log "Run `bundle install` to install missing gems" end return true end |
#deactivate! ⇒ true
De-activates the project by removing it's include directories to the
$LOAD_PATH global variable.
123 124 125 126 |
# File 'lib/dm-visualizer/project.rb', line 123 def deactivate! $LOAD_PATH.reject! { |dir| @include_dirs.include?(dir) } return true end |
#each_foreign_key(model) {|foreign_key, foreign_model| ... } ⇒ Enumerator
Enumerates over every foreign-key in a given model.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/dm-visualizer/project.rb', line 229 def each_foreign_key(model) return enum_for(:each_foreign_key,model) unless block_given? # XXX: in dm-core 1.1.0, `Model#relationships` returns a # `DataMapper::RelationshipSet`, instead of a `Mash`, which does # not provide the `each_value` method. model.relationships.each do |*args| relationship = args.last next if relationship.respond_to?(:through) case relationship when Associations::ManyToOne::Relationship, Associations::OneToOne::Relationship yield relationship.child_key.first.name, relationship.parent_model end end end |
#each_model {|model| ... } ⇒ Enumerator
Enumerates over each DataMapper Model loaded from the project.
177 178 179 |
# File 'lib/dm-visualizer/project.rb', line 177 def each_model(&block) DataMapper::Model.descendants.each(&block) end |
#each_model_inheritence {|model, direct_ancestor| ... } ⇒ Enumerator
Enumerates over each DataMapper Model loaded from the project, and their direct ancestors.
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/dm-visualizer/project.rb', line 198 def each_model_inheritence return enum_for(:each_model_inheritence) unless block_given? each_model do |model| direct_ancestor = model.ancestors[1] if direct_ancestor.class == Class yield model, direct_ancestor end end end |
#each_property(model) {|property| ... } ⇒ Enumerator
Enumerates over each DataMapper property from a given model.
265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/dm-visualizer/project.rb', line 265 def each_property(model) return enum_for(:each_property,model) unless block_given? foreign_keys = Set[] each_foreign_key(model) do |name,parent_model| foreign_keys << name end model.properties.each do |property| yield property unless foreign_keys.include?(property.name) end end |
#each_relationship {|relationship, model| ... } ⇒ Enumerator
Enumerates over each DataMapper relationship between each model.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/dm-visualizer/project.rb', line 295 def each_relationship return enum_for(:each_relationship) unless block_given? each_model do |model| # XXX: in dm-core 1.1.0, `Model#relationships` returns a # `DataMapper::RelationshipSet`, instead of a `Mash`, which does # not provide the `each_value` method. model.relationships.each do |*args| relationship = args.last unless relationship.respond_to?(:through) yield relationship, model end end end end |
#load! ⇒ true
Attempts to load all of the projects files.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/dm-visualizer/project.rb', line 133 def load! activate! @require_paths.each do |path| begin require path rescue LoadError => error log "dm-visualizer: unable to load #{path}" log "dm-visualizer: #{error.message}" end end @require_globs.each do |glob| @include_dirs.each do |dir| Dir[File.join(dir,glob)].each do |path| relative_path = path[(dir.length + 1)..-1] begin require relative_path rescue LoadError => error log "dm-visualizer: unable to load #{relative_path} from #{dir}" log "dm-visualizer: #{error.message}" end end end end deactivate! return true end |
#log(message) ⇒ Object (protected)
Prints a message to STDERR.
320 321 322 |
# File 'lib/dm-visualizer/project.rb', line 320 def log() STDERR.puts "dm-visualizer: #{message}" end |