Permalink
Browse files

Also get receiver by string.

  • Loading branch information...
1 parent 5586401 commit 9643fe1646417e2a27d0cee88fcd9ef893a4c737 rking committed May 2, 2012
Showing with 22 additions and 3 deletions.
  1. +11 −2 lib/pegex/parser.rb
  2. +8 −1 spec/parser_spec.rb
  3. +3 −0 spec/testrecvr.rb
View
@@ -19,13 +19,18 @@ def parse input, args = {}
# XXX "input" is a simple string ATM.
@input = input.clone
find_grammar
+ find_receiver
start_rule = find_start_rule args[:start_rule]
end
def find_grammar
raise 'No grammar specified' if @grammar.nil?
if ::String == @grammar.class
- require @grammar
- @grammar = eval(camelize @grammar).new
+ @grammar = string_new @grammar
+ end
+ end
+ def find_receiver
+ if ::String == @receiver.class
+ @receiver = string_new @receiver
end
end
def find_start_rule explicit = nil
@@ -40,6 +45,10 @@ def default_receiver maybe_recvr
Receiver.new
end
end
+ def string_new path
+ require path
+ eval(camelize path).new
+ end
# (Mostly) lifted from ActiveSupport::Inflector#camelize -
def camelize string
string = string.to_s.capitalize
View
@@ -33,7 +33,6 @@
@prs.grammar = 'testgrammar'
@prs.parse @input
@prs.grammar.class.should eq Testgrammar
- @prs.camelize('hi_hi/bi').should eq 'HiHi::Bi'
end
it 'should not alter actual input' do
before = @input.clone
@@ -52,4 +51,12 @@
@prs.grammar = tmp
@prs.find_start_rule.should eq 'TOP'
end
+ it "should pull in receiver by string" do
+ @prs.receiver = 'testrecvr'
+ @prs.parse @input
+ @prs.receiver.class.should eq Testrecvr
+ end
+ it "should camelize" do
+ @prs.camelize('hi_hi/bi').should eq 'HiHi::Bi'
+ end
end
View
@@ -0,0 +1,3 @@
+class Testrecvr
+ # ... ?
+end

0 comments on commit 9643fe1

Please sign in to comment.