Class: DataMapper::Visualizer::Visualization

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

Overview

The base class for all visualizations.

Direct Known Subclasses

GraphViz

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Visualization

Initializes a new visualization.

Parameters:

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

    Additional options.

Options Hash (options):

  • :repository_names (Hash)

    The actual names of the DataMapper repositories.

  • :repository_name (String)

    The actual name to use for the :default DataMappe repository.

  • :naming (Symbol)

    The naming convention to use. May be either :relational or :schema.

  • :full_names (Boolean)

    Specifies whether to demodulize class names.



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
# File 'lib/dm-visualizer/visualization.rb', line 44

def initialize(options={})
  @project = Project.new(options)

  @naming     = :relational
  @full_names = false

  @repository_names = {}

  if options[:repository_names]
    options[:repository_names].each do |name,db_name|
      @repository_names[name.to_sym] = db_name.to_s
    end
  end

  if options[:repository_name]
    @database_names[:default] = options[:repository_name].to_s
  end

  if options[:naming]
    @naming = options[:naming].to_sym
  end

  if options.has_key?(:full_names)
    @full_names  = options[:full_names]
  end
end

Instance Attribute Details

#full_namesObject

Specifies whether to demodulize class names.



23
24
25
# File 'lib/dm-visualizer/visualization.rb', line 23

def full_names
  @full_names
end

#namingObject

Specifies which naming convention to use (:relational or :schema).



20
21
22
# File 'lib/dm-visualizer/visualization.rb', line 20

def naming
  @naming
end

#projectObject (readonly)

The project that will be visualized



13
14
15
# File 'lib/dm-visualizer/visualization.rb', line 13

def project
  @project
end

#repository_namesObject (readonly)

Mapping of DataMapper repository names and their actual names.



16
17
18
# File 'lib/dm-visualizer/visualization.rb', line 16

def repository_names
  @repository_names
end

Instance Method Details

#class_name(obj) ⇒ String

Returns the class name of a given object.

Parameters:

  • obj (Class, Object)

    The object or class.

Returns:

  • (String)

    The class name of the object or class.



80
81
82
83
84
85
86
87
88
89
# File 'lib/dm-visualizer/visualization.rb', line 80

def class_name(obj)
  name = if (obj.class == Class || obj.class == Module)
           obj.name
         else
           obj.class.name
         end

  name = DataMapper::Inflector.demodulize(name) unless @full_names
  return name
end

#foreign_key_name(key) ⇒ String

Returns the name the given foreign key.

Parameters:

  • key (Symbol)

    The foreign key.

Returns:

  • (String)

    The foreign key name.



113
114
115
116
117
118
# File 'lib/dm-visualizer/visualization.rb', line 113

def foreign_key_name(key)
  key = key.to_s

  key.chomp!('_id') unless @naming == :schema
  return key
end

#model_name(model) ⇒ String

Returns the name of a model.

Parameters:

  • model (DataMapper::Model)

    The model.

Returns:

  • (String)

    The name of the model.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dm-visualizer/visualization.rb', line 155

def model_name(model)
  if @naming == :schema
    name         = model_repository_name(model)
    storage_name = model.storage_names[:default]
    storage_name ||= NamingConventions::Resource::UnderscoredAndPluralized.call(model.name)

    if name
      "#{name}.#{storage_name}"
    else
      storage_name
    end
  else
    class_name(model)
  end
end

#model_repository_name(model) ⇒ String

Returns the repository name of a model.

Parameters:

  • model (DataMapper::Model)

    The model.

Returns:

  • (String)

    The repository name.



142
143
144
# File 'lib/dm-visualizer/visualization.rb', line 142

def model_repository_name(model)
  @repository_names[model.default_repository_name]
end

#property_name(property) ⇒ String

Returns the name of a given property.

Parameters:

  • property (DataMapper::Property)

    The property.

Returns:

  • (String)

    The property name.



100
101
102
# File 'lib/dm-visualizer/visualization.rb', line 100

def property_name(property)
  property.name.to_s
end

#property_type_name(property) ⇒ String

Returns the type name of a property.

Parameters:

  • property (DataMapper::Property)

    The property.

Returns:

  • (String)

    The property type name.



129
130
131
# File 'lib/dm-visualizer/visualization.rb', line 129

def property_type_name(property)
  class_name(property.class)
end

#visualizeObject (protected)

Default method which visualizes the DataMapper models, properties and relationships.

Parameters:

  • project (Project)

    The project to visualize.



192
193
# File 'lib/dm-visualizer/visualization.rb', line 192

def visualize
end

#visualize!(*arguments) ⇒ Object

Loads the project and visualizes it.

Parameters:

  • arguments (Array)

    Additional arguments to pass to #visualize.



177
178
179
180
181
# File 'lib/dm-visualizer/visualization.rb', line 177

def visualize!(*arguments)
  @project.load!

  visualize(*arguments)
end