Class: Neography::Composable::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/neography-batch/batch.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unitObject



40
41
42
# File 'lib/neography-batch/batch.rb', line 40

def self.unit
  Batch.new
end

Instance Method Details

#==(other) ⇒ Object



85
86
87
# File 'lib/neography-batch/batch.rb', line 85

def ==(other)
  eql?(other)
end

#add(command_or_batch) ⇒ Object Also known as: <<



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/neography-batch/batch.rb', line 44

def add(command_or_batch)
  if command_or_batch.kind_of?(Batch)
    bind(command_or_batch)
  else
    unless command_or_batch.respond_to?(:each)
      raise StandardError, "command_or_batch must respond to :each"
    end

    reference = BatchReference.new(command_or_batch)
    @commands << {:cmd => command_or_batch, :reference => reference}
    reference
  end
end

#bind(batch) ⇒ Object



81
82
83
# File 'lib/neography-batch/batch.rb', line 81

def bind(batch)
  Batch.new(@db, @commands.concat(batch.commands))
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
# File 'lib/neography-batch/batch.rb', line 89

def eql?(other)
  if other.class != self.class
    false
  else
    commands.length == other.commands.length &&
        commands.zip(other.commands).all? { |z| z[0] == z[1] }
  end
end

#find_referenceObject



58
59
60
# File 'lib/neography-batch/batch.rb', line 58

def find_reference
  @commands.select { |c| !block_given? || yield(c[:cmd]) }.map { |c| c[:reference] }
end

#submitObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/neography-batch/batch.rb', line 64

def submit
  return [] if @commands.empty?
  command_list = Batch.get_resolved_commands(@commands)
  result = @db.batch(*command_list)
  if result.nil?
    raise StandardError, "batch returned no result (nil)"
  end
  batch_errors = result.select { |r| r["status"] >= 400 }
  if batch_errors.count > 0
    raise StandardError, "batch returned one or more errors: #{batch_errors.map { |e| e["message"] }.join("|")}"
  end
  results = result.map { |r| r["body"] }
  @commands.each_with_index { |c, i| c[:reference].notify_after_submit(results[i]) }
  @commands.clear()
  results
end