Class: Neo4j::Session
- Inherits:
-
Object
show all
- Defined in:
- lib/neo4j/session.rb
Defined Under Namespace
Classes: CypherError
Constant Summary
collapse
- @@current_session =
nil
- @@all_sessions =
{}
- @@factories =
{}
Class Method Summary
collapse
-
._listeners ⇒ Object
-
._notify_listeners(event, data) ⇒ Object
-
.add_listener(&listener) ⇒ Object
-
.create_session(db_type, params = {}) ⇒ Object
-
.current ⇒ Neo4j::Session
-
.current! ⇒ Object
Returns the current session or raise an exception if no session is available.
-
.inspect ⇒ Object
-
.named(name) ⇒ Object
Returns a session with given name or else raise an exception.
-
.on_session_available(&callback) ⇒ Object
Registers a callback which will be called immediately if session is already available, or called when it later becomes available.
-
.open(db_type = :server_db, *params) ⇒ Object
Creates a new session to Neo4j.
-
.open_named(db_type, name, default = nil, *params) ⇒ Object
-
.query(*args) ⇒ Object
-
.register(session, name = nil, default = nil) ⇒ Object
-
.register_db(db, &session_factory) ⇒ Object
-
.set_current(session) ⇒ Object
Sets the session to be used as default.
-
.unregister(session) ⇒ Object
-
.user_agent_string ⇒ Object
Instance Method Summary
collapse
Class Method Details
._listeners ⇒ Object
168
169
170
171
|
# File 'lib/neo4j/session.rb', line 168
def _listeners
@@listeners ||= []
@@listeners
end
|
._notify_listeners(event, data) ⇒ Object
174
175
176
|
# File 'lib/neo4j/session.rb', line 174
def _notify_listeners(event, data)
_listeners.each { |li| li.call(event, data) }
end
|
.add_listener(&listener) ⇒ Object
163
164
165
|
# File 'lib/neo4j/session.rb', line 163
def add_listener(&listener)
_listeners << listener
end
|
.create_session(db_type, params = {}) ⇒ Object
107
108
109
110
111
112
|
# File 'lib/neo4j/session.rb', line 107
def create_session(db_type, params = {})
unless @@factories[db_type]
fail "Can't connect to database '#{db_type}', available #{@@factories.keys.join(',')}"
end
@@factories[db_type].call(*params)
end
|
Returns the current session.
115
116
117
|
# File 'lib/neo4j/session.rb', line 115
def current
@@current_session
end
|
.current! ⇒ Object
Returns the current session or raise an exception if no session is available
120
121
122
123
|
# File 'lib/neo4j/session.rb', line 120
def current!
fail 'No session, please create a session first with Neo4j::Session.open(:server_db) or :embedded_db' unless current
current
end
|
.inspect ⇒ Object
194
195
196
|
# File 'lib/neo4j/session.rb', line 194
def inspect
"Neo4j::Session available: #{@@factories && @@factories.keys}"
end
|
.named(name) ⇒ Object
Returns a session with given name or else raise an exception
131
132
133
|
# File 'lib/neo4j/session.rb', line 131
def named(name)
@@all_sessions[name] || fail("No session named #{name}.")
end
|
.on_session_available(&callback) ⇒ Object
Registers a callback which will be called immediately if session is already available, or called when it later becomes available.
143
144
145
146
147
148
149
|
# File 'lib/neo4j/session.rb', line 143
def on_session_available(&callback)
callback.call(Neo4j::Session.current) if Neo4j::Session.current
add_listener do |event, data|
callback.call(data) if event == :session_available
end
end
|
.open(db_type = :server_db, *params) ⇒ Object
Creates a new session to Neo4j. This will be the default session to be used unless there is already a session created (see #current and #set_current)
97
98
99
|
# File 'lib/neo4j/session.rb', line 97
def open(db_type = :server_db, *params)
register(create_session(db_type, params))
end
|
.open_named(db_type, name, default = nil, *params) ⇒ Object
101
102
103
104
|
# File 'lib/neo4j/session.rb', line 101
def open_named(db_type, name, default = nil, *params)
fail 'Multiple sessions is currently only supported for Neo4j Server connections.' unless db_type == :server_db
register(create_session(db_type, params), name, default)
end
|
.query(*args) ⇒ Object
126
127
128
|
# File 'lib/neo4j/session.rb', line 126
def query(*args)
current!.query(*args)
end
|
.register(session, name = nil, default = nil) ⇒ Object
179
180
181
182
183
184
185
186
187
|
# File 'lib/neo4j/session.rb', line 179
def register(session, name = nil, default = nil)
if default == true
set_current(session)
elsif default.nil?
set_current(session) unless @@current_session
end
@@all_sessions[name] = session if name
session
end
|
.register_db(db, &session_factory) ⇒ Object
199
200
201
202
|
# File 'lib/neo4j/session.rb', line 199
def register_db(db, &session_factory)
puts "replace factory for #{db}" if @@factories[db]
@@factories[db] = session_factory
end
|
.set_current(session) ⇒ Object
Sets the session to be used as default
137
138
139
|
# File 'lib/neo4j/session.rb', line 137
def set_current(session)
@@current_session = session
end
|
.unregister(session) ⇒ Object
190
191
192
|
# File 'lib/neo4j/session.rb', line 190
def unregister(session)
@@current_session = nil if @@current_session == session
end
|
.user_agent_string ⇒ Object
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/neo4j/session.rb', line 151
def user_agent_string
gem, version = if defined?(::Neo4j::ActiveNode)
['neo4j', ::Neo4j::VERSION]
else
['neo4j-core', ::Neo4j::Core::VERSION]
end
"#{gem}-gem/#{version} (https://github.com/neo4jrb/#{gem})"
end
|
Instance Method Details
#_query(*params) ⇒ Object
Same as #query but does not accept an DSL and returns the raw result from the database. Notice, it might return different values depending on which database is used, embedded or server.
75
76
77
|
# File 'lib/neo4j/session.rb', line 75
def _query(*params)
fail 'not implemented'
end
|
#auto_commit? ⇒ Boolean
35
36
37
|
# File 'lib/neo4j/session.rb', line 35
def auto_commit?
true end
|
#begin_tx ⇒ Object
40
41
42
|
# File 'lib/neo4j/session.rb', line 40
def begin_tx
fail 'not impl.'
end
|
#close ⇒ Object
8
9
10
|
# File 'lib/neo4j/session.rb', line 8
def close
self.class.unregister(self)
end
|
#db_type ⇒ :embedded_db | :server_db
31
32
33
|
# File 'lib/neo4j/session.rb', line 31
def db_type
fail 'not impl.'
end
|
Performs a cypher query. See Core::Query for more details, but basic usage looks like:
68
69
70
|
# File 'lib/neo4j/session.rb', line 68
def query(options = {})
fail 'not implemented, abstract'
end
|
#running ⇒ Object
Only for embedded database
26
27
28
|
# File 'lib/neo4j/session.rb', line 26
def running
fail 'not impl.'
end
|
#shutdown ⇒ Object
Only for embedded database
20
21
22
|
# File 'lib/neo4j/session.rb', line 20
def shutdown
fail 'not impl.'
end
|
#start ⇒ Object
Only for embedded database
14
15
16
|
# File 'lib/neo4j/session.rb', line 14
def start
fail 'not impl.'
end
|