Class: TacScribe::Datastore
- Inherits:
-
Object
- Object
- TacScribe::Datastore
- 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
-
#db ⇒ Object
Returns the value of attribute db.
-
#deleted ⇒ Object
Returns the value of attribute deleted.
-
#written ⇒ Object
Returns the value of attribute written.
Instance Method Summary collapse
- #configuration ⇒ Object
- #configure {|@configuration| ... } ⇒ Object
- #connect ⇒ Object
- #truncate_table ⇒ Object
- #write_objects(objects) ⇒ Object
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
17 18 19 |
# File 'lib/tac_scribe/datastore.rb', line 17 def db @db end |
#deleted ⇒ Object
Returns the value of attribute deleted.
17 18 19 |
# File 'lib/tac_scribe/datastore.rb', line 17 def deleted @deleted end |
#written ⇒ Object
Returns the value of attribute written.
17 18 19 |
# File 'lib/tac_scribe/datastore.rb', line 17 def written @written end |
Instance Method Details
#configuration ⇒ Object
22 23 24 |
# File 'lib/tac_scribe/datastore.rb', line 22 def configuration @configuration ||= Configuration.new end |
#configure {|@configuration| ... } ⇒ Object
26 27 28 29 |
# File 'lib/tac_scribe/datastore.rb', line 26 def configure configuration yield(@configuration) if block_given? end |
#connect ⇒ Object
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_table ⇒ Object
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 |