Loading Binary Data into Rails Fixtures

Posted by Peter Donald Wed, 05 Apr 2006 21:50:00 GMT

Loading image data into fixtures was a chore until recently as I had been using separate rake tasks to do the job. The following code demonstrates how easy it is to load binary data such as images into the fixtures via standard mechanisms. It loads some image data from within the $RAILS_ROOT/test/fixtures/ directory and puts it in database.

<%
def fixture_data(name)
  render_binary("#{RAILS_ROOT}/test/fixtures/#{name}")
end

def render_binary(filename)
  data = File.open(filename,'rb').read
  "!binary | #{[data].pack('m').gsub(/\n/,"\n    ")}\n"
end
%>
picture_data_1:
  id: 1
  picture_id: 1
  content_type: 'image/jpg'
  data: <%= fixture_data("picture_data_1.jpg") %>
picture_data_2:
  id: 2
  picture_id: 2
  content_type: 'image/gif'
  data: <%= fixture_data("picture_data_2.gif") %>

If you do not use two spaces as your indent then you will need to alter the line in render_binary(filename) that replaces newline so that every newline is replaced with two indents.

Easy peasy!

Update on 16th April 2006

It turns out that it was not as easy peasy under postgres as the driver did not know it had to escape the data as binary as fixtures don’t actually load the column type. The simplest hack around it is to add in the following bit of code somewhere that just patches the driver if a 0 is in the data. This may not always work but it works with my test data so that is good enough for me at the moment.

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
  def quote(value, column = nil)
    if (value.kind_of?(String) && column && column.type == :binary) || (value.kind_of?(String) && value.include?(0))
      "'#{escape_bytea(value)}'"
    else
      super
    end
  end
end

Posted in  | Tags , , ,  | 5 comments | 1 trackback

Rails Plugin to Help Debug Views

Posted by Peter Donald Mon, 20 Mar 2006 03:02:00 GMT

The debug_view_helper plugin was developed to make it easy to add debug information into your views. It was derived from techniques described in HowtoDebugViews and it makes it possible to expose the followng following debug data;

  • Request Parameters
  • Session Variables
  • Flash Variables
  • Assigned Template Variables

Typically you add code such as the following to the bottom of your layout that exposes the debug button in development mode.

<% if RAILS_ENV == 'development' %>
<center>
  <button onclick="show_debug_popup(); return false;">
    Show debug popup
  </button>
</center>
<%= debug_popup %>
<% end %>

You can grab the plugin from subversion at;

http://www.realityforge.org/svn/code/debug-view-helper/trunk/

Update: Added the ability to add inline debug information via the following. Suggestion by John Dell.

<% if RAILS_ENV == 'development' %>
<%= debug_inline %>
<% end %>

Posted in  | Tags ,  | 2 comments | no trackbacks

Rails Plugin to Validate (X)HTML and CSS

Posted by Peter Donald Wed, 15 Mar 2006 08:18:00 GMT

Here is an enahnced version of Scott Raymond’s assert_valid_markup plugin that I use in my projects. Below are the directions for use;

HowTo Validate (X)HTML

# Calling the assertion with no parameters validates 
# whatever is in @request.body, which is automatically 
# set by the existing get/post/etc helpers. For example:

class FooControllerTest < Test::Unit::TestCase
  def test_bar_markup
    get :bar
    assert_valid_markup
  end
end

# Add a string parameter to the assertion to validate 
# any random fragment. For example:

class FooControllerTest < Test::Unit::TestCase
  def test_bar_markup
    assert_valid_markup "<div>Hello, world.</div>"
  end
end

# For the ultimate in convenience, use the class-level
# method to validate a number of actions in one line. 

class FooControllerTest < Test::Unit::TestCase
  assert_valid_markup :bar, :baz, :qux
end

HowTo Validate CSS

# Pass a string parameter to the assertion to validate 
# a css fragment. For example:

class FooControllerTest < Test::Unit::TestCase
  def test_bar_css
    filename = "#{RAILS_ROOT}/public/stylesheets/bar.css"
    assert_valid_css(File.open(filename ,'rb').read)
  end
end

# For the ultimate in convenience, use the class-level
# method to validate a bunch of css files in one line.
# Assumes that the CSS files are relative to 
# $RAILS_ROOT/public/stylesheets/ and end with '.css'. 
# The following example validates 
#  $RAILS_ROOT/public/stylesheets/layout.css, 
#  $RAILS_ROOT/public/stylesheets/standard.css and 
#  $RAILS_ROOT/public/stylesheets/theme.css

class FooControllerTest < Test::Unit::TestCase
  assert_valid_css_files 'layout', 'standard', 'theme'
end

Most of the credit for this plugin goes to Scott for the initial idea! The modifications that I made include;

  • Validation of CSS files.
  • Caching of fragments occurs in $RAILS_ROOT/tmp according to the name of the test class + test method. This avoids filling up the system temp folder with expired cache files.
  • Ability to turn off validation by setting the “NONET” environment variable to “true”.

You can grab it from subversion at;

http://assert-valid-asset.googlecode.com/svn/trunk/

Update 12th of May, 2008

Steve Sloan has also added a Git repo for the code which you can grab from:

http://github.com/CodeMonkeySteve/assert-valid-asset/tree/master

Posted in  | Tags , , , ,  | 2 comments | no trackbacks

Meta-Programming in Java

Posted by Peter Donald Thu, 09 Mar 2006 05:33:00 GMT

I have been looking over my old code repository and I came across GEL. GEL was a toolkit that allowed me to define entities and events for HLA using an XML data definition language.

The HLA is a family of specifications developed to promote intereoperability and reuse of simulation assets. Unfortunately there is a significant development effort associated with construction of a HLA-compliant federation. Less than 2% of the 2500+ lines of code in the “Hello World” sample HLA application relates to simulation logic. The integration code is a significant cost and there is often a high-level of coupling between the simulation code and the integration code.

GEL was created to simplify HLA developers task by generating all the integration code from a simulation model defined in an XML document. The HLA developer had the sole responsibility of writing the simulation/logic code. Think of GEL as a a poor-mans MDA (I even planned to have a set of separate generators that would allow the simulation objects to be hosted in a Quake3 game). This approach rocks as you only need to fix and update the code in one place (the generator) and you are far more productive at the higher level of abstraction.

Each simulation object in the GEL XML document resulted in the generation of several Java classes by velocity.

  • a Model class for holding data of a specific instance.
  • a ModelListener class that receives events relating to a Model instance.
  • a HLAPeer class that reflects the data in a Model instance into the HLA runtime.
  • a PersistPeer class that persists the data in a Model instance.
  • a number of Value Object classes to represent various events
  • etc…

I had long believed in having a single high-level model of the domain objects and generating. Prior to XML I had used SGML, INI and custom formats to represent the model. On a number of occasions I have attempted to move the model data into the code to make it easier to keep up to date. I have used C/C++’s macro expansions, XDoclet-like processors and Java 1.5 Annotations at various times.

Six months using Ruby On Rails’ has changed my world view. The model classes are very close to the level at which I would create my domain model. The good thing about ruby is that you can always add more language constructs to make sure the code matches your domain model. (I know Lisp-ers have always raved about this but I have never been able to use lisp in a commercial environment). Things like acts_as_list to order your domain objects, acts_as_versioned to version your domain objects or acts_as_threaded to have a threaded tree representation radically simplify the representation of domain objects.

I have been thinking about how I would do this in Java but most of the neat stuff just does not seem possible without the introduction of a Meta-object Protocol or some sort of load-time class file modification.

Consider a simple simulation object. In ruby I would represent it via

class Kettle < Model::Base
  shared_attr :temperature, :float, :resolution => 1.0, :accuracy => 1.0 
end

In java there is no simple equivalent. I could just define the model in a class like below. However this would probably need to be instrumented during loading so that the runtime could monitor changes to the shared attribute.

public class Kettle extends BaseModel
{
  @SharedAttribute(resolution = 1.0, accuracy = 1.0 )
  private float m_temperature;

  public float getTemperature()
  {
    return m_temperature;
  }

  public void setTemperature(final float temperature)
  {
    m_temperature = temperature;
  }
}

Instrumentation in whatever form it takes is fairly complex even if you use something like AspectJ that is perfect for this scenario. Much more complex than writing the equivalent acts_as_* plugin.

The above only works when the instrumentation code does not need to add any methods. However if you want a simulation object to support shared events you need to be able to subscribe, unsubscribe and generate these events which means you need the same boilerplate code repeated over and over.

I can not think of anyway to do this in java so I guess it is back to generating the Java classes from a central model file. Pity I think I liked the ruby way better.

Posted in  | Tags , , , , ,  | 2 comments | no trackbacks

Distributed Case-Based Reasoning using Java

Posted by Peter Donald Wed, 08 Mar 2006 02:05:00 GMT

Over the next few months I will be investing some time into a research project focusing on distributed case-base reasoning. The idea is to establish an ad-hoc network of nodes that each have a separate case-base. When a node attempts to find a solution to a problem it will query both it’s local case-base and case-bases of connected nodes to determine the solution.

The exact algorithms that the solver uses to select nodes to query will have a significant impact on the speed and accuracy with which a solution can be found. The maintainece of the distributed cases-bases via case addition or removal will also need to be addressed in our research.

I will be working on an existing codebase but I have been looking around to see if I can find any free or opensource java case-base reasoning software. Something that I can tack a distribution layer on top of but I have been unable to find any products that are not commercial software and/or under highly restrictive licenses. I wonder if this is an area that just does not appeal to FOSS developers?

Posted in  | Tags , , , , ,  | no comments | no trackbacks

Older posts: 1 2 3 4 ... 6