Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #108] Only stores one class of a given name to avoid memory le…
…aks when classes are reloaded (e.g., Rails development mode)
- Loading branch information
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Sunspot | ||
class ClassSet | ||
include Enumerable | ||
|
||
def initialize | ||
@name_to_klass = {} | ||
end | ||
|
||
def <<(klass) | ||
@name_to_klass[klass.name.to_sym] = klass | ||
self | ||
end | ||
alias_method :add, :<< | ||
|
||
def each(&block) | ||
@name_to_klass.values.each(&block) | ||
end | ||
|
||
def empty? | ||
@name_to_klass.empty? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "spec_helper" | ||
|
||
describe Sunspot::ClassSet do | ||
it "is enumerable" do | ||
class1, class2 = stub(:name => "Class1"), stub(:name => "Class2") | ||
|
||
set = described_class.new | ||
set << class1 << class2 | ||
|
||
set.to_a.should == [class1, class2] | ||
end | ||
|
||
it "replaces classes with the same name" do | ||
set = described_class.new | ||
|
||
class1 = stub(:name => "Class1") | ||
set << class1 | ||
set.to_a.should == [class1] | ||
|
||
class1_dup = stub(:name => "Class1") | ||
set << class1_dup | ||
set.to_a.should == [class1_dup] | ||
end | ||
end |