Class: TTT::RiakDB

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt/riak_db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RiakDB

Returns a new instance of RiakDB.



6
7
8
9
10
11
12
# File 'lib/ttt/riak_db.rb', line 6

def initialize(options)
  port            = options.fetch(:port, 8098)
  http_backend    = options.fetch(:http_backend, :Excon)
  self.bucket     = options.fetch(:bucket)
  self.client     = Riak::Client.new(:http_port => port)
  self.cur_id     = client[bucket].keys.length.to_s
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



5
6
7
# File 'lib/ttt/riak_db.rb', line 5

def bucket
  @bucket
end

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/ttt/riak_db.rb', line 5

def client
  @client
end

#cur_idObject

Returns the value of attribute cur_id.



5
6
7
# File 'lib/ttt/riak_db.rb', line 5

def cur_id
  @cur_id
end

Instance Method Details

#add_game(game) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/ttt/riak_db.rb', line 14

def add_game(game)
  self.cur_id = inc_cur_id
  new_game              = client[bucket].get_or_new(cur_id)
  new_game.content_type = "text/yaml"
  new_game.data         = { :game => game, :id => cur_id }
  new_game.store
  cur_id
end

#delete_game(id) ⇒ Object



43
44
45
46
# File 'lib/ttt/riak_db.rb', line 43

def delete_game(id)
  riak_game      = client[bucket].get(id)
  riak_game.delete
end

#game_listObject



23
24
25
# File 'lib/ttt/riak_db.rb', line 23

def game_list
  client[bucket].keys.map(&:to_i).sort.map(&:to_s)
end

#get_game(id) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/ttt/riak_db.rb', line 27

def get_game(id)
  begin
    game = client[bucket].get(id.to_s)
  rescue Riak::HTTPFailedRequest
    nil
  else
    game.data[:game]
  end
end

#save_game(id, game) ⇒ Object



37
38
39
40
41
# File 'lib/ttt/riak_db.rb', line 37

def save_game(id, game)
  riak_game      = client[bucket].get(id)
  riak_game.data = { :game => game, :id => id }
  riak_game.store
end