Class: RethinkdbHelper

Inherits:
Object
  • Object
show all
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

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(options={})
  @options = DEFAULTS.merge(options)

  @connection = r.connect(
    host: @options[:host],
    port: @options[:port]
  ).repl

  r.db_drop(@options[:db]) if db_exist? && drop?

  unless  db_exist?
    if create_if_missing?
      r.db_create(@options[:db]).run
    else
      raise "db: '#{@options[:db]}' does not exist"
    end
  end

  @connection.use(@options[:db])

  unless table_exist?
    if create_if_missing?
      r.table_create(@options[:table]).run
    else
      raise "table: '#{@options[:table]}' does not exist"
    end
  end

  @table = r.table(@options[:table])
end

Instance Method Details

#closeObject



102
103
104
# File 'lib/rethinkdb_helper.rb', line 102

def close
  @connection.close
end

#create_if_missing?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rethinkdb_helper.rb', line 67

def create_if_missing?
  @options[:create_if_missing]
end

#db_exist?Boolean

def initialize

Returns:

  • (Boolean)


55
56
57
# File 'lib/rethinkdb_helper.rb', line 55

def db_exist?
  r.db_list.run.include?(@options[:db])
end

#drop?Boolean

Returns:

  • (Boolean)


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

def drop?
  @options[: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

Returns:

  • (Boolean)


59
60
61
# File 'lib/rethinkdb_helper.rb', line 59

def table_exist?
  r.table_list.run.include?($options[:table])
end