The code here is a simplification of the bigger solution. I'm trying to figure out how to make a ruby DSL that "reads" nicely. The first block of code; works (works for now) I would like to know how to write the DSL
The core of the problem is that while the class is being processed, it doesn't have an instance variables for me to work with. IE: @match_code
Does anyone know of a simpler more elegant solution? The entirety of code must kept with in a class.
Want I want it to look like is:
class MatchStuff
include ProcessBase
match 'account' do | event |
pp event
end
end
matcher = MatchStuff.new
matcher.accept 'account'
Current working (not so nice) code
class ProcessBase
def initialize
@match_code = []
end
def match(string_match, &block)
@match_code.push([string_match, block])
end
def accept(test_str)
@match_code.each do | test_block |
if test_str == test_block[0])
test_block[1].call test
end
end
end
end
class MatchStuff < ProcessBase
def initialize
super
match 'account' do | event |
pp event
end
end
end
test = MatchStuff.new
test.accept 'account'