Class: AnswerFactory::Batch
- Inherits:
-
Array
- Object
- Array
- AnswerFactory::Batch
- 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
- #<<(obj) ⇒ Object
- #[]=(index, obj) ⇒ Object
- #bulk_save!(couchdb_uri) ⇒ Object
- #data ⇒ Object
-
#initialize(*args) ⇒ Batch
constructor
A new instance of Batch.
Constructor Details
Class Method Details
.[](*args) ⇒ Object
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
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.}'" # raise e end return batch end |
Instance Method Details
#<<(obj) ⇒ Object
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
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
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 |
#data ⇒ Object
63 64 65 |
# File 'lib/answers/batch.rb', line 63 def data self.collect {|i| i.data} end |