Elegant way to code generated coffeescript in ruby? - ruby

I have the following which converts a coffeescript when ...../coffee/xxxx.js is called. For example I am trying to load variables which compliments the script to be loaded. should I load it in a separate js file or any elegant way of injecting it as coffee script as well?
get "/coffee/*.js" do
filename = params[:splat].first
coffee "../public/coffee/#{filename}".to_sym
end

Related

Cucumber, how to write this line

I did some selenium-webdriver ruby code. It works, but now I want to use cucumber to take advantage of the html report that looks good with green and red colors. When I started to use some code in the file called custom_steps.rb, it did not work.
Can you please tell me how to write this line in this file:
require 'selenium-cucumber'
# Do Not Remove This File
# Add your custom steps here
# $driver is instance of webdriver use this instance to write your custom code
divs = $driver.find_elements(:xpath,"//div[#class='col-xs-12 check-box-container']/a/div");
How is the code from custom_steps.rb called in .feature file?
It is not called by that file, your step definition interprets meaning from the .feature file.
In your step definition file you need to write regular expressions to match the lines in the .feature file.
Here is an example of the syntax in action from dispora, a large rails app with many cucumber tests https://github.com/diaspora/diaspora/blob/21980681b156bc163c616cf0344bcdf3f3a195db/features/step_definitions/search_steps.rb.
Also see https://github.com/cucumber/cucumber-ruby

Get the source code of a ruby file in the form of a string/text

Is there any simple way to get all the source code of a ruby file in the form of a string? I'm looking for something that would behave like inspect.sourcelines(codeObj) in python.
I'm surprised this hasn't been asked before... But I couldn't find the question on SO anywhere...
myCode = File.read(__FILE__)
That should do the trick.
__FILE__ is a special variable which contains the full file path of the currently executing file. Depending how you script is being executed you might be better off using $0 instead. But this really depends on what you really want.

Is it possible to require a Ruby file in an ERB template?

I know that you can embed Ruby code in an ERB template. But I have this massive file that has over 200 lines of code. It wouldn't make sense to place every line into the ERB file as then the page would be too big.
I think a better solution would be to require a Ruby file in ERB. Is this possible? If so, how do I do it?
My code itself has a lot of methods defined and I'm not sure they can be added to a view file anyways.
The program is a game that will be played on the home page.

Should I use load or require in IRB?

I had the impression that I should use require 'some_path' to get a file or a library I want to use, but I couldn't get it to work, and load 'some_path' worked.
When are the different times when I should use these?
Also, is it possible to import a directory with the load or require commmands? I am trying to use a whole directory, but so far I could only get one file at a time with load.
You can use Dir to list all the files ending with .rb and require/load them
Dir["/path/to/dir/*.rb"].each { |file| load_your_file_here(file) }
I recommend requiring file and then including the module that file loads... If you are not using module or class inside your file than maybe you should reconsider your structure.
load might have some unintended consequences and it's not performant.
Once you call require for a file further calls of require will no longer require it again(i.e. will have no effect), while load will reload it every time you call it. As far as I know there is no way to load a whole directory.

How do I extract or introspect the literal text code of a Ruby function or block?

I'm trying to integrate working with CouchDB into the Camping web framework. Previously I was using S Expressions to parse simple Ruby blocks into JavaScript, and just writing inline JavaScript in strings when I needed something more complex.
Then I discovered the View Server, and I want to move to using Ruby for the Map and Reduce functions.
At the moment, I can do something like this:
view = CouchDB::View.new :myview do
map do |doc|
emit(doc._id, doc.price) if doc.kind == "Product"
end
reduce do |values|
return sum(values)
end
end
I'd like to be able to have a similar syntax for that, but for the actual text of those blocks (or methods, or whatever) to be something I can directly read and push to the database that stores the views. Is this possible to do in a way that's not really evil, like just putting them in a string, or reading the file and parsing it somehow for the text of the views?
You could use something like ruby2ruby to parse the contents of the block.
It will give you the tokenized version of the block, but not the literal text as the markup is discarded when the source is compiled into bytecode.

Resources