Class: DataMapper::Visualizer::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-visualizer/project.rb

Overview

Defines the paths and directories to load for a DataMapper project.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Project

Creates a new project.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :include (Array)

    The directories to include into the $LOAD_PATH global variable.

  • :bundle (Enumerable, Symbol, String, Boolean)

    Specifies which groups of dependencies to activate using Bundler.

  • :require (Array)

    The paths to require.

  • :require_all (Array)

    The path globs to require.



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(options={})
  @bundle = Set[]
  @include_dirs = Set[]
  @require_paths = Set[]
  @require_globs = Set[]

  if options[:include]
    options[:include].each do |dir|
      @include_dirs << File.expand_path(dir)
    end
  end

  case options[:bundle]
  when String, Symbol
    @bundle << options[:bundle].to_sym
  when Enumerable
    options[:bundle].each do |group|
      @bundle << group.to_sym
    end
  when true
    @bundle << :default
  end

  if options[:require]
    @require_paths += options[:require]
  end

  if options[:require_all]
    @require_globs += options[:require_all]
  end
end

Instance Attribute Details

#bundleObject (readonly)

Specifies which Bundler groups to activate.



12
13
14
# File 'lib/dm-visualizer/project.rb', line 12

def bundle
  @bundle
end

#include_dirsObject (readonly)

The directories to include



15
16
17
# File 'lib/dm-visualizer/project.rb', line 15

def include_dirs
  @include_dirs
end

#require_globsObject (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_pathsObject (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.

Returns:

  • (true)


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.

Returns:

  • (true)


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

Returns:

  • (true)


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.

Parameters:

  • model (DataMapper::Model)

    The given model.

Yields:

  • (foreign_key, foreign_model)

    The given block will be passed every foreign-key and the model that the foreign-key will reference.

Yield Parameters:

  • foreign_key (String)

    The name of the foreign-key.

  • foreign_model (DataMapper::Model)

    The model that the foreign-key references.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# 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.
  each_relationship_for(model) do |relationship|
    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.

Yields:

  • (model)

    The given block will be passed every model registered with DataMapper.

Yield Parameters:

  • A (DataMapper::Model)

    model loaded from the project.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



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.

Yields:

  • (model, direct_ancestor)

    The given block will be passed every model and their immediate ancestor.

Yield Parameters:

  • model (DataMapper::Model)

    The model.

  • direct_ancestor (DataMapper::Model)

    The first ancestor of the model.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



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.

Parameters:

  • model (DataMapper::Model)

    The given model.

Yields:

  • (property)

    The given block will be passed every property from the given model.

Yield Parameters:

  • property (DataMapper::Property)

    The property.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/dm-visualizer/project.rb', line 261

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.

Yields:

  • (relationship, model)

    The given block will be passed every relationship from every model registered with DataMapper.

Yield Parameters:

  • relationship (DataMapper::Relationship)

    The relationship.

  • model (DataMapper::Model)

    The model that the relationship belongs to.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



291
292
293
294
295
296
297
298
299
# File 'lib/dm-visualizer/project.rb', line 291

def each_relationship
  return enum_for(:each_relationship) unless block_given?

  each_model do |model|
    each_relationship_for(model) do |relationship|
      yield relationship, model
    end
  end
end

#each_relationship_for(model) {|relationship| ... } ⇒ Object (protected)

Enumerates over each DataMapper relationship in a model.

Yields:

  • (relationship)

    The given block will be passed each relationship in the model.

Yield Parameters:

  • relationship (DataMapper::Relationship)

    A relationship.

Since:

  • 0.2.1



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/dm-visualizer/project.rb', line 324

def each_relationship_for(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 = case args
                   when Array
                     args.last
                   else
                     args
                   end

    unless relationship.respond_to?(:through)
      yield relationship
    end
  end
end

#load!true

Attempts to load all of the projects files.

Returns:

  • (true)


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.

Parameters:

  • message (String)

    The message to print.



309
310
311
# File 'lib/dm-visualizer/project.rb', line 309

def log(message)
  STDERR.puts "dm-visualizer: #{message}"
end