Module: Leases::Model::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/leases/model/base.rb

Instance Method Summary collapse

Instance Method Details

#break!Object

Break a lease. This is usually called when a model is destroyed.

> account.break!



69
70
71
# File 'lib/leases/model/base.rb', line 69

def break!
  Apartment::Database.drop(leaser_name)
end

#enterObject

Enter the leaser-context.

> account.enter



31
32
33
34
# File 'lib/leases/model/base.rb', line 31

def enter
  Apartment::Database.switch(leaser_name)
  Thread.current[:leaser] = self
end

#lease!Object

Create a new lease. This is usually called when a model is created.

> account.lease!



61
62
63
# File 'lib/leases/model/base.rb', line 61

def lease!
  Apartment::Database.create(leaser_name)
end

#leaser_nameObject

Name of the leaser, used for naming the database. This can be set in the leaser_options. Must be unique.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/leases/model/base.rb', line 16

def leaser_name
  name = self.class.leases_options[:name]

  if name.nil?
    [self.class.name.parameterize, id].join('-')
  elsif name.is_a?(Symbol)
    send(name)
  elsif name.is_a?(Proc)
    name.call(self)
  end
end

#leaveObject

Leave the leaser-context.

> account.leave



39
40
41
42
# File 'lib/leases/model/base.rb', line 39

def leave
  Thread.current[:leaser] = nil
  Apartment::Database.reset
end

#visit(&block) ⇒ Object

Visit a leaser by entering and leaving. Very useful for executing code in a leaser-context

> account.visit { User.find(1) }



48
49
50
51
52
53
54
55
# File 'lib/leases/model/base.rb', line 48

def visit(&block)
  enter
  begin
    yield
  ensure
    leave
  end
end