Semintelligent

Padrino - rspec - adding dynamic code to all controllers during testing · 5 February 2013, 13:34

We add in code that allows us to send mock parameters to controller actions via Capybara tests for all controllers while testing -so we can simulate session state ( for example - user being logged in ). In Rails you do this by re-opening ApplicationController in spec/spec_helper.rb and adding in a before_filter. In Padrino you can do this by adding custom code to app.rb in a before block - the before block is called for every controller action.
       configure :test do
          before do
            params.keys.each do |param|
              if param =~ /^mock_/
                mock_param = param.gsub(/mock_/, '') 
                session[ mock_param ] = params[ param ]
                logger.debug %{ #{mock_param} set to #{params[ param ]}} 
              end 
            end 
          end 
        end

— Max Schubert

Comment

---

Class variables in Rspec tests - reloads will happen during testing! · 5 February 2013, 13:20

As is the case when a ruby application runs in a multi-process web container (phusion passenger for example), during your rspec test runs, classes can be reloaded – meaning any class-level variables that are set cannot be counted on across tests, even within the same describe block.

We have a few classes that act as API wrappers for external services that we like to be able to have off by default in our test environment and on by default in production – we initially used class variables for these, but found that the state was getting reset across runs.

Fix: move the initial states into config/application.rb and config/environments/* and use those to initialize the cattr_accessor definitions at the top of the classes.

— Max Schubert

Comment

---

Easy to use ruby library for interacting with Confluence - confluence4r · 31 July 2009, 12:43

http://confluence.atlassian.com/display/CONFEXT/Confluence4r

I added a gemspec for the package to the bottom of the page if you want to build it as a gem in-house.

— Max Schubert

Comment

---

Getting ruby 1.8.7 and newer to compile with readline support on Red Hat Enterprise Linux (RHEL4 and RHEL5) · 4 April 2009, 06:20

Paraphrased from http://www.sanft.com/2008/12/01/upgrading-to-ruby-186-on-red-hat/

First, ensure you have the following packages installed:

Then make sure you remove the system ruby and ruby-devel packages, otherwise gems and other extensions might find the wrong version of ruby when they look for compile flags etc:

After unpacking the source for ruby, do the usual:

configure --prefix /usr/local
make all
sudo make install

Now do the following from the ruby source directory:

cd ext/readline
/usr/local/bin/ruby extconf.rb
make
make install

To ensure that ruby now has readline support, run

/usr/local/bin/ruby -rreadline -e 1

If you get no output (which should be the result), voila, readline support is now active.

— Max Schubert

Comment

---

Getting ruby gem mysql native extension to install on RHEL5 / CentOS 5 · 2 April 2009, 08:04

From

http://www.wzzrd.com/2008_02_01_archive.html

If you are on a 32-bit platform:

gem install mysql -- --with-mysql-conf=/usr/bin/mysql_config --with-mysql-lib=/usr/lib/mysql

If you are on a 64-bit platform:

gem install mysql -- --with-mysql-conf=/usr/bin/mysql_config --with-mysql-lib=/usr/lib64/mysql

— Max Schubert

Comment [1]

---

Which rubies do not work with rubygems 1.3.x? · 2 April 2009, 07:28

Anything later than ruby 1.8.7. Spent a few hours learning that lesson:

Thanks to my coworker, Ryan Richins, for pointing me to a version that does work!

— Max Schubert

Comment

---

Freezing Ruby Data Structures Recursively · 15 September 2008, 10:18

Flavorrific published a monkey patch that extends Object with a deep freeze method that freezes child arrays and hashes of a data structure so that they aren’t accidentally changed (for example, configuration data from YAML).

I have extended his work in the following ways:


class Object

# Define a deep_freeze method in Object (based on code posted by flavorrific # http://flavoriffic.blogspot.com/2008/08/freezing-deep-ruby-data-structures.html) # that will call freeze on the top-level object instance the method is # called on in as well as any child object instances contained in the # parent. This patch will also raise an IndexError if keys from # ‘deeply frozen’ Hashes or Arrays are accessed that do not exist.

def deep_freeze

# String doesn’t support each if (self.class != String) && (self.respond_to? :each) each { |v| v.deep_freeze if v.respond_to?(:deep_freeze) } end # Deep freeze instance variable values if self.kind_of? Object self.instance_variables.each { |v| iv = self.instance_variable_get(v) iv.deep_freeze self.instance_variable_set(v, iv) } end if self.kind_of? Hash instance_eval(<<EOF) def default(key) raise IndexError, “Frozen hash: key ‘\#{key}’ does not exist!” end EOF end # Prevent user from accessing array elements that do not exist. if self.kind_of? Array instance_eval(<<EOF) def at(index) self.fetch(index) end def [](arg1, arg2 = nil) results = Array.new if ! arg2.nil? # Start index and end index given arg1.upto(arg1 + arg2) { |index| results << self.fetch(index) } else if arg1.kind_of? Range # Range passed in arg1.each { |index| results << self.fetch(index) } else results << self.fetch(arg1) end end results end EOF end # Freeze the current structure freeze end

end

— Max Schubert

Comment

---