Class: TacScribe::Datastore

Inherits:
Object
  • Object
show all
Includes:
GeoRuby::SimpleFeatures, Singleton
Defined in:
lib/tac_scribe/datastore.rb

Overview

Acts as the interface to the back-end datastore and hides all datastore implementation details from callers. Note that ruby does not support exception chaining we are not wrapping exceptions yet. This will happen when bugs.ruby-lang.org/issues/8257 is fixed.

Defined Under Namespace

Classes: Configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dbObject

Returns the value of attribute db.



17
18
19
# File 'lib/tac_scribe/datastore.rb', line 17

def db
  @db
end

#deletedObject

Returns the value of attribute deleted.



17
18
19
# File 'lib/tac_scribe/datastore.rb', line 17

def deleted
  @deleted
end

#writtenObject

Returns the value of attribute written.



17
18
19
# File 'lib/tac_scribe/datastore.rb', line 17

def written
  @written
end

Instance Method Details

#configurationObject



22
23
24
# File 'lib/tac_scribe/datastore.rb', line 22

def configuration
  @configuration ||= Configuration.new
end

#configure {|@configuration| ... } ⇒ Object

Yields:



26
27
28
29
# File 'lib/tac_scribe/datastore.rb', line 26

def configure
  configuration
  yield(@configuration) if block_given?
end

#connectObject



45
46
47
48
49
# File 'lib/tac_scribe/datastore.rb', line 45

def connect
  configure
  @db = Sequel.connect(connection_string, max_connections: 49)
  @db.extension :postgis_georuby
end

#truncate_tableObject



51
52
53
# File 'lib/tac_scribe/datastore.rb', line 51

def truncate_table
  @db[:units].truncate
end

#write_objects(objects) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tac_scribe/datastore.rb', line 55

def write_objects(objects)
  objects = objects.map do |object|
    obj = object.clone
    obj.delete(:game_time)
    obj
  end

  @db[:units].insert_conflict(
    constraint: :units_pkey,
    update: { position: Sequel[:excluded][:position],
              altitude: Sequel[:excluded][:altitude],
              heading: Sequel[:excluded][:heading],
              speed: Sequel[:excluded][:speed],
              updated_at: Sequel[:excluded][:updated_at],
              deleted: Sequel[:excluded][:deleted] }
  )
             .multi_insert(objects)
  deleted_ids = @db[:units].where(deleted: true).select_map(:id)
  @db[:units].where(deleted: true).delete
  self.written = objects.size
  self.deleted = deleted_ids.size

  deleted_ids
end