-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This class is responsible for holding as set of references to classes (by their name). This is done instead of just having the classes to avoid loading the classes too early as well as to allow code reloading of these classes.
- Loading branch information
John Hawthorn
committed
May 30, 2016
1 parent
257bc28
commit 0e2c9cd
Showing
8 changed files
with
125 additions
and
17 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,31 @@ | ||
module Spree | ||
module Core | ||
module ClassConstantizer | ||
class Set | ||
include Enumerable | ||
|
||
def initialize | ||
@collection = ::Set.new | ||
end | ||
|
||
def <<(klass) | ||
@collection << klass.to_s | ||
end | ||
|
||
def concat(klasses) | ||
klasses.each do |klass| | ||
self << klass | ||
end | ||
end | ||
|
||
delegate :clear, :empty?, to: :@collection | ||
|
||
def each | ||
@collection.each do |klass| | ||
yield klass.constantize | ||
end | ||
end | ||
end | ||
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
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
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
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,68 @@ | ||
require 'spec_helper' | ||
|
||
module ClassConstantizerTest | ||
ClassA = Class.new | ||
ClassB = Class.new | ||
|
||
def self.reload | ||
[:ClassA, :ClassB].each do |klass| | ||
remove_const(klass) | ||
const_set(klass, Class.new) | ||
end | ||
end | ||
end | ||
|
||
describe Spree::Core::ClassConstantizer::Set do | ||
let(:set) { described_class.new } | ||
|
||
describe "#concat" do | ||
it "can add one item" do | ||
set.concat(['ClassConstantizerTest::ClassA']) | ||
expect(set).to include(ClassConstantizerTest::ClassA) | ||
end | ||
|
||
it "can add two items" do | ||
set.concat(['ClassConstantizerTest::ClassA', ClassConstantizerTest::ClassB]) | ||
expect(set).to include(ClassConstantizerTest::ClassA) | ||
expect(set).to include(ClassConstantizerTest::ClassB) | ||
end | ||
end | ||
|
||
describe "<<" do | ||
it "can add by string" do | ||
set << "ClassConstantizerTest::ClassA" | ||
expect(set).to include(ClassConstantizerTest::ClassA) | ||
end | ||
|
||
it "can add by class" do | ||
set << ClassConstantizerTest::ClassA | ||
expect(set).to include(ClassConstantizerTest::ClassA) | ||
end | ||
|
||
describe "class redefinition" do | ||
shared_examples "working code reloading" do | ||
it "works with a class" do | ||
original = ClassConstantizerTest::ClassA | ||
|
||
ClassConstantizerTest.reload | ||
|
||
# Sanity check | ||
expect(original).not_to eq(ClassConstantizerTest::ClassA) | ||
|
||
expect(set).to include(ClassConstantizerTest::ClassA) | ||
expect(set).to_not include(original) | ||
end | ||
end | ||
|
||
context "with a class" do | ||
before { set << ClassConstantizerTest::ClassA } | ||
it_should_behave_like "working code reloading" | ||
end | ||
|
||
context "with a string" do | ||
before { set << "ClassConstantizerTest::ClassA" } | ||
it_should_behave_like "working code reloading" | ||
end | ||
end | ||
end | ||
end |
@jhawthorn It seems that if we do it this way, when the get method is called the first time,
nil
would be returned. (although [] would be defined to that property). That might cause the issue if the caller assume at least[]
to be returned.