Class: WordTree::DB::Librarian
- Inherits:
-
Object
- Object
- WordTree::DB::Librarian
- Defined in:
- lib/wordtree/db/librarian.rb
Instance Method Summary collapse
- #find(book_id) ⇒ Object
-
#initialize(connection) ⇒ Librarian
constructor
If a hash, it uses keys :host, :port, and :db.
- #save(book) ⇒ Object
- #search(params, page = 1, per_page = 20) ⇒ Object
Constructor Details
#initialize(connection) ⇒ Librarian
If a hash, it uses keys :host, :port, and :db
9 10 11 12 13 14 15 16 |
# File 'lib/wordtree/db/librarian.rb', line 9 def initialize(connection) @r = RethinkDB::RQL.new if connection.is_a? Hash @rdb = @r.connect(connection) else @rdb = connection end end |
Instance Method Details
#find(book_id) ⇒ Object
18 19 20 21 |
# File 'lib/wordtree/db/librarian.rb', line 18 def find(book_id) result = @r.table('books').get(book_id).run(@rdb) result ? Book.new(result) : nil end |
#save(book) ⇒ Object
23 24 25 26 |
# File 'lib/wordtree/db/librarian.rb', line 23 def save(book) result = @r.table('books').insert(book., :conflict => :update).run(@rdb) return result["replaced"] == 1 || result["inserted"] == 1 || result["unchanged"] == 1 end |
#search(params, page = 1, per_page = 20) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/wordtree/db/librarian.rb', line 28 def search(params, page=1, per_page=20) conditions = match_list(params, [:id, :title, :author, :source, :status], [:year, :size_bytes]) cursor = @r.table('books'). order_by(:index => 'year'). filter(&conditions). skip((page-1)*per_page). limit(per_page). run(@rdb) if !(results = cursor.to_a).empty? results.map{ |result| Book.new(result) } else nil end end |