class NoSE::Model

A conceptual data model of a set of entities

Constants

LOAD_PATH

The subdirectory models are loaded from

Attributes

entities[R]

Public Class Methods

new(&block) click to toggle source
# File lib/nose/model.rb, line 17
def initialize(&block)
  @entities = {}

  # Apply the DSL
  WorkloadDSL.new(self).instance_eval(&block) if block_given?
end

Public Instance Methods

==(other) click to toggle source

Compare all entities @return [Boolean]

# File lib/nose/model.rb, line 26
def ==(other)
  other.is_a?(Model) && @entities = other.entities
end
Also aliased as: eql?
[](name) click to toggle source

Retrieve an entity by name @return [Entity]

# File lib/nose/model.rb, line 33
def [](name)
  return @entities[name] if @entities.key? name
  fail EntityNotFound
end
add_entity(entity) click to toggle source

Add an {Entity} to the workload @return [Entity]

# File lib/nose/model.rb, line 40
def add_entity(entity)
  fail InvalidEntity, 'no primary key defined' if entity.id_field.nil?
  @entities[entity.name] = entity
end
eql?(other)
Alias for: ==
find_field(field) click to toggle source

Find a field given an Enumerable of identifiers @return [Field]

# File lib/nose/model.rb, line 47
def find_field(field)
  if field.count > 2
    find_field_chain field
  else
    find_entity_field(*field)
  end
end
output(format, filename, include_fields = false) click to toggle source

Output a PNG representation of entities in the model

# File lib/nose/model.rb, line 56
def output(format, filename, include_fields = false)
  graph = GraphViz.new :G, type: :digraph
  nodes = add_graph_nodes graph, include_fields
  add_graph_edges graph, nodes

  graph.output(**{ format => filename })
end

Private Instance Methods

add_graph_edges(graph, nodes) click to toggle source

Add the edges (foreign keys) to a GraphViz object

# File lib/nose/model.rb, line 82
def add_graph_edges(graph, nodes)
  @entities.each_value do |entity|
    entity.foreign_keys.each_value do |key|
      graph.add_edges nodes[entity.name], nodes[key.entity.name]
    end
  end
end
add_graph_nodes(graph, include_fields) click to toggle source

Add the nodes (entities) to a GraphViz object

# File lib/nose/model.rb, line 67
def add_graph_nodes(graph, include_fields)
  Hash[@entities.each_value.map do |entity|
    label = "#{entity.name}\n"
    if include_fields
      label += entity.fields.each_value.map do |field|
        type = field.class.name.sub(/^NoSE::(.*?)(Field)?$/, '\1')
        "#{field.name}: #{type}"
      end.join("\n")
    end

    [entity.name, graph.add_nodes(label)]
  end]
end
find_entity_field(entity, field) click to toggle source

Find a field in an entity where the entity may be a string or an object

# File lib/nose/model.rb, line 100
def find_entity_field(entity, field)
  entity = entities[entity] if entity.is_a?(String)
  entity[field]
end
find_field_chain(field) click to toggle source

Find a field in an entity where the entity may be a string or an object

# File lib/nose/model.rb, line 91
def find_field_chain(field)
  # Do a foreign key lookup
  field = field.dup
  key_field = @entities[field[0]][field[1]]
  field[0..1] = key_field ? key_field.entity.name : field[1]
  find_field field
end