Class: Cadet::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/cadet/session.rb

Direct Known Subclasses

BatchInserter::Session

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Session

Returns a new instance of Session.



8
9
10
# File 'lib/cadet/session.rb', line 8

def initialize(db)
  @db = db
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



56
57
58
59
60
# File 'lib/cadet/session.rb', line 56

def method_missing(name, *args)
  if match = /^get_a_([A-Z][A-Za-z]*)_by_([A-z]*)/.match(name)
    return get_node match.captures[0], match.captures[1], args[0]
  end
end

Class Method Details

.open(location) ⇒ Object



12
13
14
# File 'lib/cadet/session.rb', line 12

def self.open(location)
  new GraphDatabaseFactory.new.newEmbeddedDatabase(location)
end

Instance Method Details

#closeObject



15
16
17
# File 'lib/cadet/session.rb', line 15

def close
  @db.shutdown
end

#constraint(label, property) ⇒ Object



49
50
51
52
53
54
# File 'lib/cadet/session.rb', line 49

def constraint(label, property)
  @db.schema
    .constraintFor(DynamicLabel.label(label))
    .assertPropertyIsUnique(property)
    .create
end

#create_node(label, property, value) ⇒ Object



19
20
21
22
23
24
# File 'lib/cadet/session.rb', line 19

def create_node(label, property, value)
  n = Node.new @db.createNode
  n.add_label label
  n[property] = value
  n
end

#find_node(label, property, value) ⇒ Object



26
27
28
29
# File 'lib/cadet/session.rb', line 26

def find_node(label, property, value)
  node = IteratorUtil.firstOrNull @db.findNodesByLabelAndProperty(DynamicLabel.label(label), property, value)
  node ? Node.new(node) : null
end

#get_node(label, property, value) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cadet/session.rb', line 31

def get_node(label, property, value)
  n = find_node(label, property, value)
  if n.nil?
    n = create_node(label, property, value)
  end
  n
end

#transactionObject



39
40
41
42
43
44
45
46
47
# File 'lib/cadet/session.rb', line 39

def transaction
  tx = @db.beginTx
  begin
    yield tx
    tx.success
  ensure
    tx.close
  end
end