Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Namespace AssemblyLine when used in irb
When using AssemblyLine in the console, you now must prefix your 'let' definitions with AssemblyLine. The methods are no longer defined at the global level. Old: >> Assemble(:drinks) >> drinks => [:gin, :vodka] New: >> Assemble(:drinks) >> AssemblyLine.drinks => [:gin, :vodka] Apparently, defining an arbitrary set of methods on Kernel can wreak havoc on your software. While testing AssemblyLine in irb I encountered a recursive loop caused by method_missing. A NameError should have been raised but instead one of my 'let' definitions was being called because it was defined globally. Heres an example: >> def foo >> :bar >> end => nil >> class A >> def baz >> foo >> end >> end => nil >> A.new.baz => :bar A#baz should raise a NameError but instead, #foo gets called because it is defined globally.
- Loading branch information
Showing
6 changed files
with
58 additions
and
63 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
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,34 @@ | ||
require 'spec_helper' | ||
|
||
describe AssemblyLine::GenericContext do | ||
let(:generic_context) { AssemblyLine.generic_context } | ||
|
||
after { generic_context.clear } | ||
|
||
describe "#let" do | ||
it "defines the method on the GenericContext instance" do | ||
generic_context.let(:foobarish) { :bar } | ||
generic_context.foobarish.should == :bar | ||
end | ||
|
||
it "memoizes the defined method" do | ||
generic_context.instance_variable_set(:@count, 0) | ||
generic_context.let(:my_count) { @count += 1 } | ||
5.times { generic_context.my_count } | ||
generic_context.my_count.should == 1 | ||
end | ||
|
||
it "adds a delegating method to AssemblyLine" do | ||
generic_context.let(:global_method) { :global } | ||
AssemblyLine.global_method.should == :global | ||
end | ||
end | ||
|
||
describe "#before" do | ||
it "runs the code block" do | ||
expect do | ||
generic_context.before { @done = true } | ||
end.to change { generic_context.instance_variable_get(:@done) }.from(nil).to(true) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,26 @@ | ||
require 'spec_helper' | ||
|
||
global_assembly_set_up = lambda do | ||
set_up_assembly = lambda do | ||
AssemblyLine.define(:global_assembly) do | ||
let(:foo) { @bar +=1 } | ||
let(:foo) { @bar += 1 } | ||
before do | ||
@bar = 0 | ||
end | ||
end | ||
Assemble(:global_assembly) | ||
end | ||
|
||
def call_global_assembly(count = 5) | ||
(count - 1).times { foo } | ||
foo | ||
end | ||
|
||
describe "in the global context" do | ||
before(:all) do | ||
global_assembly_set_up.call | ||
before do | ||
set_up_assembly.call | ||
end | ||
|
||
it "defined the method within AssemblyLine::GlobalContext" do | ||
AssemblyLine::GlobalContext.should respond_to(:foo) | ||
it "defines the method on AssemblyLine.generic_context" do | ||
AssemblyLine.generic_context.should respond_to(:foo) | ||
end | ||
|
||
it "does not increase the value because the method is memoized" do | ||
call_global_assembly.should == 1 | ||
it "memoizes the value" do | ||
5.times { AssemblyLine.foo } | ||
AssemblyLine.foo.should == 1 | ||
end | ||
end |