malkomalko
3/21/2009 - 12:50 AM

gistfile1.rb

Hardcoded in for testing, ugly first implementation

class RedisModel
  DB = Redis.new
  attr_accessor :id, :created_at, :updated_at
  
  def initialize(*args)

  end
  
  def self.all
    arr   = Array.new
    (1..50).each do |user_id|
      begin
        hash  = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
        hash['user']['id'] = user_id.to_s
        User::ATTRIBUTES.each do |field|
          hash['user'][field] = DB['user:'+user_id.to_s+':'+field]
        end
        arr << hash
      rescue NoMethodError => e
        nil
      end
    end
    arr
  end
  
  def self.find(id, *args)
    # @db = Redis.new
    # @key = "user:#{id}:"
    # 
    # @user = self.new
    # @user.id = id
    # 
    # ATTRIBUTES.each do |attribute|
    #   eval <<-EVAL
    #     @user.#{attribute} = @db[@key+"#{attribute}"]
    #   EVAL
    # end
    # 
    # p @user.to_json
  end
  
end

class User < RedisModel
  
  ATTRIBUTES = %w(first_name last_name phone email)
  ATTRIBUTES.each { |attribute| attr_accessor attribute.to_sym }
  
end

class UsersController < ApplicationController

  def index
    @users = User.all

    respond_to do |format|
      format.html
      format.json { render :json => @users }
    end
  end

....