Class: RethinkdbHelper
- Inherits:
-
Object
- Object
- RethinkdbHelper
- Includes:
- RethinkDB::Shortcuts
- Defined in:
- lib/rethinkdb_helper.rb
Constant Summary collapse
- DEFAULTS =
{ host: ENV['RETHINKDB_HOST'] || 'localhost', port: (ENV['RETHINKDB_PORT'] || '28015').to_i, db: ENV['RETHINKDB_DB'] || 'test', table: ENV['RETHINKDB_TABLE'] || 'test', drop: false, create_if_missing: false }
Instance Method Summary collapse
- #close ⇒ Object
- #create_if_missing? ⇒ Boolean
-
#db_exist? ⇒ Boolean
def initialize.
- #drop? ⇒ Boolean
-
#filter(params = {}) ⇒ Object
(also: #search)
TODO: Currently limited to one search field and regex consider how to use more than one field.
-
#initialize(options = {}) ⇒ RethinkdbHelper
constructor
TODO: Limited to one table per instance, consider support for multiple table per db support; consider multiple db per instance support.
-
#insert(*payloads) ⇒ Object
(also: #add, #load)
payloads is an array of hashes or a single hash document.
- #table_exist? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ RethinkdbHelper
TODO: Limited to one table per instance, consider
support for multiple table per db support;
consider multiple db per instance support.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rethinkdb_helper.rb', line 24 def initialize(={}) = DEFAULTS.merge() @connection = r.connect( host: [:host], port: [:port] ).repl r.db_drop([:db]) if db_exist? && drop? unless db_exist? if create_if_missing? r.db_create([:db]).run else raise "db: '#{@options[:db]}' does not exist" end end @connection.use([:db]) unless table_exist? if create_if_missing? r.table_create([:table]).run else raise "table: '#{@options[:table]}' does not exist" end end @table = r.table([:table]) end |
Instance Method Details
#close ⇒ Object
102 103 104 |
# File 'lib/rethinkdb_helper.rb', line 102 def close @connection.close end |
#create_if_missing? ⇒ Boolean
67 68 69 |
# File 'lib/rethinkdb_helper.rb', line 67 def create_if_missing? [:create_if_missing] end |
#db_exist? ⇒ Boolean
def initialize
55 56 57 |
# File 'lib/rethinkdb_helper.rb', line 55 def db_exist? r.db_list.run.include?([:db]) end |
#drop? ⇒ Boolean
63 64 65 |
# File 'lib/rethinkdb_helper.rb', line 63 def drop? [:drop] end |
#filter(params = {}) ⇒ Object Also known as: search
TODO: Currently limited to one search field and regex
consider how to use more than one field
returns an enumerable cursor into the database for documents that match the search terms.
params is a hash where the key is the symbolized field_name and its value is the regex by which to filter
91 92 93 94 95 96 97 98 99 |
# File 'lib/rethinkdb_helper.rb', line 91 def filter(params={}) raise 'No search terms' if params.empty? field_name = params.keys.first search_regex = params[field_name] @table.filter{|document| document[field_name]. match(search_regex)}. run end |
#insert(*payloads) ⇒ Object Also known as: add, load
payloads is an array of hashes or a single hash document.
73 74 75 76 77 78 79 |
# File 'lib/rethinkdb_helper.rb', line 73 def insert(*payloads) raise 'No document provided' if payloads.empty? invalid_payloads = false payloads.map{|doc| invalid_payloads &&= !doc.is_a?(Hash)} raise 'Invalid document: must be Hash' if invalid_payloads @table.insert(payloads).run end |
#table_exist? ⇒ Boolean
59 60 61 |
# File 'lib/rethinkdb_helper.rb', line 59 def table_exist? r.table_list.run.include?([:table]) end |