Class: Friendly::DataStore
- Inherits:
-
Object
- Object
- Friendly::DataStore
- Defined in:
- lib/friendly/data_store.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Instance Method Summary collapse
- #all(persistable, query) ⇒ Object
- #count(persistable, query) ⇒ Object
- #delete(persistable, id) ⇒ Object
- #first(persistable, query) ⇒ Object
- #flush_batch ⇒ Object
-
#initialize(database) ⇒ DataStore
constructor
A new instance of DataStore.
- #insert(persistable, attributes) ⇒ Object
- #reset_batch ⇒ Object
- #start_batch ⇒ Object
- #update(persistable, id, attributes) ⇒ Object
Constructor Details
#initialize(database) ⇒ DataStore
Returns a new instance of DataStore.
5 6 7 |
# File 'lib/friendly/data_store.rb', line 5 def initialize(database) @database = database end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
3 4 5 |
# File 'lib/friendly/data_store.rb', line 3 def database @database end |
Instance Method Details
#all(persistable, query) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/friendly/data_store.rb', line 14 def all(persistable, query) filtered = dataset(persistable).where(query.conditions) if query.limit || query.offset filtered = filtered.limit(query.limit, query.offset) end filtered = filtered.order(query.order) if query.order filtered.map end |
#count(persistable, query) ⇒ Object
35 36 37 |
# File 'lib/friendly/data_store.rb', line 35 def count(persistable, query) dataset(persistable).where(query.conditions).count end |
#delete(persistable, id) ⇒ Object
31 32 33 |
# File 'lib/friendly/data_store.rb', line 31 def delete(persistable, id) dataset(persistable).where(:id => id).delete end |
#first(persistable, query) ⇒ Object
23 24 25 |
# File 'lib/friendly/data_store.rb', line 23 def first(persistable, query) dataset(persistable).first(query.conditions) end |
#flush_batch ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/friendly/data_store.rb', line 47 def flush_batch batch = Thread.current[:friendly_batch] batch.keys.each do |k| database.from(k).multi_insert(batch[k], :commit_every => 1000) end reset_batch end |
#insert(persistable, attributes) ⇒ Object
9 10 11 12 |
# File 'lib/friendly/data_store.rb', line 9 def insert(persistable, attributes) batch? ? batch_insert(persistable, attributes) : immediate_insert(persistable, attributes) end |
#reset_batch ⇒ Object
43 44 45 |
# File 'lib/friendly/data_store.rb', line 43 def reset_batch Thread.current[:friendly_batch] = nil end |
#start_batch ⇒ Object
39 40 41 |
# File 'lib/friendly/data_store.rb', line 39 def start_batch Thread.current[:friendly_batch] = Hash.new { |h, k| h[k] = [] } end |
#update(persistable, id, attributes) ⇒ Object
27 28 29 |
# File 'lib/friendly/data_store.rb', line 27 def update(persistable, id, attributes) dataset(persistable).where(:id => id).update(attributes) end |