> class AORAdapter < Array
oops, this should've just been:
class AORAdapter
complete (revised) source below:
require 'ostruct'
def extract_attributes(rec)
if rec.kind_of?(OpenStruct)
attributes = rec.marshal_dump.keys
else
attributes = rec.public_methods
attributes.delete_if do |a|
!(a =~ /^[a-z_]*$/i && attributes.include?("#{a}="))
end
end
attributes
end
def aor_to_roa(arr)
attributes = extract_attributes(arr.first)
roa = arr.first.class.new
attributes.each { |a| roa.send("#{a}=", []) }
arr.each do |rec|
attributes.each do |a|
roa.send(a).push(rec.send(a))
end
end
roa
end
def roa_to_aor(roa)
attributes = extract_attributes(roa)
arr = []
roa.send(attributes.first).size.times do |i|
arr[i] = roa.class.new
attributes.each do |a|
arr[i].send("#{a}=", roa.send(a)[i])
end
end
arr
end
class ROAAdapter
def initialize(arr)
@arr = arr
end
def method_missing(a)
arr = @arr
Class.new do
define_method(:[]) { |i| arr[i].send(a) }
end.new
end
end
class AORAdapter
def initialize(roa)
@roa = roa
end
def [](i)
roa = @roa
Class.new do
define_method(:method_missing) { |a| roa.send(a)[i] }
end.new
end
end