Class: Oculus::Storage::SequelStore
- Inherits:
-
Object
- Object
- Oculus::Storage::SequelStore
- Defined in:
- lib/oculus/storage/sequel_store.rb
Instance Attribute Summary collapse
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
- #all_queries ⇒ Object
- #create_table ⇒ Object
- #delete_query(id) ⇒ Object
- #drop_table ⇒ Object
-
#initialize(options = {}) ⇒ SequelStore
constructor
A new instance of SequelStore.
- #load_query(id) ⇒ Object
- #save_query(query) ⇒ Object
- #starred_queries ⇒ Object
- #with_db ⇒ Object
- #with_table ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ SequelStore
Returns a new instance of SequelStore.
9 10 11 12 13 14 |
# File 'lib/oculus/storage/sequel_store.rb', line 9 def initialize( = {}) raise ArgumentError, "URI is required" unless [:uri] @uri = [:uri] @table_name = [:table] || :oculus create_table end |
Instance Attribute Details
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
7 8 9 |
# File 'lib/oculus/storage/sequel_store.rb', line 7 def table_name @table_name end |
Instance Method Details
#all_queries ⇒ Object
27 28 29 30 31 |
# File 'lib/oculus/storage/sequel_store.rb', line 27 def all_queries with_table do |table| to_queries table.order(:id.desc) end end |
#create_table ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/oculus/storage/sequel_store.rb', line 67 def create_table with_db do |db| db.create_table?(table_name) do primary_key :id Integer :thread_id String :name String :author String :query String :results Time :started_at Time :finished_at TrueClass :starred String :error end end end |
#delete_query(id) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/oculus/storage/sequel_store.rb', line 60 def delete_query(id) with_table do |table| raise ArgumentError unless id.to_i > 0 raise QueryNotFound, id unless table.where(:id => id).delete == 1 end end |
#drop_table ⇒ Object
84 85 86 |
# File 'lib/oculus/storage/sequel_store.rb', line 84 def drop_table with_db { |db| db.drop_table(table_name) } end |
#load_query(id) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/oculus/storage/sequel_store.rb', line 50 def load_query(id) with_table do |table| if query = table.where(:id => id).first deserialize query else raise QueryNotFound, id end end end |
#save_query(query) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/oculus/storage/sequel_store.rb', line 39 def save_query(query) attrs = serialize(query) with_table do |table| if query.id table.where(:id => query.id).update(attrs) else query.id = table.insert(attrs) end end end |
#starred_queries ⇒ Object
33 34 35 36 37 |
# File 'lib/oculus/storage/sequel_store.rb', line 33 def starred_queries with_table do |table| to_queries table.where(:starred => true).order(:id.desc) end end |
#with_db ⇒ Object
16 17 18 19 20 21 |
# File 'lib/oculus/storage/sequel_store.rb', line 16 def with_db db = Sequel.connect(@uri, :encoding => 'utf8') result = yield db db.disconnect result end |
#with_table ⇒ Object
23 24 25 |
# File 'lib/oculus/storage/sequel_store.rb', line 23 def with_table with_db { |db| yield db.from(@table_name) } end |