Class: Cadet::Session
- Inherits:
-
Object
show all
- Defined in:
- lib/cadet/session.rb
Constant Summary
collapse
- @@current_session =
nil
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(underlying) ⇒ Session
Returns a new instance of Session.
9
10
11
|
# File 'lib/cadet/session.rb', line 9
def initialize(underlying)
@underlying = underlying
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/cadet/session.rb', line 71
def method_missing(name, *args, &block)
case name
when /^([A-z_]*)_by_([A-z_]*)$/
self.class.class_eval "
def #{name}(value)
get_node :#{$1}, :#{$2}, value
end"
return self.send(name, *args, &block)
when /^create_([A-z_]*)$/
self.class.class_eval "
def #{name}(value)
create_node :#{$1}, value
end"
return self.send(name, *args, &block)
else
raise NotImplementedError
end
end
|
Class Method Details
.current_session ⇒ Object
5
6
7
|
# File 'lib/cadet/session.rb', line 5
def self.current_session
@@current_session
end
|
.open(location = nil, &block) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/cadet/session.rb', line 13
def self.open(location = nil, &block)
(location ?
new(org.neo4j.graphdb.factory.GraphDatabaseFactory.new.newEmbeddedDatabase(location)) :
new(org.neo4j.test.TestGraphDatabaseFactory.new.newImpermanentDatabase))
.tap do |session|
@@current_session = session
if block_given?
session.instance_exec(session, &block)
session.close
end
end
end
|
Instance Method Details
#begin_tx ⇒ Object
67
68
69
|
# File 'lib/cadet/session.rb', line 67
def begin_tx
@underlying.beginTx
end
|
#close ⇒ Object
26
27
28
|
# File 'lib/cadet/session.rb', line 26
def close
@underlying.shutdown
end
|
#constraint(label, property) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/cadet/session.rb', line 60
def constraint(label, property)
@underlying.schema
.constraintFor(DynamicLabel.label(label))
.assertPropertyIsUnique(property)
.create()
end
|
#create_node(label, properties, indexing_property = nil) ⇒ Object
30
31
32
33
34
35
|
# File 'lib/cadet/session.rb', line 30
def create_node(label, properties, indexing_property = nil)
Node.new(@underlying.createNode).tap do |n|
n.add_label label
properties.each { |prop, val| n[prop] = val }
end
end
|
#find_node(label, property, value) ⇒ Object
37
38
39
40
|
# File 'lib/cadet/session.rb', line 37
def find_node(label, property, value)
( node = org.neo4j.helpers.collection.IteratorUtil.firstOrNull(@underlying.findNodesByLabelAndProperty(DynamicLabel.label(label), property, value)) ) ?
Node.new(node) : nil
end
|
#get_node(label, property, value) ⇒ Object
42
43
44
|
# File 'lib/cadet/session.rb', line 42
def get_node(label, property, value)
find_node(label, property, value) || create_node(label, {property.to_sym => value})
end
|
#get_transaction ⇒ Object
46
47
48
|
# File 'lib/cadet/session.rb', line 46
def get_transaction
Transaction.new(self)
end
|
#transaction ⇒ Object
50
51
52
53
54
55
56
57
58
|
# File 'lib/cadet/session.rb', line 50
def transaction
tx = get_transaction
begin
yield tx
tx.success
ensure
tx.close
end
end
|