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
# File 'lib/yapper/db.rb', line 24

def initialize(options)
  @options = options
  @name = options[:name]
  @indexes = {}; @indexes_created = false

  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



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

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

#connectionObject



86
87
88
89
# File 'lib/yapper/db.rb', line 86

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

#dbObject



91
92
93
94
# File 'lib/yapper/db.rb', line 91

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

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



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

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

  create_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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yapper/db.rb', line 72

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

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

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

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

#purgeObject



57
58
59
60
61
62
# File 'lib/yapper/db.rb', line 57

def purge
  Yapper::Settings.purge
  @index_creation_required = true
  create_indexes!
  execute { |txn| txn.removeAllObjectsInAllCollections }
end

#transactionObject



68
69
70
# File 'lib/yapper/db.rb', line 68

def transaction
  Thread.current[:yapper_transaction]
end

#transaction=(transaction) ⇒ Object



64
65
66
# File 'lib/yapper/db.rb', line 64

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