Sha256: 99c13f6f2e500760f7a0c8b83936a46616b11bae7b48e028198c5aee75a8dafe

Contents?: true

Size: 886 Bytes

Versions: 5

Compression:

Stored size: 886 Bytes

Contents

class Person < RedisBackedModel::RedisBackedModel
  attr_reader :id, :first_name, :last_name
  
  def self.all
    all = []
    ids.each do |id|
      all << self.find(id)
    end
    all
  end

  #def self.first
    #id = ids.first
    #self.find(id)
  #end
  
  def self.find(id)
    attr = $redis.hgetall("person:#{id}")
    self.new(attr.merge({:id => id}))
  end

  #def self.last
    #id = ids.last
    #self.find(id)
  #end

  def self.to_csv
    f = File.new("person.csv", "w+")
    f << "id,first_name,last_name\n"
    self.all.each do |x|
      f << "#{x.id},#{x.first_name},#{x.last_name}\n"
    end
  end

  # def initialize(attr)
  #   @id = attr[:id]
  #   @first_name = attr["first_name"]
  #   @last_name = attr["last_name"]
  # end

  def name
    "#{self.first_name} #{self.last_name}"
  end

  private

  def self.ids
    @ids ||= $redis.sort('person_ids')
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
redis_backed_model-0.0.5 spec/extras/person.rb
redis_backed_model-0.0.4 spec/extras/person.rb
redis_backed_model-0.0.3 spec/extras/person.rb
redis_backed_model-0.0.2 spec/extras/person.rb
redis_backed_model-0.0.1 spec/extras/person.rb