Class: Yapper::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/yapper/db.rb

Defined Under Namespace

Classes: Notifications, Transaction

Constant Summary collapse

@@dbs =
{}
@@queue =
Dispatch::Queue.new("#{NSBundle.mainBundle.bundleIdentifier}.yapper.db#{@name}")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DB

Returns a new instance of DB.



24
25
26
27
28
29
30
31
# File 'lib/yapper/db.rb', line 24

def initialize(options)
  @options = options
  @name = options[:name]
  @indexes = {}
  @search_indexes = {}

  self
end

Instance Attribute Details

#indexesObject (readonly)

Returns the value of attribute indexes.



22
23
24
# File 'lib/yapper/db.rb', line 22

def indexes
  @indexes
end

Class Method Details

.instanceObject



13
14
15
16
17
18
19
20
# File 'lib/yapper/db.rb', line 13

def self.instance
  @@db = begin
           @@queue.sync do
             @@dbs[name] ||= self.new(:name => name)
           end
           @@dbs[name]
         end
end

.purgeObject



9
10
11
# File 'lib/yapper/db.rb', line 9

def self.purge
  default.purge
end

Instance Method Details

#configure(&block) ⇒ Object



33
34
35
# File 'lib/yapper/db.rb', line 33

def configure(&block)
  block.call(db)
end

#connectionObject



106
107
108
109
# File 'lib/yapper/db.rb', line 106

def connection
  Dispatch.once { @connection ||= db.newConnection }
  @connection
end

#dbObject



111
112
113
114
# File 'lib/yapper/db.rb', line 111

def db
  Dispatch.once { @db ||= YapDatabase.alloc.initWithPath(document_path) }
  @db
end

#execute(notifications = {}, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/yapper/db.rb', line 37

def execute(notifications={}, &block)
  Notifications.track(notifications)

  create_indexes!
  create_search_indexes!

  result = nil
  unless self.transaction
    self.transaction = Transaction.new(self)
    begin
      result = transaction.run(&block)
    ensure
      self.transaction = nil
    end
    Notifications.trigger
  else
    result = block.call(self.transaction.txn)
  end

  result
end

#index(model, args = []) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yapper/db.rb', line 79

def index(model, args=[])
  options = args.extract_options!

  @index_creation_required = true
  @indexes[model._type] ||= {}

  args.each do |field|
    options = model.fields[field]; raise "#{model._type}:#{field} not defined" unless options
    type    = options[:type];      raise "#{model._type}:#{field} must define type as its indexed" if type.nil?

    @indexes[model._type][field] = { :type => type }
  end
end

#purgeObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/yapper/db.rb', line 59

def purge
  Yapper::Settings.purge

  @index_creation_required = true
  @search_index_creation_required = true

  create_indexes!
  create_search_indexes!

  execute { |txn| txn.removeAllObjectsInAllCollections }
end

#search_index(model, args = []) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/yapper/db.rb', line 93

def search_index(model, args=[])
  options = args.extract_options!

  @search_index_creation_required = true
  @search_indexes[model._type] ||= []

  args.each do |field|
    options = model.fields[field]; raise "#{model._type}:#{field} not defined" unless options

    @search_indexes[model._type] << field
  end
end

#transactionObject



75
76
77
# File 'lib/yapper/db.rb', line 75

def transaction
  Thread.current[:yapper_transaction]
end

#transaction=(transaction) ⇒ Object



71
72
73
# File 'lib/yapper/db.rb', line 71

def transaction=(transaction)
  Thread.current[:yapper_transaction] = transaction
end