Skip to content

Commit

Permalink
Adding in loading thread local behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 15, 2011
1 parent ff34f0d commit 1a747d0
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/mongoid/threaded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ def begin_create
create_stack.push(true)
end

# Begins a creating block.
#
# @example Begin the load.
# Threaded.begin_load
#
# @return [ true ] Always true.
#
# @since 2.3.2
def begin_load
load_stack.push(true)
end

# Begin validating a document on the current thread.
#
# @example Begin validation.
Expand Down Expand Up @@ -116,6 +128,18 @@ def creating?
!create_stack.empty?
end

# Is the current thread in creating mode?
#
# @example Is the thread in creating mode?
# Threaded.creating?
#
# @return [ true, false ] If the thread is in creating mode?
#
# @since 2.3.2
def loading?
!load_stack.empty?
end

# Get the assign stack for the current thread. Is simply an array of calls
# to Mongoid's assigning method.
#
Expand Down Expand Up @@ -168,6 +192,19 @@ def create_stack
Thread.current[:"[mongoid]:create-stack"] ||= []
end

# Get the load stack for the current thread. Is simply an array of calls
# to Mongoid's loading method.
#
# @example Get the load stack.
# Threaded.load_stack
#
# @return [ Array ] The array of load calls.
#
# @since 2.3.2
def load_stack
Thread.current[:"[mongoid]:load-stack"] ||= []
end

# Clear out all the safety options set using the safely proxy.
#
# @example Clear out the options.
Expand Down Expand Up @@ -239,6 +276,18 @@ def exit_create
create_stack.pop
end

# Exit the loading block.
#
# @example Exit the loading block.
# Threaded.exit_load
#
# @return [ true ] The last element in the stack.
#
# @since 2.1.9
def exit_load
load_stack.pop
end

# Exit validating a document on the current thread.
#
# @example Exit validation.
Expand Down
29 changes: 29 additions & 0 deletions lib/mongoid/threaded/lifecycle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@ def _creating?
Threaded.creating?
end

# Execute a block in loading mode.
#
# @example Execute in loading mode.
# _loading do
# relation.push(doc)
# end
#
# @return [ Object ] The return value of the block.
#
# @since 2.3.2
def _loading
Threaded.begin_build
yield
ensure
Threaded.exit_build
end

# Is the current thread in loading mode?
#
# @example Is the current thread in loading mode?
# proxy._loading?
#
# @return [ true, false ] If the thread is loading.
#
# @since 2.3.2
def _loading?
Threaded.loading?
end

module ClassMethods #:nodoc:

# Execute a block in creating mode.
Expand Down
100 changes: 100 additions & 0 deletions spec/unit/mongoid/threaded_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,90 @@
stub
end

describe "#begin_load" do

before do
described_class.begin_load
end

after do
described_class.load_stack.clear
end

it "adds a boolen to the load stack" do
described_class.load_stack.should eq([ true ])
end
end

describe "#loading?" do

context "when loading is not set" do

it "returns false" do
described_class.should_not be_loading
end
end

context "when loading has elements" do

before do
Thread.current[:"[mongoid]:load-stack"] = [ true ]
end

after do
Thread.current[:"[mongoid]:load-stack"] = []
end

it "returns true" do
described_class.should be_loading
end
end

context "when loading has no elements" do

before do
Thread.current[:"[mongoid]:load-stack"] = []
end

it "returns false" do
described_class.should_not be_loading
end
end
end

describe "#load_stack" do

context "when no load stack has been initialized" do

let(:loading) do
described_class.load_stack
end

it "returns an empty stack" do
loading.should eq([])
end
end

context "when a load stack has been initialized" do

before do
Thread.current[:"[mongoid]:load-stack"] = [ true ]
end

let(:loading) do
described_class.load_stack
end

after do
Thread.current[:"[mongoid]:load-stack"] = []
end

it "returns the stack" do
loading.should eq([ true ])
end
end
end

describe "#begin_assign" do

before do
Expand Down Expand Up @@ -370,6 +454,22 @@
end
end

describe "#exit_load" do

before do
described_class.begin_load
described_class.exit_load
end

after do
described_class.load_stack.clear
end

it "adds a boolen to the load stack" do
described_class.load_stack.should be_empty
end
end

describe "#exit_bind" do

before do
Expand Down

0 comments on commit 1a747d0

Please sign in to comment.