Module: Zermelo::Records::ClassMethods

Extended by:
Forwardable
Includes:
Attributes
Defined in:
lib/zermelo/records/class_methods.rb

Instance Method Summary collapse

Methods included from Attributes

#attribute_types

Instance Method Details

#add_id(id) ⇒ Object



40
41
42
# File 'lib/zermelo/records/class_methods.rb', line 40

def add_id(id)
  backend.add(ids_key, id.to_s)
end

#backendObject



73
74
75
76
# File 'lib/zermelo/records/class_methods.rb', line 73

def backend
  raise "No data storage backend set for #{self.name}" if @backend.nil?
  @backend
end

#delete_id(id) ⇒ Object



44
45
46
# File 'lib/zermelo/records/class_methods.rb', line 44

def delete_id(id)
  backend.delete(ids_key, id.to_s)
end

#generate_idObject



31
32
33
34
35
36
37
38
# File 'lib/zermelo/records/class_methods.rb', line 31

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

#lock(*klasses, &block) ⇒ Object



48
49
50
51
# File 'lib/zermelo/records/class_methods.rb', line 48

def lock(*klasses, &block)
  klasses += [self] unless klasses.include?(self)
  backend.lock(*klasses, &block)
end

#transaction(&block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zermelo/records/class_methods.rb', line 53

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