ruby

Are plugins available to a rake task?

When I run a rake task for an application that uses Models defined in a plugin I get an Uninitialized Constant error, but when I run the model process, with script/runner, that is fired in the rake task then the job runs fine? Is there some difference between script/runner that loads all my plugins that doesn't happen when I fire up a r...

Rails dashboard design: one controller action per div

I am implementing a dashboard as a relative Rails newbie (more of an infrastructure guy). The dashboard will consist of multiple pages, each page of which will contain multiple charts/tables/etc. For modularity, I want it to be as easy as possible to add new charts or change views of the data. Say one page has 5 different charts. I c...

How to test-drive software that uses external command-line tools

I'm trying to figure out how to test-drive software that launches external processes that take file paths as an input and write output after lengthy processing to stdout or some file? Is there some common patterns on writing tests in this kind of situations? It is hard to create fast executing tests that could verify correct usage of ext...

Variable field name in named_scope?

In a Rails model I am trying to acheive a named_scope that filters on a start_date and end_date. This is easy. But I am going to have to do it on lots of different fields on many occasions. Is this asking for trouble? If so why (SQL injection?) and is there another way to acheive this. named_scope :between, lambda {|start_date, end_da...

on my local Windows machine, how do i write a script to download a comic strip every day and email it to myself?

on my local Windows machine, how do i write a script to download a comic strip every day and email it to myself? such as http://comics.com/peanuts/ Update: i know how to download the image as a file. the hard part is how to email it from my local Windows machine. ...

Gems and Ubuntu 9.04

Hi, I recently upgraded to Ubuntu 9.04 and I have issues using gems. I have installed ruby, ruby gems and rails using apt-get The rails command do work. I have then installed capistrano and other gems (such as heroku). In order to do that, I have used the command sudo gem install XXX When I want to use the cap command it does not w...

Ruby path management

What is the best way to manage the require paths in a ruby program? Let me give a basic example, consider a structure like: \MyProgram \MyProgram\src\myclass.rb \MyProgram\test\mytest.rb If in my test i use require '../src/myclass' then I can only call the test from \MyProgram\test folder, but I want to be able to call it from any ...

How to unzip password protected file via Ruby

I'd like to unzip an encrypted/password protected file via a Ruby script without dropping down to a system call. I currently use the rubyzip library to unzip files but don't see an option for working with encrypted files. Anyone know of some code or a library that will do this? ...

Truncating Ruby Time objects efficiently

Hi, I am trying to come up with an efficient method for truncating Ruby Time objects according to a given resolution. class Time def truncate resolution t = to_a case resolution when :min t[0] = 0 when :hour t[0] = t[1] = 0 when :day t[0] = t[1] = 0 t[2] = 1 when :month t[0] ...

How to render different partials depending on current locale

I got index.erb and index-de.erb, and somewhere I saw an example where this resulted in index-de being rendered when I18n.locale was :de, but i just tried and it did not work and I could not find any documentation on this subject. So does this actually work or do I need an extra plugin for this ? ...

Amazon-ecs how to get the product's top level category?

I'm using the amazon-ecs gem to get product details from Amazon, using the itemlookup function (passing the ASIN). While I'm able to get all product details from this function, I'm not sure how to get the product's top level category - for example, if I have a children's book, I want the category as 'book' not fiction or children's book...

Use Ruby to permanently (ie, in the registry) set environment variables?

On Windows, how can I use Ruby to permanently set an environment variable? I know I need to change the registry (through the win32ole module?) but I am a novice with regard to scripting the registry. I understand that I can say ENV['FOO'] = "c:\bar\baz" to set the environment variable FOO for the session. However, I am instead interes...

Using a gem without installing it

I need to run a bunch of ruby scripts that I have written on a server that I don't have sudo access to. On my own machine, I have installed a bunch of gems using 'sudo gem install ..' and used them in my code.. Is there any mechanism which would let me use these gems without formally installing them on a remote machine? ...

What is the best way to create alias to attributes in Ruby?

What is the best way to create an alias to a instance atribute in Ruby (I'm not using rails or any ruby gem, just, Ruby). For example given the class below, how can I create an alias to the :student_name attribute accessors? class Student attr_accessor :student_name alias :name :student_name #wrong end s = Student.new s.student_...

What's the best option for a framework-agnostic Ruby background worker library?

I'm building a simple recipe search engine with Ruby and Sinatra for an iPhone app, using RabbitMQ for my message queue. I'm looking around and finding a lot of different implementation choices for background processes, but most of them either implement custom message queue algorithms or operate as Rails plugins. What's out there in te...

Testing for collection index-types (ie arguments to [] method)

What's an efficient, rubyesque way of testing if a collection supports string indexing? Long version: I'd like for a class of mine to normalize certain values. To achieve this, an instance takes a collection of values as its 'values' attribute. I'd like values= to accept both lists (integer indexed collections, including the built-in ...

How do I validate XML via RELAX NG in Ruby?

The REXML module appears to have support for RELAX NG validation, but the docs don't have any real information on using the validation portion of the framework. How would you validate an XML document with a RELAX NG schema? A code snippet would be most helpful. TIA! ...

Netbeans/Ruby - extraneous(?) autocomplete info

I'm getting a ton of (what I would call) extraneous autocomplete information in Ruby Netbeans 6.5.1. For example, if I type the name of a model object and then type a period (whether I'm in a controller or a view), it shows a dizzying dropdown list of thousands of methods, including several hundred different versions of the "new" method...

Mocha : How do you set up an expectation for an instance method?

Assume this ruby code: class User def self.failed_login!(email) user = User.find_by_email(email) if user user.failed_login_count = user.failed_login_count + 1 user.save end end end I want to write a test that tests that user.save is never called when an invalid email is given. E.g.: it "should not incremen...

bracket syntax for Ruby Hashes

Do these two statements pass the same type of argument (a Hash) to the new method? @seat = Seat.new(:flight_id => @flight.id) @seat = Seat.new({:flight_id => @flight.id}) Do the Hash brackets {} change anything in the second example? ...