Class: Arcade::Vertex

Inherits:
Base
  • Object
show all
Extended by:
Support::Model
Includes:
Support::Model
Defined in:
lib/model/vertex.rb

Direct Known Subclasses

Basicvertex

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Model

_allocate_model, resolve_edge_name

Methods inherited from Base

#==, all, count, create_type, database_name, #delete, drop_type, find, first, insert, #insert_document, #inspect, #invariant_attributes, last, #method_missing, not_permitted, properties, #query, query, #rid?, timestamps, #to_html, #to_json, #to_or, update, #update, update!, #update_embedded, #update_list, #update_map, upsert, where

Methods included from Support::Sql

#compose_where, #generate_sql_list

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Arcade::Base

Class Method Details

.create(timestamp: true, **args) ⇒ Object

Creates a Vertex-Instance.

Similar to `Vertex#insert`.

Difference is the presence of a `created` property, a timestamp set to the time and date of creation.


48
49
50
51
52
# File 'lib/model/vertex.rb', line 48

def self.create timestamp: true, **args
  #t= timestamp ?  ", created = Date(#{DateTime.now.to_i}) "  : ""
  t= timestamp ?  ", created = sysdate() "  : ""
  db.execute { "create VERTEX #{database_name} set #{args.map{|x,y| [x,y.to_or].join("=")}.join(', ')+t}" } &.first.allocate_model(false)
end

.delete(where: {}, **args) ⇒ Object

Vertex.delete fires a “delete vertex” command to the database.

To remove all records  use  »all: true« as argument

To remove a specific rid, use  rid: "#nn:mmm" as argument

"where" parameter is optional

 ExtraNode.delete where: { item: 67  }  == ExtraNode.delete item: 67


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/model/vertex.rb', line 28

def self.delete where: {} , **args
  if args[:all] == true
    where = {}
  elsif args[:rid].present?
    return db.execute { "delete vertex #{args[:rid]}" }.first["count"]
  else
    where.merge!(args) if where.is_a?(Hash)
    return 0 if where.empty?
  end
  # query returns [{count => n }]
  db.execute { "delete vertex #{database_name} #{compose_where(where)}"  } &.first[:count] rescue 0
end

.nodes(in_or_out = :both, via: nil, **args) ⇒ Object



58
59
60
61
62
63
# File 'lib/model/vertex.rb', line 58

def self.nodes in_or_out = :both, via: nil ,  **args

  s =  Query.new from: self
  s.nodes in_or_out, via: via, **args
  s.query &.select_result
end

Instance Method Details

#accepted_methodsObject

#



11
12
13
# File 'lib/model/vertex.rb', line 11

def accepted_methods
 super + [ :in, :out, :both, :edges, :inE, :outE, :bothE,  :assign]
end

#assign(vertex:, via:, **attributes) ⇒ Object

Assigns another Vertex via an EdgeClass. If specified, puts attributes on the edge.

Returns the reloaded assigned vertex

Wrapper for

Edge.create in: self, out: a_vertex,  some: attributes.  on: the,  edge: type }

returns the assigned vertex, thus enabling to chain vertices through

Vertex.assign() via: E , vertex: VertexClass.create()).assign( via: E, ... )

or (1..100).each{|n| vertex = vertex.assign(via: E2, vertex: V2.create(item: n))}



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/model/vertex.rb', line 184

def assign vertex: , via:   , **attributes

  via.create from: self, to: vertex,  **attributes

  db.get vertex.rid  # return the assigned vertex
rescue IndexError => e
  db.logger.error "Edge not created, already present."
  vertex  #  return the vertex (for chaining)
rescue ArgumentError => e
  db.logger.error "ArgumentError: #{e.message}"
  nil
end

#both(count = 0, via: nil) ⇒ Object

get all Vertices connected by edges of type via



112
113
114
115
116
117
118
# File 'lib/model/vertex.rb', line 112

def both count=0, via:nil
  if count.zero?
    @bufferedboth ||= nodes :both, 1, via: via
  else
    nodes :both, count, via: via   # not cached
  end
end

#bothE(count = 1, via: nil) ⇒ Object

get all via-type-edges



132
133
134
# File 'lib/model/vertex.rb', line 132

def bothE  count=1, via:nil
  nodes :bothE, count, via: via
end

#edges(in_or_out = :both, depth = 1, via: nil, execute: true, **args) ⇒ Object

Supports where: { where condition for edges }



87
88
89
90
# File 'lib/model/vertex.rb', line 87

def edges in_or_out = :both, depth= 1, via: nil , execute: true, **args
  in_or_out = in_or_out.to_s + "E"
  nodes in_or_out, depth, via: via , execute: execute, **args
end

#expandObject

# ——————————— Instance Methods ——————————— ##

We need expand as fallback if a vertex, which is stored as link, is automatically loaded


71
72
73
# File 'lib/model/vertex.rb', line 71

def expand
  self
end

#in(count = 0, via: nil) ⇒ Object

get Vertices through in by edges of type via



94
95
96
97
98
99
100
# File 'lib/model/vertex.rb', line 94

def in count=0, via:nil
  if count.zero?
    @bufferedin ||= nodes :in,  1, via: via
  else
    nodes :in, count, via: via   # not cached
  end
end

#inE(count = 1, via: nil) ⇒ Object

get via-type-edges through in



122
123
124
# File 'lib/model/vertex.rb', line 122

def inE count=1, via:nil
  nodes :inE,  count, via: via
end

#nodes(in_or_out = :both, depth = 1, via: nil, execute: true, **args) ⇒ Object

Supports where: –> Strategie.first nodes where: 10 “select both()[ size = 10 ] from #113:8 ”



76
77
78
79
80
81
82
83
84
# File 'lib/model/vertex.rb', line 76

def nodes in_or_out=:both, depth= 1, via: nil , execute: true, **args
  s =  Query.new from: rid
  s.nodes in_or_out, via: via, **args
  if execute
     s.query &.select_result
   else
     s  #  just return the query
   end
end

#out(count = 0, via: nil) ⇒ Object

get Vertices through out by edges of type via



103
104
105
106
107
108
109
# File 'lib/model/vertex.rb', line 103

def out count=0, via:nil
  if count.zero?
    @bufferedout ||= nodes :out, 1,  via: via
  else
    nodes :out, count, via: via   # not cached
  end
end

#outE(count = 1, via: nil) ⇒ Object

get via-type-edges through out



127
128
129
# File 'lib/model/vertex.rb', line 127

def outE count=1, via:nil
  nodes :outE, count,  via: via
end

#refreshObject



228
229
230
231
232
233
# File 'lib/model/vertex.rb', line 228

def refresh
  # force reloading of edges and nodes
  # edges are not cached (now)
  @bufferedin, @bufferedout, @bufferedboth = nil
  super
end

#removeObject



198
199
200
# File 'lib/model/vertex.rb', line 198

def remove
   db.execute{ "delete vertex #{rid}" }
end

#to_humanObject

Human readable representation of Vertices

Format: < Classname: Edges, Attributes >



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/model/vertex.rb', line 206

def to_human

   in_and_out = -> { "{#{self.in.count}->}{->#{self.out.count }}, " }

	#Default presentation of Arcade::Base::Model-Objects

	"<#{self.class.to_s.snake_case}[#{rid}]:"  +
     in_and_out[] +
     invariant_attributes.map do |attr, value|
		v= case value
			 when  Class
				 "< #{self.class.to_s.snake_case}: #{value.rid} >"
			 when Array
				 value.to_s
			 else
				 value.from_db
			 end
		"%s: %s" % [ attr, v]  unless v.nil?
	end.compact.sort.join(', ') + ">".gsub('"' , ' ')
end

#traverse(in_or_out = :out, via: nil, depth: 1, execute: true, start_at: 0, where: nil) ⇒ Object

e.i.

traverse( :in, via: [TG::DateOf, Arcade::HasOrder], depth: 4, start_at: 1 ).map(&:w).reverse


154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/model/vertex.rb', line 154

def traverse in_or_out = :out, via: nil,  depth: 1, execute: true, start_at: 0, where: nil

	the_query = query kind: 'traverse'
    the_query.projection  in_or_out.to_s + "(" + resolve_edge_name(*via) + ")"
	the_query.where where if where.present?
	the_query.while "$depth < #{depth} " unless depth <=0
	outer_query = Query.new from: the_query, where: "$depth >= #{start_at}"
	if execute
      outer_query.execute.allocate_model
		else
#			the_query.from self  #  complete the query by assigning self
			the_query            #  returns the Query  -traverse object
		end
end