Module: Zermelo::Records::ClassMethods
Instance Method Summary collapse
- #backend ⇒ Object
- #generate_id ⇒ Object
- #key_dump ⇒ Object
- #lock(*klasses, &block) ⇒ Object
- #transaction(&block) ⇒ Object
Methods included from Attributes
Instance Method Details
#backend ⇒ Object
62 63 64 65 |
# File 'lib/zermelo/records/class_methods.rb', line 62 def backend raise "No data storage backend set for #{self.name}" if @backend.nil? @backend end |
#generate_id ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/zermelo/records/class_methods.rb', line 28 def generate_id return SecureRandom.uuid if SecureRandom.respond_to?(:uuid) # from 1.9 stdlib ary = SecureRandom.random_bytes(16).unpack("NnnnnN") ary[2] = (ary[2] & 0x0fff) | 0x4000 ary[3] = (ary[3] & 0x3fff) | 0x8000 "%08x-%04x-%04x-%04x-%04x%08x" % ary end |
#key_dump ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/zermelo/records/class_methods.rb', line 67 def key_dump klass_keys = [backend.key_to_backend_key(ids_key), ids_key] self.send(:with_index_data) do |d| d.keys.each do |k| klass_keys += self.send("#{k}_index".to_sym).key_dump end end Hash[ *klass_keys ] end |
#lock(*klasses, &block) ⇒ Object
37 38 39 40 |
# File 'lib/zermelo/records/class_methods.rb', line 37 def lock(*klasses, &block) klasses += [self] unless klasses.include?(self) backend.lock(*klasses, &block) end |
#transaction(&block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/zermelo/records/class_methods.rb', line 42 def transaction(&block) failed = false backend.begin_transaction begin yield rescue Exception # => e backend.abort_transaction # p e.message # puts e.backtrace.join("\n") failed = true ensure backend.commit_transaction unless failed end # TODO include exception info raise "Transaction failed" if failed end |