Skip to content

Initialisation

Hartmut Bischoff edited this page Apr 8, 2021 · 19 revisions

ActivdeOrient requires a running OrientDB-Server-instance

ActiveOrient::Init.connect database: <temp>,
				user: <root>,
				password: <root>,
				server: <localhost>,
				port: <2480>,
				logger: <nil>

connects to the server, opens the database and gathers information about server and database.

Base-Classes for Edges and Vertexes, E and V, are allocated.

Preallocate Database Classes

The directive ActiveOrient::Model.keep_models_without_file controls the preallocation process.

  • If set to false, only database-classes with corresponding model-files are preallocated and assigned to their ActiveOrient::Modelclass.
  • If set to true, any database class present is allocated to a ActiveOrient::Modelclass.

Model-Dir

ActiveOrient::Model-Methods are stored in (root)/lib/modelby default.

The location is set by

ActiveOrient::Model.model_dir = project_root + '/model'

Multiple resources may me specified, ie.

ActiveOrient::Model.model_dir = [ project_root + "lib/model", /some/other/location/ ]

A typical setup using the ActiveOrient Gem:

#(after ActiveOrient::Init.connect)
project_root = File.expand_path('../..', __FILE__)
ActiveOrient::Model.model_dir =  project_root + '/model'
ActiveOrient::OrientDB.new  preallocate: true 

Namespacing

ActiveOrient::Init.connect 

project_root = File.expand_path('../..', __FILE__)
ActiveOrient::Model.model_dir =  project_root + '/model'
ActiveOrient::Model.keep_models_without_file = false

# allocate classes from /model directory
ActiveOrient::OrientDB.new  preallocate: true  

module HC; end
ActiveOrient::Init.define_namespace { HC }

# allocate classes from /model/hc directory
ActiveOrient::OrientDB.new  preallocate: true  

# allocate any other classes and use methods defined in /model/tg and /lib/tangerine/tg
module TG; end
ActiveOrient::Init.define_namespace { TG } 
ActiveOrient::Model.keep_models_without_file = true
ActiveOrient::OrientDB.new  preallocate: true  model_file: project_root +'/lib/tangerine'

Each ActiveOrient::Model contains a reference to the ActiveOrient::OrientDB-Instance. Further references are not required. Thus V.db references the database instance.

Example Model Files

Due to limitations of the REST-protocoll, a query select from hc_basiswert where name like 'A%' is not transmitted properly. Instead, the function left has to be used. The following code enables the call of HC::Basiswert.like {some string}

(/model/v.rb)


class V

class << self    #  class methods
  def like  p, order: 'asc'
    p.chop! if ["%","*"].include?(p[-1])
    p[0] = p[0].upcase
    query.where( "name.left(#{p.length})" => p )
 	 .order: { :name => order }
         .execute
  end
end
end  # class

Another example, borrowed from the TimeGraph Gem

Jahr-Vertexes are connected to Monat-Vertices through MONTH_OF-Edges. Anything is performed in the TG-namespace. To access the connected Monat-Vertex, the following helper was written. Its chainable.

( /model/tg/jahr.rb )
class TG::Jahr 
  def der_monat m
    m >0 && m<13 ? out_tg_month_of[m].in : nil
  end
end