Storing Ruby in a YAML file -


i've got idea, it's implications scare me. perhaps you, dear reader, can help. :)

the setup

i've created ruby-based cli app allows user configuration via yaml file. in file, there scenario user can define pre , post "actions" display message (with arbitrary, non-relevant code in-between). example:

actions:   - action       # ...other keys...       pre:         message: 'this pre message'         action: puts 'pre command'       post:         message: 'this post message'         action: puts 'post command' 

in case, app output this pre message, evaluate "pre" action (thus outputting pre command), irrelevant stuff, output this post message, , evaluate "post" action (thus outputting post command).

the problem

you can guess problem; appeared when used word "evaluate". that's scary thing. though locally-run, client-centric app, idea of eval'ing random ruby terrifying.

solution idea #1

the first idea that: eval actions. destroyed (unless 1 of knows-more-ruby-than-me types can convince me otherwise).

solution idea #2

do "checking" (via regexp, perhaps) validate command somehow "valid". seems wildly large , difficult contain.

solution idea #3

another idea wrap acceptable commands in data structures of own (thus limiting possibilities user define). instance, might create open_url action safely validates , opens url in default browser.

i idea, seems rather limiting; i'd have define zillion wrappers on time, seems like. perhaps that's price pay safety?

your turn

i appreciate additional thoughts have!

you'd lot better off writing simple framework allows ruby plugins glue out of yaml , snippets of code.

you're right "eval" terrifying, , should be, it's elegant solution out of possible inelegant solutions. i'd argue time not 1 of cases.

it's not @ hard write simple dsl in ruby can express configuration in code:

action.pre.message = 'this pre message' action.pre.command   puts "pre command" end 

all depends on having number of pre-defined structures have methods message= taking string argument or command taking block. if want fancy can write method_missing handlers , make things go along, allowing maximum flexibility.

you can see many examples of this, rakefile capistrano, , works out lot better having non-ruby configuration file format ruby in it.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -