Created
February 27, 2009 13:42
-
-
Save brendanjerwin/71472 to your computer and use it in GitHub Desktop.
A super-simple "ORM" (Object Row Mapper)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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