Class: AnswerFactory::Batch

Inherits:
Array
  • Object
show all
Defined in:
lib/answers/batch.rb

Overview

A Batch is simply an Array of Answers, with validation for all assignment methods

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Batch

Returns a new instance of Batch.

Raises:

  • (ArgumentError)


24
25
26
27
28
# File 'lib/answers/batch.rb', line 24

def initialize(*args)
  raise ArgumentError, "Batches can only contain Answers" unless
    args.inject(true) {|anded, a| anded & a.kind_of?(Answer)}
  super
end

Class Method Details

.[](*args) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
# File 'lib/answers/batch.rb', line 5

def self.[](*args)
  raise ArgumentError unless args.inject(true) {|anded, a| anded & a.kind_of?(Answer)}
  super
end

.load_from_couch(couchdb_uri, design_doc) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/answers/batch.rb', line 43

def self.load_from_couch(couchdb_uri, design_doc)
  raise ArgumentError, "#{couchdb_uri} is not a String" unless couchdb_uri.kind_of?(String)
  raise ArgumentError, "#{design_doc} is not a String" unless design_doc.kind_of?(String)
  
  batch = Batch.new
  
  db = CouchRest.database(couchdb_uri) # add the view document and key here
  begin
    response = db.view(design_doc)
    response["rows"].each do |hash|
      batch << Answer.from_serial_hash(hash)
    end
  rescue JSON::ParserError => e
    puts "Batch not read due to JSON ParserError: '#{e.message}'"
    # raise e
  end
  return batch
end

Instance Method Details

#<<(obj) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/answers/batch.rb', line 17

def <<(obj)
  raise ArgumentError, "Batch.<< cannot append class #{obj.class}" unless
    obj.kind_of?(Answer)
  super
end

#[]=(index, obj) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
# File 'lib/answers/batch.rb', line 11

def []=(index, obj)
  raise ArgumentError unless obj.kind_of?(Answer)
  super
end

#bulk_save!(couchdb_uri) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
# File 'lib/answers/batch.rb', line 31

def bulk_save!(couchdb_uri)
  raise ArgumentError, "#{couchdb_uri} is not a String" unless couchdb_uri.kind_of?(String)
  
  db = CouchRest.database!(couchdb_uri)
  response = db.bulk_save(self.data)
  response.each_index do |idx|
    self[idx].couch_id = response[idx]["id"]
    self[idx].couch_rev = response[idx]["rev"]
  end
end

#dataObject



63
64
65
# File 'lib/answers/batch.rb', line 63

def data
  self.collect {|i| i.data}
end