Skip to content

Commit

Permalink
Also get receiver by string.
Browse files Browse the repository at this point in the history
  • Loading branch information
rking committed May 2, 2012
1 parent 5586401 commit 9643fe1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/pegex/parser.rb
Expand Up @@ -19,13 +19,18 @@ def parse input, args = {}
# XXX "input" is a simple string ATM. # XXX "input" is a simple string ATM.
@input = input.clone @input = input.clone
find_grammar find_grammar
find_receiver
start_rule = find_start_rule args[:start_rule] start_rule = find_start_rule args[:start_rule]
end end
def find_grammar def find_grammar
raise 'No grammar specified' if @grammar.nil? raise 'No grammar specified' if @grammar.nil?
if ::String == @grammar.class if ::String == @grammar.class
require @grammar @grammar = string_new @grammar
@grammar = eval(camelize @grammar).new end
end
def find_receiver
if ::String == @receiver.class
@receiver = string_new @receiver
end end
end end
def find_start_rule explicit = nil def find_start_rule explicit = nil
Expand All @@ -40,6 +45,10 @@ def default_receiver maybe_recvr
Receiver.new Receiver.new
end end
end end
def string_new path
require path
eval(camelize path).new
end
# (Mostly) lifted from ActiveSupport::Inflector#camelize - # (Mostly) lifted from ActiveSupport::Inflector#camelize -
def camelize string def camelize string
string = string.to_s.capitalize string = string.to_s.capitalize
Expand Down
9 changes: 8 additions & 1 deletion spec/parser_spec.rb
Expand Up @@ -33,7 +33,6 @@
@prs.grammar = 'testgrammar' @prs.grammar = 'testgrammar'
@prs.parse @input @prs.parse @input
@prs.grammar.class.should eq Testgrammar @prs.grammar.class.should eq Testgrammar
@prs.camelize('hi_hi/bi').should eq 'HiHi::Bi'
end end
it 'should not alter actual input' do it 'should not alter actual input' do
before = @input.clone before = @input.clone
Expand All @@ -52,4 +51,12 @@
@prs.grammar = tmp @prs.grammar = tmp
@prs.find_start_rule.should eq 'TOP' @prs.find_start_rule.should eq 'TOP'
end 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 end
3 changes: 3 additions & 0 deletions spec/testrecvr.rb
@@ -0,0 +1,3 @@
class Testrecvr
# ... ?
end

0 comments on commit 9643fe1

Please sign in to comment.