Class: Guacamole::Transaction::TxEdgeCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/guacamole/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ea, model) ⇒ TxEdgeCollection

Returns a new instance of TxEdgeCollection.



15
16
17
18
19
20
21
# File 'lib/guacamole/transaction.rb', line 15

def initialize(ea, model)
  @ea              = ea
  @model           = model
  @edge_collection = EdgeCollection.for(ea.edge_class)

  init
end

Instance Attribute Details

#eaObject (readonly)

Returns the value of attribute ea.



13
14
15
# File 'lib/guacamole/transaction.rb', line 13

def ea
  @ea
end

#edge_collectionObject (readonly)

Returns the value of attribute edge_collection.



13
14
15
# File 'lib/guacamole/transaction.rb', line 13

def edge_collection
  @edge_collection
end

#from_modelsObject (readonly)

Returns the value of attribute from_models.



13
14
15
# File 'lib/guacamole/transaction.rb', line 13

def from_models
  @from_models
end

#modelObject (readonly)

Returns the value of attribute model.



13
14
15
# File 'lib/guacamole/transaction.rb', line 13

def model
  @model
end

#old_edgesObject (readonly)

Returns the value of attribute old_edges.



13
14
15
# File 'lib/guacamole/transaction.rb', line 13

def old_edges
  @old_edges
end

#to_modelsObject (readonly)

Returns the value of attribute to_models.



13
14
15
# File 'lib/guacamole/transaction.rb', line 13

def to_models
  @to_models
end

Instance Method Details

#edge_collection_for_transactionObject



82
83
84
85
86
87
88
89
90
# File 'lib/guacamole/transaction.rb', line 82

def edge_collection_for_transaction
  {
    name: edge_collection.collection_name,
    fromVertices: from_vertices,
    toVertices: to_vertices_with_only_existing_documents,
    edges: edges,
    oldEdges: old_edges
  }
end

#edgesObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/guacamole/transaction.rb', line 70

def edges
  from_vertices.each_with_object([]) do |from_vertex, edges|
    to_vertices.each do |to_vertex|
      edges << {
        _from: from_vertex[:_id] || from_vertex[:object_id],
        _to: to_vertex[:_id] || to_vertex[:object_id],
        attributes: {}
      }
    end
  end
end

#from_verticesObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/guacamole/transaction.rb', line 42

def from_vertices
  from_models.map do |m|
    {
      object_id: m.object_id,
      collection: edge_collection.edge_class.from_collection.collection_name,
      document: select_mapper.call(m).model_to_document(m),
      _key: m.key,
      _id: m._id
    }
  end
end

#initObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/guacamole/transaction.rb', line 23

def init
  case model
  when ea.edge_class.from_collection.model_class
    @from_models = [model]
    @to_models   = [ea.get_value(model)].compact.flatten
    @old_edges   = edge_collection.by_example(_from: model._id).map(&:key)
  when ea.edge_class.to_collection.model_class
    @to_models   = [model]
    @from_models = [ea.get_value(model)].compact.flatten
    @old_edges   = edge_collection.by_example(_to: model._id).map(&:key)
  else
    raise RuntimeError
  end
end

#select_mapperObject



38
39
40
# File 'lib/guacamole/transaction.rb', line 38

def select_mapper
  ->(m) { edge_collection.mapper_for_start(m) }
end

#to_verticesObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/guacamole/transaction.rb', line 54

def to_vertices
  to_models.map do |m|
    {
      object_id: m.object_id,
      collection: edge_collection.edge_class.to_collection.collection_name,
      document: select_mapper.call(m).model_to_document(m),
      _key: m.key,
      _id: m._id
    }
  end
end

#to_vertices_with_only_existing_documentsObject



66
67
68
# File 'lib/guacamole/transaction.rb', line 66

def to_vertices_with_only_existing_documents
  to_vertices.select { |v| v[:_key].nil?  }
end