Binder for ironruby mvc

I’ve progressed far enough with IronRuby MVC to start thinking about a sample and I’ve decided to write 2 separate samples both on the same technologies. As an OR/M I will be using LightSpeed from Mindscape, just because it’s an awesome piece of technology.

At this moment I’m looking at creating a binder for objects that takes a hash as input. The default model binder of ASP.NET MVC doesn’t quite fit all that well with the ironruby implementation so I’m creating a ruby binder implementation. This stuff is just something that makes ruby shine.

While doing so I came up with this little tidbit of code:

class System::Object

  class << self

    def create_from_hash(options)
      result = self.new
      result.populate_from_hash options
      result
    end

  end

  def populate_from_hash(options)
    options.each do |k, v|
      mn = "#{k}=".to_sym
      self.send(mn, v) if self.respond_to?(mn)
    end
  end
end

This code allows you to populate any .NET object from a hash.

So doing Person.create_from_hash(:username => “joe”) would create a user instance with the property username set to joe.

You do need to provide a hash where the values have the correct type.

Technorati Tags: ,

  1. ASP.NET MVC Archived Blog Posts, Page 1 - pingback on April 13, 2009 at 06:14
  2. Have you looked at hashie [1] for creating objects from hashes? I don’t know that it actually works for this, but it looks like another good alternative.

    [1] http://github.com/intridea/hashie

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks and Pingbacks: