Class: Friendly::StorageProxy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, storage_factory = StorageFactory.new, table_creator = TableCreator.new) ⇒ StorageProxy

Returns a new instance of StorageProxy.



8
9
10
11
12
13
14
15
16
# File 'lib/friendly/storage_proxy.rb', line 8

def initialize(klass, storage_factory = StorageFactory.new,
                table_creator=TableCreator.new)
  super()
  @klass           = klass
  @storage_factory = storage_factory
  @table_creator   = table_creator
  @tables          = [storage_factory.document_table(klass)]
  @caches          = []
end

Instance Attribute Details

#cachesObject (readonly)

Returns the value of attribute caches.



6
7
8
# File 'lib/friendly/storage_proxy.rb', line 6

def caches
  @caches
end

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/friendly/storage_proxy.rb', line 6

def klass
  @klass
end

#storage_factoryObject (readonly)

Returns the value of attribute storage_factory.



6
7
8
# File 'lib/friendly/storage_proxy.rb', line 6

def storage_factory
  @storage_factory
end

#table_creatorObject (readonly)

Returns the value of attribute table_creator.



6
7
8
# File 'lib/friendly/storage_proxy.rb', line 6

def table_creator
  @table_creator
end

#tablesObject (readonly)

Returns the value of attribute tables.



6
7
8
# File 'lib/friendly/storage_proxy.rb', line 6

def tables
  @tables
end

Instance Method Details

#add(*args) ⇒ Object



38
39
40
# File 'lib/friendly/storage_proxy.rb', line 38

def add(*args)
  tables << storage_factory.index(klass, *args)
end

#all(query) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/friendly/storage_proxy.rb', line 24

def all(query)
  objects = perform_all(query).compact
  if query.preserve_order?
    order = query.conditions[:id]
    objects.sort { |a,b| order.index(a.id) <=> order.index(b.id) }
  else
    objects
  end
end

#cache(fields, options = {}) ⇒ Object



42
43
44
# File 'lib/friendly/storage_proxy.rb', line 42

def cache(fields, options = {})
  caches << storage_factory.cache(klass, fields, options)
end

#count(query) ⇒ Object



34
35
36
# File 'lib/friendly/storage_proxy.rb', line 34

def count(query)
  index_for(query).count(query)
end

#create(document) ⇒ Object



46
47
48
# File 'lib/friendly/storage_proxy.rb', line 46

def create(document)
  each_store { |s| s.create(document) }
end

#create_tables!Object



58
59
60
# File 'lib/friendly/storage_proxy.rb', line 58

def create_tables!
  tables.each { |t| table_creator.create(t) }
end

#destroy(document) ⇒ Object



54
55
56
# File 'lib/friendly/storage_proxy.rb', line 54

def destroy(document)
  stores.reverse.each { |i| i.destroy(document) }
end

#first(conditions) ⇒ Object



18
19
20
21
22
# File 'lib/friendly/storage_proxy.rb', line 18

def first(conditions)
  first_from_cache(conditions) do
    index_for(conditions).first(conditions)
  end
end

#index_for(conditions) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/friendly/storage_proxy.rb', line 62

def index_for(conditions)
  index = tables.detect { |i| i.satisfies?(conditions) }
  if index.nil?
    raise MissingIndex, "No index found to satisfy: #{conditions.inspect}."
  end
  index
end

#index_for_fields(fields) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/friendly/storage_proxy.rb', line 70

def index_for_fields(fields)
  tables.detect do |t|
    t.respond_to?(:fields) && t.fields == fields
  end.tap do |i|
    raise MissingIndex, "No index found matching #{fields.join(", ")}." if i.nil?
  end
end

#update(document) ⇒ Object



50
51
52
# File 'lib/friendly/storage_proxy.rb', line 50

def update(document)
  each_store { |s| s.update(document) }
end