Class: NoSE::Model

Inherits:
Object show all
Includes:
Loader
Defined in:
lib/nose/model.rb

Overview

A conceptual data model of a set of entities

Constant Summary collapse

LOAD_PATH =

The subdirectory models are loaded from

'models'

Instance Attribute Summary collapse

Attributes included from Loader

#source_code

Instance Method Summary collapse

Methods included from Loader

included

Constructor Details

#initialize(&block) ⇒ Model

Returns a new instance of Model.



17
18
19
20
21
22
# File 'lib/nose/model.rb', line 17

def initialize(&block)
  @entities = {}

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

Instance Attribute Details

#entitiesObject (readonly)

Returns the value of attribute entities.



15
16
17
# File 'lib/nose/model.rb', line 15

def entities
  @entities
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare all entities

Returns:

  • (Boolean)


26
27
28
# File 'lib/nose/model.rb', line 26

def ==(other)
  other.is_a?(Model) && @entities = other.entities
end

#[](name) ⇒ Entity

Retrieve an entity by name

Returns:



33
34
35
36
# File 'lib/nose/model.rb', line 33

def [](name)
  return @entities[name] if @entities.key? name
  fail EntityNotFound
end

#add_entity(entity) ⇒ Entity

Add an Entity to the workload

Returns:



40
41
42
43
# 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

#find_field(field) ⇒ Field

Find a field given an Enumerable of identifiers

Returns:

  • (Field)


47
48
49
50
51
52
53
# 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) ⇒ Object

Output a PNG representation of entities in the model



56
57
58
59
60
61
62
# 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