Class: Friendly::DataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly/data_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#databaseObject (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
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

#count(persistable, query) ⇒ Object



36
37
38
# File 'lib/friendly/data_store.rb', line 36

def count(persistable, query)
  dataset(persistable).where(query.conditions).count
end

#delete(persistable, id) ⇒ Object



32
33
34
# File 'lib/friendly/data_store.rb', line 32

def delete(persistable, id)
  dataset(persistable).where(:id => id).delete
end

#first(persistable, query) ⇒ Object



24
25
26
# File 'lib/friendly/data_store.rb', line 24

def first(persistable, query)
  dataset(persistable).first(query.conditions)
end

#flush_batchObject



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

#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_batchObject



44
45
46
# File 'lib/friendly/data_store.rb', line 44

def reset_batch
  Thread.current[:friendly_batch] = nil
end

#start_batchObject



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

#update(persistable, id, attributes) ⇒ Object



28
29
30
# File 'lib/friendly/data_store.rb', line 28

def update(persistable, id, attributes)
  dataset(persistable).where(:id => id).update(attributes)
end