Sha256: 271894923fd75b85441bb077150d573a21c2b8824e51302e206b6fa0b52e9875

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

# Mock ActiveRecord::Base
class NotQuiteActiveRecord
  attr_accessor :name, :title, :other_method, :attributes

  def initialize(args = {})
    @attributes = args
    extract_attributes
    self.class.members << self
  end
  
  def extract_attributes
    attributes.each do |name, value|
      self.send("#{name}=", value.to_s) if self.respond_to?(name.to_sym)
    end
  end

  def save
    self.class.saved << self if new_record?
    true
  end

  def new_record?
    !self.class.saved.include? self
  end
  
  def self.where(hash)
    members.select{|m| hash.collect{|k, v| m.send(k) == v.to_s}.uniq == [true]}
  end
  
  def self.find_or_initialize_by(hash)
    if exists?(hash)
      where(hash).first
    else
      new(hash)
    end
  end

  def self.exists?(hash)
    !members.select{|m| m.send(hash.first[0]) == hash.first[1].to_s}.empty?
  end

  def self.create!(hash)
    new(hash).save
  end

  def self.members
    @members ||= []
  end

  def self.count
    members.length
  end

  def self.saved
    @saved ||= []
  end

  def self.clear_all
    @members = []
    @saved = []
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dibber-0.6.0 test/not_quite_active_record.rb
dibber-0.5.0 test/not_quite_active_record.rb
dibber-0.4.0 test/not_quite_active_record.rb