Parent

Files

DataMapper::Visualizer::Project

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

Attributes

bundle[R]

Specifies which Bundler groups to activate.

include_dirs[R]

The directories to include

require_globs[R]

The path glob patterns to require

require_paths[R]

The paths to require

Public Class Methods

new(options={}) click to toggle source

Creates a new project.

@param [Hash] options

Additional options.

@option options [Array] :include

The directories to include into the `$LOAD_PATH` global variable.

@option options [Boolean, Array] :bundle

Specifies which groups of dependencies to activate using Bundler.

@option options [Array] :require

The paths to require.

@option options [Array] :require_all

The path globs to require.
# File lib/dm-visualizer/project.rb, line 43
def initialize(options={})
  @bundle = nil
  @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

  if options[:bundle]
    @bundle = Set[]

    options[:bundle].each do |group|
      @bundle << group.to_sym
    end
  end

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

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

Public Instance Methods

activate!() click to toggle source

Activates the project by adding it's include directories to the `$LOAD_PATH` global variable.

@return [true]

# File lib/dm-visualizer/project.rb, line 105
def activate!
  @include_dirs.each do |dir|
    $LOAD_PATH << dir if File.directory?(dir)
  end

  # use Bundler if a Gemfile is present
  bundle! if (@bundle && File.file?('Gemfile'))

  return true
end
bundle!() click to toggle source

Activates dependencies of the project using Bundler.

@return [true]

# File lib/dm-visualizer/project.rb, line 77
def bundle!
  unless File.file?('Gemfile')
    log "Gemfile is missing or not a valid file."
  end

  begin
    require 'bundler'
  rescue LoadError => e
    log "Gemfile exists, but bundler is not installed"
    log "Run `gem install bundler` to install bundler."
  end

  begin
    Bundler.setup(*@bundle)
  rescue Bundler::BundleError => e
    log e.message
    log "Run `bundle install` to install missing gems"
  end

  return true
end
deactivate!() click to toggle source

De-activates the project by removing it's include directories to the `$LOAD_PATH` global variable.

@return [true]

# File lib/dm-visualizer/project.rb, line 122
def deactivate!
  $LOAD_PATH.reject! { |dir| @include_dirs.include?(dir) }
  return true
end
each_foreign_key(model) click to toggle source

Enumerates over every foreign-key in a given model.

@param [DataMapper::Model] model

The given model.

@yield [foreign_key, foreign_model]

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

@yieldparam [String] foreign_key

The name of the foreign-key.

@yieldparam [DataMapper::Model] foreign_model

The model that the foreign-key references.

@return [Enumerator]

If no block is given, an Enumerator object will be returned.
# File lib/dm-visualizer/project.rb, line 256
def each_foreign_key(model)
  unless block_given?
    return Enumerator.new(self,:each_foreign_key,model)
  end

  model.relationships.each_value do |relationship|
    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(&block) click to toggle source

Enumerates over each DataMapper Model loaded from the project.

@yield [model]

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

@yieldparam [DataMapper::Model]

A model loaded from the project.

@return [Enumerator]

If no block is given, an Enumerator object will be returned.
# File lib/dm-visualizer/project.rb, line 176
def each_model(&block)
  DataMapper::Model.descendants.each(&block)
end
each_model_inheritence() click to toggle source

Enumerates over each DataMapper Model loaded from the project, and their direct ancestors.

@yield [model, direct_ancestor]

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

@yieldparam [DataMapper::Model] model

The model.

@yieldparam [DataMapper::Model] direct_ancestor

The first ancestor of the model.

@return [Enumerator]

If no block is given, an Enumerator object will be returned.
# File lib/dm-visualizer/project.rb, line 197
def each_model_inheritence
  unless block_given?
    return Enumerator.new(self,:each_model_inheritence)
  end

  each_model do |model|
    direct_ancestor = model.ancestors[1]

    if direct_ancestor.class == Class
      yield model, direct_ancestor
    end
  end
end
each_property(model) click to toggle source

Enumerates over each DataMapper property from a given model.

@param [DataMapper::Model] model

The given model.

@yield [property]

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

@yieldparam [DataMapper::Property] property

The property.

@return [Enumerator]

If no block is given, an Enumerator object will be returned.
# File lib/dm-visualizer/project.rb, line 227
def each_property(model)
  unless block_given?
    return Enumerator.new(self,:each_property,model)
  end

  model.properties.each do |property|
    yield property
  end
end
each_relationship() click to toggle source

Enumerates over each DataMapper relationship between each model.

@yield [relationship,model]

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

@yieldparam [DataMapper::Relationship] relationship

The relationship.

@yieldparam [DataMapper::Model] model

The model that the relationship belongs to.

@return [Enumerator]

If no block is given, an Enumerator object will be returned.
# File lib/dm-visualizer/project.rb, line 289
def each_relationship
  return Enumerator.new(self,:each_relationship) unless block_given?

  each_model do |model|
    model.relationships.each_value do |relationship|
      unless relationship.respond_to?(:through)
        yield relationship, model
      end
    end
  end
end
load!() click to toggle source

Attempts to load all of the projects files.

@return [true]

# File lib/dm-visualizer/project.rb, line 132
def load!
  activate!

  @require_paths.each do |path|
    begin
      require path
    rescue LoadError => e
      log "dm-visualizer: unable to load #{path}"
      log "dm-visualizer: #{e.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 => e
          log "dm-visualizer: unable to load #{relative_path} from #{dir}"
          log "dm-visualizer: #{e.message}"
        end
      end
    end
  end

  deactivate!
  return true
end

Protected Instance Methods

log(message) click to toggle source

Prints a message to `STDERR`.

@param [String] message

The message to print.
# File lib/dm-visualizer/project.rb, line 309
def log(message)
  STDERR.puts "dm-visualizer: #{message}"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.