M
Michael Judge
I'm working on implementing mini-scripting languages for two different
projects, so I'm building a framework that could handle the task
generically.
Does this seem like a good way to approach it?
1. Store each command's matching regular expression and ruby code
within the database. (sample fixture below)
2. For each line in script:
Test line against each command's corresponding regular
expression
If matched, execute the command's ruby code using an
instance_eval.
My thoughts are:
1. Storing executable code in the database is a security problem
2. instance_eval is slow
3. The alternative (a big if/elsif tree) would span many pages and
be unweildy.
Have a better suggestion?
Sample code:
def compile
syntax.each do |line|
command = commands.find { |c| c.match? line }
raise "Command not found that can process '#{line}'" if command.nil?
instance_eval command.ruby
end
end
Sample commands fixture:
label:
id: 1
name: label
regexp: ^Q (.*)$
ruby: puts "$1\n"
single-punch:
id: 2
name: single-punch
regexp: ^X-(\d+) (.*)$
ruby: puts " o $2\n"
multiple-punch:
id: 3
name: multiple-punch
regexp: ^M-(\d+) (.*)$
ruby: puts " [ ] $2\n"
blank-line:
id: 4
name: blank-line
regexp: ^\s*$
ruby: # Do nothing
projects, so I'm building a framework that could handle the task
generically.
Does this seem like a good way to approach it?
1. Store each command's matching regular expression and ruby code
within the database. (sample fixture below)
2. For each line in script:
Test line against each command's corresponding regular
expression
If matched, execute the command's ruby code using an
instance_eval.
My thoughts are:
1. Storing executable code in the database is a security problem
2. instance_eval is slow
3. The alternative (a big if/elsif tree) would span many pages and
be unweildy.
Have a better suggestion?
Sample code:
def compile
syntax.each do |line|
command = commands.find { |c| c.match? line }
raise "Command not found that can process '#{line}'" if command.nil?
instance_eval command.ruby
end
end
Sample commands fixture:
label:
id: 1
name: label
regexp: ^Q (.*)$
ruby: puts "$1\n"
single-punch:
id: 2
name: single-punch
regexp: ^X-(\d+) (.*)$
ruby: puts " o $2\n"
multiple-punch:
id: 3
name: multiple-punch
regexp: ^M-(\d+) (.*)$
ruby: puts " [ ] $2\n"
blank-line:
id: 4
name: blank-line
regexp: ^\s*$
ruby: # Do nothing