Skip to content

Instantly share code, notes, and snippets.

@brendanjerwin
Created February 27, 2009 13:42
Show Gist options
  • Save brendanjerwin/71472 to your computer and use it in GitHub Desktop.
Save brendanjerwin/71472 to your computer and use it in GitHub Desktop.
A super-simple "ORM" (Object Row Mapper)
require 'simple_entity'
#Example Usage
obj = SimpleEntity.new("SELECT * FROM Payers WHERE PayerID = 'dg8gfb01-fgba-48ge-be5d-02d5dfg9hh7d'")
puts obj.PayerName + "\n\n"
puts obj.inspect #See all the columns
require 'rubygems'
require 'dbi'
class SimpleEntity
attr_accessor :hash
def initialize(sql)
dbh=DBI.connect("DBI:ODBC:BrendansDB","sa","PASSWORD")
row = dbh.select_one(sql)
@hash = Hash.new
row.each_with_name do |val, name|
@hash.store(name,val)
end
end
def method_missing(method_id, *arguments)
raise NoMethodError unless @hash.has_key?(method_id.to_s)
@hash[method_id.to_s]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment