Sha256: 4d25eea12675964a5940f2615972a486b216e966f278855e3f402fd3228d9746

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Cadet
  class Session
    include_package "org.neo4j.graphdb"
    include_package "org.neo4j.graphdb.factory"
    include_package "org.neo4j.unsafe.batchinsert"
    include_package "org.neo4j.helpers.collection"

    def initialize(db)
      @db = db
    end

    def self.open(location)
      new GraphDatabaseFactory.new.newEmbeddedDatabase(location)
    end
    def close
      @db.shutdown
    end

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

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

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

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

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

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cadet-0.0.4-java lib/cadet/session.rb