Class: V

Inherits:
ActiveOrient::Model show all
Defined in:
lib/model/vertex.rb

Constant Summary

Constants included from OrientDB

OrientDB::DocumentDatabase, OrientDB::DocumentDatabasePool, OrientDB::DocumentDatabasePooled, OrientDB::GraphDatabase, OrientDB::IndexType, OrientDB::OClassImpl, OrientDB::OTraverse, OrientDB::PropertyImpl, OrientDB::RemoteStorage, OrientDB::SQLCommand, OrientDB::SQLSynchQuery, OrientDB::Schema, OrientDB::SchemaProxy, OrientDB::SchemaType, OrientDB::ServerAdmin, OrientDB::User, OrientDB::UsingJava

Instance Attribute Summary

Attributes inherited from ActiveOrient::Model

#metadata

Attributes inherited from ActiveOrient::Base

#metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveOrient::Model

_to_partial_path, autoload_object, delete_class, #document, #persisted?, #to_ary, to_or, use_or_allocate

Methods included from CustomClass

#like

Methods included from ModelClass

#add_edge_link, #all, #alter_property, #classname, #count, #create, #create_index, #create_properties, #create_property, #custom_where, #delete_property, #delete_record, #delete_records, #first, #get, #get_records, #indexes, #last, #link_list, #match, #namespace_prefix, #naming_convention, #orientdb_class, #print_properties, #properties, #query_database, #remove, #require_model_file, #update_all, #upsert, #where

Methods included from OrientSupport::Support

#compose_where, #generate_sql_list

Methods included from ModelRecord

classname, #find, #from_orient, #has_property?, #increment_version, #is_edge?, #method_missing, #properties, #query, #reload!, #remove, #rid, #rrid, #save, #to_or, #transfer_content, #update, #update_attribute, #update_attributes, #version, #version=

Methods included from ActiveOrient::BaseProperties

#==, #content_attributes, #default_attributes, #embedded, #set_attribute_defaults, #update_missing

Methods inherited from ActiveOrient::Base

#[], #[]=, attr_accessible, attr_protected, #attributes, #attributes=, belongs_to, display_rid, exclude_the_following_properties, get_rid, has_many, has_one, #included_links, #initialize, #my_metadata, remove_rid, reset_rid_store, serialize, store_rid, #to_model, #update_attribute

Constructor Details

This class inherits a constructor from ActiveOrient::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ModelRecord

Class Method Details

.create(**keyword_arguments) ⇒ Object

specialized creation of vertices, overloads model#create



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/model/vertex.rb', line 7

def self.create( **keyword_arguments )
new_vert = db.create_vertex self, attributes: keyword_arguments
new_vert =  new_vert.pop if new_vert.is_a?( Array) && new_vert.size == 1
if new_vert.nil?
  logger.error('Vertex'){ "Table #{ref_name} ->>  create failed:  #{keyword_arguments.inspect}" } 
elsif block_given?
  yield new_vert
else
  new_vert # returns the created vertex (or an array of created vertices)
end
end

.delete(where: "") ⇒ Object

Vertex#delete fires a “delete vertex” command to the database. The where statement can be empty ( “” or {}“), then all vertices are removed

The rid-cache is reseted, too



24
25
26
27
# File 'lib/model/vertex.rb', line 24

def self.delete where: ""
  db.execute { "delete vertex #{ref_name} #{db.compose_where(where)}" }
  reset_rid_store
end

Instance Method Details

#detect_edges(kind = :in, edge_name = nil) ⇒ Object

v.to_human

> “<V2: in: E2=>1, node : 4>”

v.detect_edges( :in, 2).to_human

> [“<E2: in : #<V2:0x0000000002e66228>, out : #<V1:0x0000000002ed0060>>”]

v.detect_edges( :in, E1).to_human

> [“<E2: in : #<V2:0x0000000002e66228>, out : #<V1:0x0000000002ed0060>>”]

v.detect_edges( :in, /e/).to_human

> [“<E2: in : #<V2:0x0000000002e66228>, out : #<V1:0x0000000002ed0060>>”]



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/model/vertex.rb', line 49

def detect_edges kind = :in,  edge_name = nil # :nodoc:
  ## returns a list of inherented classes
  get_superclass = ->(e) do
    n = orientdb.get_db_superclass(e)
    n =='E' ? e : e + ',' + get_superclass[n]
  end
if edge_name.nil?
    edges(kind).map &:expand
else
  e_name = if edge_name.is_a?(Regexp)
             edge_name
           else
             Regexp.new  case  edge_name
                         when  Class
                           edge_name.ref_name 
                         when String
                           edge_name
                         when Symbol
                           edge_name.to_s 
                         when Numeric
                           edge_name.to_i.to_s
                         end
           end
   the_edges = [:edges][kind].find_all{|y| get_superclass[y].split(',').detect{|x| x =~ e_name } }
  
  the_edges.map do | the_edge|
  candidate= attributes["#{kind.to_s}_#{the_edge}".to_sym]
  candidate.present?  ? candidate.map( &:expand ).first  : nil 
  end
  end
end

#edges(kind = :all) ⇒ Object

Retrieves connected edges

The basic usage is to fetch all/ incomming/ outgoing edges

Model-Instance.edges :in # :out | :all

One can filter specific edges by providing parts of the edge-name

Model-Instance.edges 'in_sector'
Model-Instance.edges /sector/

The method returns an array of rid’s.

Example:

Industry.first.attributes.keys
 => ["in_sector_classification", "k", "name", "created_at", "updated_at"]  # edge--> in ...

Industry.first.edges :out
  => []

Industry.first.edges :in
=> ["#61:0", "#61:9", "#61:21", "#61:33", "#61:39", "#61:93", "#61:120", "#61:150", "#61:240", "#61:252", "#61:264", "#61:279", "#61:303", "#61:339" ...]

To fetch the associated records use the ActiveOrient::Model.autoload_object method

ActiveOrient::Model.autoload_object Industry.first.edges( :in).first  
# or
Industry.autoload_object Industry.first.edges( /sector/ ).first
 => #<SectorClassification:0x00000002daad20 @metadata={"type"=>"d", "class"=>"sector_classification", "version"=>1, "fieldTypes"=>"out=x,in=x", "cluster"=>61, "record"=>0},(...)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/model/vertex.rb', line 155

def edges kind=:all  # :all, :in, :out 
  expression = case kind
 when :all
   /^in|^out/ 
 when :in
   /^in/ 
 when :out
   /^out/ 
 when String
   /#{kind}/
 when Regexp
   kind
 else
   return  []
 end

  edges = attributes.keys.find_all{ |x| x =~  expression }
  edges.map{|x| attributes[x]}.flatten
end

#in_e(edge_name = nil) ⇒ Object Also known as: in

»in« and »out« provide the main access to edges. »in» is a reserved keyword. Therfore its only an alias to in_e.

If called without a parameter, all connected edges are retrieved.

If called with a string, symbol or class, the edge-class is resolved and even inherented edges are retrieved.



110
111
112
# File 'lib/model/vertex.rb', line 110

def in_e edge_name= nil
  detect_edges :in, edge_name
end

#in_edgesObject

»in_edges« and »out_edges« are shortcuts to »edges :in« and »edges :out«

Its easy to expand the result:

tg.out( :ohlc).out.out_edges
 => [["#102:11032", "#121:0"]] 
 tg.out( :ohlc).out.out_edges.from_orient
 => [[#<TG::GRID_OF:0x00000002620e38

this displays the out-edges correctly

whereas tg.out( :ohlc).out.edges( :out)

=> [["#101:11032", "#102:11032", "#94:10653", "#121:0"]]

returns all edges. The parameter (:out) is not recognized, because out is already a nested array.

this

tg.out( :ohlc).first.out.edges( :out)

is a walkaround, but using in_- and out_edges is more elegant.



196
197
198
# File 'lib/model/vertex.rb', line 196

def in_edges
  edges :in
end

#nodes(in_or_out = :out, via: nil, where: nil, expand: false) ⇒ Object

Lists all connected Vertices

The Edge-classes can be specified via Classname or a regular expression.

If a regular expression is used, the database-names are searched.



86
87
88
89
90
91
92
93
94
95
# File 'lib/model/vertex.rb', line 86

def nodes in_or_out = :out, via:  nil, where: nil, expand:  false
  if via.present?
    edges = detect_edges( in_or_out, via )
    detected_nodes = edges.map do |e|
      q = OrientSupport::OrientQuery.new 
      q.nodes in_or_out, via: e.class, where: where, expand: expand
      query( q )
    end.first
  end
end

#out(edge_name = nil) ⇒ Object



116
117
118
# File 'lib/model/vertex.rb', line 116

def out edge_name =  nil
  detect_edges :out, edge_name
end

#out_edgesObject



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

def out_edges
  edges :out
end

#to_humanObject

Human readable represantation of Vertices

Format: < Classname : Edges, Attributes >



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/model/vertex.rb', line 211

def to_human
  count_and_display_classes = ->(array){array.map(&:class)&.group_by(&:itself)&.transform_values(&:count)} 

  the_ins =    count_and_display_classes[ in_e] 
  the_outs =  count_and_display_classes[ out]

  in_and_out = in_edges.empty? ? "" : "in: #{the_ins}, " 
  in_and_out += out_edges.empty? ? "" : "out: #{the_outs}, " 


  #Default presentation of ActiveOrient::Model-Objects

  "<#{self.class.to_s.demodulize}[#{rid}]: " + in_and_out  + content_attributes.map do |attr, value|
    v= case value
       when ActiveOrient::Model
         "< #{self.class.to_s.demodulize} : #{value.rid} >"
       when OrientSupport::Array
         value.to_s
#          value.rrid #.to_human #.map(&:to_human).join("::")
       else
         value.from_orient
       end
    "%s : %s" % [ attr, v]  unless v.nil?
  end.compact.sort.join(', ') + ">".gsub('"' , ' ')
end