Class: Neo4Apis::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4apis/base.rb

Constant Summary collapse

UUID_FIELDS =
{}
NODE_PROXIES =
{}
IMPORTERS =
{}
DEFAULT_BATCH_SIZE =
500

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(neo4j_session, options = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
19
20
# File 'lib/neo4apis/base.rb', line 12

def initialize(neo4j_session, options = {})
  @buffer = QueryBuffer.new(neo4j_session, options[:batch_size] || self.class.batch_size || DEFAULT_BATCH_SIZE)
  @options = options

  UUID_FIELDS.each do |label, uuid_field|
    @buffer << create_constraint_query(label, uuid_field)
  end
  @buffer.flush
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/neo4apis/base.rb', line 10

def options
  @options
end

Class Method Details

.batch_size(batch_size = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/neo4apis/base.rb', line 108

def self.batch_size(batch_size = nil)
  if batch_size.is_a?(Integer)
    @batch_size = batch_size
  elsif batch_size.nil?
    @batch_size
  else
    fail ArgumentError, "Invalid value for batch_size: #{batch_size.inspect}"
  end
end

.common_label(common_label = nil) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/neo4apis/base.rb', line 65

def self.common_label(common_label = nil)
  if common_label.nil?
    @common_label
  else
    @common_label = common_label
  end
end

.importer(label, &block) ⇒ Object



73
74
75
# File 'lib/neo4apis/base.rb', line 73

def self.importer(label, &block)
  IMPORTERS[label.to_sym] = block
end

.node_proxy(label) ⇒ Object



81
82
83
84
85
# File 'lib/neo4apis/base.rb', line 81

def self.node_proxy(label)
  uuid_field = UUID_FIELDS[label.to_sym]

  NODE_PROXIES[label.to_sym] ||= node_proxy_from_uuid(label, uuid_field)
end

.node_proxy_from_uuid(label, uuid_field) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/neo4apis/base.rb', line 87

def self.node_proxy_from_uuid(label, uuid_field)
  Struct.new(:props) do
    const_set(:UUID_FIELD, uuid_field.to_sym)
    const_set(:LABEL, label.to_sym)

    def uuid_field
      self.class::UUID_FIELD
    end

    def label
      self.class::LABEL
    end

    def uuid_value
      fail ArgumentError, "props does not have UUID field `#{uuid_field}` for #{self.inspect}" if not props.has_key?(uuid_field)

      props[uuid_field]
    end
  end
end

.uuid(label, uuid_field) ⇒ Object



77
78
79
# File 'lib/neo4apis/base.rb', line 77

def self.uuid(label, uuid_field)
  UUID_FIELDS[label.to_sym] = uuid_field.to_sym
end

Instance Method Details

#add_node(label, object = nil, columns = []) {|props| ... } ⇒ Object

Yields:

  • (props)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/neo4apis/base.rb', line 22

def add_node(label, object = nil, columns = [])
  props = OpenStruct.new

  if object
    columns.each do |column|
      props[column] = object.send(column)
    end
  end

  yield props if block_given?

  require_batch

  fail ArgumentError, "No UUID specified for label `#{label}`" if not UUID_FIELDS[label.to_sym]

  self.class.node_proxy(label).new(props.marshal_dump).tap do |node_proxy|
    @buffer << create_node_query(node_proxy)
  end
end

#add_relationship(type, source, target, props = {}) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/neo4apis/base.rb', line 42

def add_relationship(type, source, target, props = {})
  fail ArgumentError, "No source specified" if not source
  fail ArgumentError, "No target specified" if not target

  require_batch

  @buffer << create_relationship_query(type, source, target, props)
end

#batchObject



51
52
53
54
55
56
57
58
59
# File 'lib/neo4apis/base.rb', line 51

def batch
  @in_batch = true

  yield

  @buffer.close
ensure
  @in_batch = false
end

#import(label, *args) ⇒ Object



61
62
63
# File 'lib/neo4apis/base.rb', line 61

def import(label, *args)
  self.instance_exec(*args, &IMPORTERS[label.to_sym])
end