Storing Ruby in a YAML file [closed] - ruby

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've got an idea, but it's implications scare me. Perhaps you, dear reader, can help. :)
The Setup
I've created a Ruby-based CLI app that allows user configuration via a YAML file. In that file, there is scenario where the user can define pre and post "actions" that display a message (with some arbitrary, non-relevant code in-between). For example:
actions:
- action
# ...other keys...
pre:
message: 'This is the pre message'
action: puts 'PRE COMMAND'
post:
message: 'This is the post message'
action: puts 'POST COMMAND'
In this case, my app would output This is the pre message, evaluate the "pre" action (thus outputting PRE COMMAND), do some irrelevant stuff, output This is the post message, and finally evaluate the "post" action (thus outputting POST COMMAND).
The Problem
You can already guess the problem; it appeared when I used the word "evaluate". That's a scary thing. Even though this is a locally-run, client-centric app, the idea of eval'ing random Ruby is terrifying.
Solution Idea #1
The first idea was just that: eval the actions. I quickly destroyed it (unless one of you knows-more-Ruby-than-me types can convince me otherwise).
Solution Idea #2
Do some "checking" (via Regexp, perhaps) to validate that the command is somehow "valid". That seems wildly large and difficult to contain.
Solution Idea #3
Another idea was to wrap acceptable commands in data structures of my own (thus limiting the possibilities that a user could define). For instance, I might create an open_url action that safely validates and opens a URL in the default browser.
I like this idea, but it seems rather limiting; I'd have to define a zillion wrappers over time, it seems like. But perhaps that's the price you pay for safety?
Your Turn
I appreciate any additional thoughts you have!

You'd probably be a lot better off writing a simple framework that allows for Ruby plugins than to glue together something out of YAML and snippets of code.
You're right that "eval" is terrifying, and it should be, but sometimes it's the most elegant solution out of all possible inelegant solutions. I'd argue that this time is not one of those cases.
It's not at all hard to write a very simple DSL in Ruby where you can express your configuration in code:
action.pre.message = 'This is the pre message'
action.pre.command do
puts "PRE COMMAND"
end
All this depends on is having a number of pre-defined structures that have methods like message= taking a string as an argument or command taking a block. If you want to get fancy you can write some method_missing handlers and make up things as you go along, allowing for maximum flexibility.
You can see many examples of this, from your Rakefile to capistrano, and it usually works out a lot better than having a non-Ruby configuration file format with Ruby in it.

Related

Creating a new snip% with Racket

I am trying to create a new GUI element within DrRacket's text window, like picts or syntax objects. As far as I can tell, the most standard way of doing this is with a snip%.1
Unfortunately, the documentation for creating new snips, while comprehensive, is a bit impenetrable and leaves some questions to be answered.
For starters, what is the difference between a snip% and a snip-class%? Why do these need to be separated out into two classes, rather than simply being combined into one class? Is it because multiple snips will use one snip class?
Second off, what is snip-reader<%>? Not only why does it need to be a separate class, but why is the module providing it supposed to be installed?2 If it does need to be a new class, why can't it just be referred to directly. Why go through this whole process of constructing and then parsing a string of the form: "(lib ...)\n(lib ...)"?
I mean, there might now be any reason for this design, and it might just be a remnant of an old API. If so, has anyone thought of making a new more consistent API? Or if there is a reason for this design, can you please tell me what it is, as the docs don't seem to make that clear.
I mean, as of right now, I can copy/paste the sample given in the docs on creating a new snip. But I'm having a hard time understanding the design going on here, so I can use them properly.
1I know there are other ways to do it, but I also want to have interactive buttons and whatnot.
2I know it doesn't need to be installed as a library per se, but the documentation seems to strongly push you in that direction.
Okay, I think I finally found the answer. Broadly speaking:
The snip% class includes the methods for drawing the snip, telling the editor how much space to reserve for the picture, and handling events such as mouse clicks.
Next, the snip-class% class is used for encoding and decoding snips. This must be a separate class because when saved to a file, the editor needs to encode what type of snip it is, and for obvious reasons it can't just put the literal snip% class in there. The value it stores in the file is the snip-class%'s 'class name'. This can be anything, and as long as the editor has the classname associated to a snip-class%, it can be loaded. Additionally, if it is of the form "(lib ...)" or "(lib ...) (lib ...)" Racket will just automatically load it into the list for you.
Nothing 'needs' to be installed per se, its just the easiest way to go about it. Otherwise you manually need to tell the editor how to handle the snip before actually loading the file.

Rails #raw or #html_safe methods are truncating text. Is there anyway around this? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am new to Rails and am trying to put together a little app that will read cucumber results from a mongo database. The results stored in the document are parsed into html. In the rails app I am taking those results and displaying them using the raw() method call. The string that I get back is fairly large and as it turns out, the raw() method is truncating the text that I pass into it. When I output the text without raw() I get the entire string as expected (except that it has been escaped and not rendering as html).
My question is, is there any way to get around this? I really don't want to have to do the html conversion in the rails app or on the client. Both seem too costly. Especially when I can do it elsewhere and just store it in monogdb as an html string. Anyone have any ideas?
Thanks,
Jake
It turns out that there was a part of the string that was causing the rendering of the html to choke. Because cucumber syntax pass variables to Scenario steps using < >, there were places that <style> was written. Because is a valid open html tag, the html stopped rendering. I found this out by looking at the page source (where I was using the inspect element on the developer tools before). I saw that the whole html that I was expecting was in the source. I parsed through the text and used gsub to replace the <style> tag and all is working now.

Is there a best practice to documenting a Command Line Interface? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have designed a few programs that have a CLI and want to document them as standard as possible. Are there any agreements out there as to the best way to do this?
An example:
Let's say the Program is "sayHello" and it takes in a few parameters: name and message. So a standard call would look like this:
> sayHello "Bob" "You look great"
Okay, so my command usage would look something like this:
sayHello [name] [message]
That may already be a mistake if brackets have a specific meaning in usage commands. But let's go a step farther and say "message" is optional:
sayHello [name] [message (optional)]
And then just one more wrinkle, what if there is a default we want to denote:
sayHello [name] [message (optional: default 'you look good')]
I realize this usage statement looks a little obtuse at this point. I'm really asking if there are somewhat agreed-upon standards on how to write these. I have a sneaking suspicion that the parenthesis and brackets all have specific meanings.
While I am unaware of any official standard, there are some efforts to provide conventions-by-framework. Docopt is one such framework, and may suit your needs here. In their own words:
docopt helps you:
define interface for your command-line app, and
automatically generate parser for it.
There are implementations for many programming languages, including shell.
You might want to look at the manuals for common Unix commands (e.g. man grep) or the help documentation for Windows commands (e.g. find /?) and using them as a general guide. If you picked either of those patterns (or used some elements common to both), you'd at least surprise the fewest number of people.
Apache commons also has some classes in the commons-cli package that will print usage information for your particular set of command-line options.
Options options = new Options();
options.addOption(OptionBuilder.withLongOpt("file")
.withDescription("The file to be processed")
.hasArg()
.withArgName("FILE")
.isRequired()
.create('f'));
options.addOption(OptionBuilder.withLongOpt("version")
.withDescription("Print the version of the application")
.create('v'));
options.addOption(OptionBuilder.withLongOpt("help").create('h'));
String header = "Do something useful with an input file\n\n";
String footer = "\nPlease report issues at http://example.com/issues";
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("myapp", header, options, footer, true);
Using the above will generate help output that looks like:
usage: myapp -f [-h] [-v]
Do something useful with an input file
-f,--file <FILE> The file to be processed
-h,--help
-v,--version Print the version of the application
Please report issues at http://example.com/issues

How to generate a "canned response" with several variables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm brand new to Ruby and programming. I'd like to create a little program to automate one of my more tedious work tasks that I'm currently doing by hand but I'm not sure where to start.
People register to take courses through an online form, and I receive their registration information at the end of each day as a CSV document. I go line by line through that document and generate a confirmation email to send to them based on their input on the online form: the course they'd like to take, their room preference, how much they chose to pay for the course (sliding scale), etc. The email ends up looking something like this:
Dear So and so, Thank you for signing up for "Such-and-such An Awesome Course," with Professor Superdude. The course starts on Monday, September 1, 2030 at 4pm and ends on Thursday at 1pm. You paid such-and-such an amount...
et cetera. So ideally the program would take in the CSV document with information like "student name," "course title," "fee paid," and generate emails based on blocks of text ("Dear , Thank you for signing up for _,") and variables (the dates of the course) that are stored externally so they are easy to edit without going into the source code (maybe as CSV and plain text files).
Additionally, I need the output to be in rich text, so I can bold and underline certain things. I'm familiar with Markdown so I could use that in the source code but it would be ideal if the output could be rich text.
I'm not expecting anyone to write a program for me, but if you could let me know what I should look into or even what I should Google, that would be very helpful.
I assume you're trying to put together an email. If so, I'd probably start with a simple ERB template. If you want to generate HTML, you can write one HTML template and one plain text template; variable substitution works the same way for both, with the exception that you'll need to html-escape anything that contains characters that HTML considers special (ampersands, greater than, less then, for example). See ERB Documentation here.
If you're trying to parse CSV, user FasterCSV or a similar library. FasterCSV is documented here.
If you want to send an email, you can use ActionMailer, the mail gem, or the pony gem. ActionMailer is part of rails, but can be used independently. Pony is a good facade for creating email, as well; both ActionMailer and Pony depend on the "mail" gem, so unless you want to spend more time thinking about how email formats work, use one of those.
If you're not trying to send an email, and instead are trying to create a formatted document, you can still use ERB, but use it to generate output in TeX, or if you're more adventurous than I am, a Word compatible XML document. Alternatively, if you're wedded to Microsoft Word or RTF, you might try either http://ruby-rtf.rubyforge.org/ (Ruby RTF) or use COM/OLE interop to talk to Word, but I would only do that if really I had to; if I had to go that route, I'd probably suck it up and just use the built in mail merge feature in Word perhaps with a little VBA code.

Abstract testing of GUIs

In general how does one test a various parts of a GUI? What are good practices? (Yes I am being overly general here).
Let take for Notepad's Find dialog box:
Notepad's Find dialog box http://img697.imageshack.us/img697/5483/imgp.png
What are some things that can be tested? How does one know its working correctly? What are edge cases to look out for? Stress tests?
Here.
I doubt any good generalization can be made about this - it always depends on the situation.
When someone asks for tests for GUI I always assume that that mean 'this part of application that is accessible via this GUI'. Otherwise it would mean testing the only the GUI without any logic hooked. Dunno why no one never actually asked for testing if the events are fired when button is pressed or is displayed window acquiring focus.
Anyway back to the question. First of all find out about equivalence classes, boundary conditions other testing techniques. Than try to apply it for given problem. Than try to be creative.
All those should be applied when creating following tests:
1) happy path tests - application acts right when given input is good
2) negative tests - application acts right when given input is bad
3) psychotic user behavior (I saw someone use this term, and I find it to be great) - that one user that has nothing better to do than break your application or is to stupid to actually know how bad and horrible things he is doing with your app.
After all this if all tests are passing and you can't figure out other, than you don't know is it working properly, but you can say that it passed all tests and it seems to be working correctly.
As for given GUI example.
1)
Is the application finding string that is in opened file?
Is the application finding character that is in opened file?
How is it reacting to reaching end of file during search?
Is it finding other appearances of given string/character or just one, when there are many of those appearances ?
Is it handling special search characters like * or ? correctly?
Is it searching in desired direction?
Is it 'Mach case ' option working properly?
When opening find setting some criteria, canceling search and launching it again - are search criteria back to default values? Or are they set as you left them when clicking Cancel?
2)
Is it informing user that no mach was found when trying to search for data that is not in opened file?
Is it reacting properly when trying to search down form end of file?
Is it reacting properly when trying to search up form beginning of file?
How search feature is reacting when no file is loaded? (in MS notepad it can be done, but in other editors you can launch editor without opening a file hence this test)
Can I mark both Up and Downs search direction?
3)
Is it working properly on 4GB file?
Can I load 4 GB string in 'Find What:' field and search for it?
Can I provide as input special characters by providing ASCII codes? (it was done like pressing Alt and number of character... or something like that)
Can I search for empty character (there was something like that in character table).
Can I search for characters like end of line or CarretReturn?
Will it search for characters form different languages? (Chinese, or other non-english alphabet characters)
Can I inject something like ') DROP ALL TABLES; (if that would be web based search).
Will I be able to launch proper event twice by really fast double click on search button? (easier on web apps)
With reasonable test suite you know it seems to work correctly.
I think it is better to separate out functional aspects and the usability aspects for the GUI testing.
Let us say in the above example take the use case of user entering some text and hitting the Find button. From the functional aspect I would say your tests should check whether this user action (event) calls the appropriate event handler methods. These can be automated if your code has good separation between the GUI display code and the
functional part.
Testing of usability aspect would involve checking things like whether the display occurs correctly in multiple platforms. I think this needs to be verified manually. But I think there are some tools that automate this kind of GUI testing as well but I've no experience with them.
It's difficult and error-prone to test finished UIs.
But if you are more interested form the programmer's perspective, please have a read of the paper The Humble Dialog. It presents an architecture for creating UIs whose functionality can be tested in code using standard testing frameworks.

Resources