Class: RethinkdbHelper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
RethinkDB::Shortcuts
Defined in:
lib/rethinkdb_helper.rb

Constant Summary collapse

DEFAULTS =
{
  host:               ENV['RDB_HOST']     || 'localhost',
  port:              (ENV['RDB_PORT']     || '28015').to_i, # SMELL: is to_i necessary?
  db:                 ENV['RDB_DB']       || 'test',
  table:              ENV['RDB_TABLE']    || 'test',
  auth_key:           ENV['RDB_AUTH_KEY'] || 'unknown',
  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.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rethinkdb_helper.rb', line 41

def initialize(options={})
  @options = DEFAULTS.merge(options)

  @connection = connect

  db_drop if db_exist? && drop?

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

  use(@options[:db])

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

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

Instance Method Details

#changes(options = {}) ⇒ Object



152
153
154
# File 'lib/rethinkdb_helper.rb', line 152

def changes(options={})
  @table.changes(options).run(connect)
end

#connect(options = {host: @options[:host], port: @options[:port]}) ⇒ Object



104
105
106
# File 'lib/rethinkdb_helper.rb', line 104

def connect(options={host: @options[:host], port: @options[:port]})
  r.connect(options).repl
end

#create_if_missing?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/rethinkdb_helper.rb', line 211

def create_if_missing?
  @options[:create_if_missing]
end

#create_simple_index(field_name) ⇒ Object



186
187
188
# File 'lib/rethinkdb_helper.rb', line 186

def create_simple_index(field_name)
  @table.index_create(field_name.to_s).run
end

#db(db_name = @options[:db]) ⇒ Object

def use(db_name=@options)

@connection.use(db_name)

end



121
122
123
# File 'lib/rethinkdb_helper.rb', line 121

def db(db_name=@options[:db])
  @db = r.db(db_name)
end

#db_configObject



91
92
93
# File 'lib/rethinkdb_helper.rb', line 91

def db_config
  @db.config.run
end

#db_create(db_name = @options[:db]) ⇒ Object Also known as: create_db



160
161
162
# File 'lib/rethinkdb_helper.rb', line 160

def db_create(db_name=@options[:db])
  @db = r.db_create(db_name).run
end

#db_drop(db_name = @options[:db]) ⇒ Object Also known as: drop_db, db_delete, delete_db



108
109
110
111
112
# File 'lib/rethinkdb_helper.rb', line 108

def db_drop(db_name=@options[:db])
  @db    = nil
  @table = nil
  r.db_drop(db_name).run
end

#db_exist?(db_name = @options[:db]) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/rethinkdb_helper.rb', line 156

def db_exist?(db_name=@options[:db])
  db_list.include?(db_name)
end

#db_listObject Also known as: list_db



165
166
167
# File 'lib/rethinkdb_helper.rb', line 165

def db_list
  r.db_list.run
end

#db_wait(*options) ⇒ Object



96
97
98
# File 'lib/rethinkdb_helper.rb', line 96

def db_wait(*options)
  @db.wait(options).run
end

#drop?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/rethinkdb_helper.rb', line 207

def drop?
  @options[:drop]
end

#get_all_keys(keys, options = {}) ⇒ Object



219
220
221
# File 'lib/rethinkdb_helper.rb', line 219

def get_all_keys(keys, options={})
  @table.get_all([keys].flatten, options).run
end

#get_between_keys(lower_key, upper_key, options = {}) ⇒ Object Also known as: between_keys



223
224
225
# File 'lib/rethinkdb_helper.rb', line 223

def get_between_keys(lower_key, upper_key, options={})
  @table.between(lower_key, upper_key, options).run
end

#get_key(key) ⇒ Object



215
216
217
# File 'lib/rethinkdb_helper.rb', line 215

def get_key(key)
  @table.get(key).run
end

#get_table(table_name = @options[:table], options) ⇒ Object



129
130
131
# File 'lib/rethinkdb_helper.rb', line 129

def get_table(table_name=@options[:table], options)
  r.table(table_name, options).run
end

#index_drop(index_name) ⇒ Object Also known as: drop_inde, delete_index, index_delete



195
196
197
# File 'lib/rethinkdb_helper.rb', line 195

def index_drop(index_name)
  @table.index_drop(index_name).run
end

#index_listObject Also known as: list_indexes



190
191
192
# File 'lib/rethinkdb_helper.rb', line 190

def index_list
  @table.index_list.run
end

#index_wait(index_name) ⇒ Object Also known as: wait_on_index



179
180
181
# File 'lib/rethinkdb_helper.rb', line 179

def index_wait(*indexes)
  @table.index_wait(indexes).run
end

#insert(payloads, options = {}) ⇒ Object Also known as: add, load

payloads is an array of hashes or a single hash document.



236
237
238
239
240
241
242
243
# File 'lib/rethinkdb_helper.rb', line 236

def insert(payloads, options={})
  payloads = [payloads].flatten
  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.flatten, options).run
end

#join(foreign_key, table_name, options = {}) ⇒ Object



229
230
231
232
# File 'lib/rethinkdb_helper.rb', line 229

def join(foreign_key, table_name,options={})
  @table.eq_join(foreign_key,
    r.table(table_name), options).without({:right => "id"}).zip().run
end

#rebalanceObject Also known as: table_rebalance



82
83
84
# File 'lib/rethinkdb_helper.rb', line 82

def rebalance
  @table.rebalance.run
end

#reconfigure(options = {}) ⇒ Object Also known as: table_reconfigure



77
78
79
# File 'lib/rethinkdb_helper.rb', line 77

def reconfigure(options={})
  @taboe.reconfigure(options).run
end

#search(params = {}) ⇒ Object

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



255
256
257
258
259
260
261
262
263
# File 'lib/rethinkdb_helper.rb', line 255

def search(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

#server_wait(*options) ⇒ Object



100
101
102
# File 'lib/rethinkdb_helper.rb', line 100

def server_wait(*options)
  r.wait(options).run
end

#syncObject Also known as: flush



133
134
135
# File 'lib/rethinkdb_helper.rb', line 133

def sync
  @table.sync.run
end

#table(table_name = @options[:table], options = {}) ⇒ Object



125
126
127
# File 'lib/rethinkdb_helper.rb', line 125

def table(table_name=@options[:table],options={})
  @table = r.table(table_name, options)
end

#table_configObject



87
88
89
# File 'lib/rethinkdb_helper.rb', line 87

def table_config
  @table.config.run
end

#table_create(table_name = @options[:table], options = {}) ⇒ Object Also known as: create_table



139
140
141
# File 'lib/rethinkdb_helper.rb', line 139

def table_create(table_name=@options[:table], options={})
  @table = r.table_create(table_name, options).run
end

#table_drop(table_name = @options[:table]) ⇒ Object Also known as: drop_table, elete_table, table_delete



144
145
146
147
# File 'lib/rethinkdb_helper.rb', line 144

def table_drop(table_name=@options[:table])
  @table = nil
  @db.table_drop(table_name)
end

#table_exist?(table_name = @options[:table]) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/rethinkdb_helper.rb', line 175

def table_exist?(table_name=@options[:table])
  table_list.include?(table_name)
end

#table_listObject Also known as: list_table



170
171
172
# File 'lib/rethinkdb_helper.rb', line 170

def table_list
  r.table_list.run
end

#table_status(table_name = @options[:table]) ⇒ Object



73
74
75
# File 'lib/rethinkdb_helper.rb', line 73

def table_status(table_name=@options[:table])
  r.table_status(table_name).run
end

#table_wait(*options) ⇒ Object

def initialize



69
70
71
# File 'lib/rethinkdb_helper.rb', line 69

def table_wait(*options)
  @table.wait(options).run
end

#wait_for_indexObject



183
184
185
# File 'lib/rethinkdb_helper.rb', line 183

def index_wait(*indexes)
  @table.index_wait(indexes).run
end

#wait_indexObject



184
185
186
# File 'lib/rethinkdb_helper.rb', line 184

def index_wait(*indexes)
  @table.index_wait(indexes).run
end