Class: Friendly::DataStore
- Inherits:
-
Object
- Object
- Friendly::DataStore
- Defined in:
- lib/friendly/data_store.rb
Instance Attribute Summary (collapse)
-
- (Object) database
readonly
Returns the value of attribute database.
Instance Method Summary (collapse)
- - (Object) all(persistable, query)
- - (Object) count(persistable, query)
- - (Object) delete(persistable, id)
- - (Object) first(persistable, query)
- - (Object) flush_batch
-
- (DataStore) initialize(database)
constructor
A new instance of DataStore.
- - (Object) insert(persistable, attributes)
- - (Object) reset_batch
- - (Object) start_batch
- - (Object) update(persistable, id, attributes)
Constructor Details
- (DataStore) initialize(database)
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
- (Object) database (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
- (Object) all(persistable, query)
14 15 16 17 18 19 20 21 22 |
# File 'lib/friendly/data_store.rb', line 14 def all(persistable, query) filtered = dataset(persistable) filtered = filtered.where(query.conditions) unless query.conditions.empty? if query.limit || query.offset filtered = filtered.limit(query.limit, query.offset) end filtered = filtered.order(query.order) if query.order filtered.map end |
- (Object) count(persistable, query)
36 37 38 |
# File 'lib/friendly/data_store.rb', line 36 def count(persistable, query) dataset(persistable).where(query.conditions).count end |
- (Object) delete(persistable, id)
32 33 34 |
# File 'lib/friendly/data_store.rb', line 32 def delete(persistable, id) dataset(persistable).where(:id => id).delete end |
- (Object) first(persistable, query)
24 25 26 |
# File 'lib/friendly/data_store.rb', line 24 def first(persistable, query) dataset(persistable).first(query.conditions) end |
- (Object) flush_batch
48 49 50 51 52 53 54 |
# File 'lib/friendly/data_store.rb', line 48 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 |
- (Object) insert(persistable, attributes)
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 |
- (Object) reset_batch
44 45 46 |
# File 'lib/friendly/data_store.rb', line 44 def reset_batch Thread.current[:friendly_batch] = nil end |
- (Object) start_batch
40 41 42 |
# File 'lib/friendly/data_store.rb', line 40 def start_batch Thread.current[:friendly_batch] = Hash.new { |h, k| h[k] = [] } end |
- (Object) update(persistable, id, attributes)
28 29 30 |
# File 'lib/friendly/data_store.rb', line 28 def update(persistable, id, attributes) dataset(persistable).where(:id => id).update(attributes) end |