Sha256: a40cca3a5078c0e4dc0da143d07f6a02bab661117a1b63659b57bed9db85aea6

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

module ActiveRecordFake

  def self.included(base)
    base.instance_variable_set(:@items, [])
    base.instance_variable_set(:@next_id, 1)    
    base.extend(ClassMethods)
  end


  attr_accessor :id

  def initialize options={}
    @id = options.fetch(:id) { self.class.get_next_id }
    set_attribute_values_from_options options.reject{|key, value| key == :id }
    self.class.add self
  end

  def delete!
    self.class.remove(self)
  end

  def update options={}
    set_attribute_values_from_options options
  end

  module ClassMethods
    def create *args
      new *args
    end
    
    def delete_all
      @items.clear
      reset_id
      true
    end

    def all
      @items.dup
    end
    
    
    def add instance
      @items << instance
    end

    def remove instance
      @items.delete(instance)
    end

    def get_next_id
      id = @next_id
      @next_id += 1
      id
    end

    def reset_id
      @next_id = 1
    end
  end

  private

    def set_attribute_values_from_options options
      options.each{|attr, value| self.send("#{attr}=".to_sym, value) }
    end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
clockwork-1.3.1 test/database_events/support/active_record_fake.rb
clockwork-1.3.0 test/database_events/support/active_record_fake.rb
clockwork-1.2.1 test/database_events/support/active_record_fake.rb
clockwork-1.2.0 test/database_events/support/active_record_fake.rb
clockwork-1.1.0 test/database_events/support/active_record_fake.rb
clockwork-1.0.2 test/database_events/support/active_record_fake.rb