module CassandraObject class Scope module FinderMethods def find(ids) if ids.is_a?(Array) find_some(ids) else find_one(ids) end end def find_by_id(ids) find(ids) rescue CassandraObject::RecordNotFound nil end def find_all_in_batches(next_cursor = nil) obj = self.clone obj.is_all = true obj.next_cursor = next_cursor obj.to_a end def first return limit(1).find_all_in_batches[:results].first if self.schema_type == :dynamic_attributes || self.schema_type == :schemaless limit(1).to_a.first end private def find_one(id) if id.blank? raise CassandraObject::RecordNotFound, "Couldn't find #{self.name} with key #{id.inspect}" elsif self.schema_type == :dynamic_attributes record = where_ids(id).to_a raise CassandraObject::RecordNotFound if record.empty? record elsif record = where_ids(id)[0] record else raise CassandraObject::RecordNotFound end end def find_some(ids) ids = ids.flatten return [] if ids.empty? ids = ids.compact.map(&:to_s).uniq where_ids(ids).to_a end end end end