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

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

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

- (Object) caches (readonly)

Returns the value of attribute caches



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

def caches
  @caches
end

- (Object) klass (readonly)

Returns the value of attribute klass



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

def klass
  @klass
end

- (Object) storage_factory (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

- (Object) table_creator (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

- (Object) tables (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

- (Object) add(*args)



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

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

- (Object) all(query)



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

- (Object) cache(fields, options = {})



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

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

- (Object) count(query)



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

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

- (Object) create(document)



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

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

- (Object) create_tables!



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

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

- (Object) destroy(document)



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

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

- (Object) first(conditions)



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

- (Object) index_for(conditions)



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

- (Object) index_for_fields(fields)



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

- (Object) update(document)



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

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