Sha256: df6e18ce173ef089b595717070ab26cc056a8c9b3bbbee9731cd45624746e967

Contents?: true

Size: 1.19 KB

Versions: 17

Compression:

Stored size: 1.19 KB

Contents

module ActiveMocker
module Mock
class Records

  extend Forwardable
  def_delegators :records, :<<, :count, :length, :to_a

  attr_reader :records, :record_index
  private     :records, :record_index
  def initialize(records = [])
    @records      ||= records
    @record_index ||= {}
  end

  def insert(record)
    record.attributes[:id] ||= next_id
    validate_unique_id(record)
    add_to_record_index(record)
    records << record
  end

  def delete(record)
    raise RecordNotFound, 'Record has not been created.' if new_record?(record)
    record_index.delete("#{record.id}")
    index = records.index(record)
    records.delete_at(index)
  end

  def exists?(record)
    records.include?(record)
  end

  def new_record?(record)
    !exists?(record)
  end

  def persisted?(id)
    records.map(&:id).include?(id)
  end

  def reset
    record_index.clear
    records.clear
  end

  private

  def next_id
    NextId.new(records).next
  end

  def add_to_record_index(record)
    record_index.merge!({record.id.to_s => records.length})
  end

  def validate_unique_id(record)
    raise IdError, "Duplicate ID found for record #{record.inspect}" if record_index.has_key?(record.id.to_s)
  end

end
end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
active_mocker-1.7.3 lib/active_mocker/mock/records.rb
active_mocker-1.7.2 lib/active_mocker/mock/records.rb
active_mocker-1.7.1 lib/active_mocker/mock/records.rb
active_mocker-1.7.1rc lib/active_mocker/mock/records.rb
active_mocker-1.7 lib/active_mocker/mock/records.rb
active_mocker-1.7rc2 lib/active_mocker/mock/records.rb
active_mocker-1.7.beta3 lib/active_mocker/mock/records.rb
active_mocker-1.7.beta2 lib/active_mocker/mock/records.rb
active_mocker-1.6.4 lib/active_mocker/mock/records.rb
active_mocker-1.7.beta1 lib/active_mocker/mock/records.rb
active_mocker-1.6.3 lib/active_mocker/mock/records.rb
active_mocker-1.6.2 lib/active_mocker/mock/records.rb
active_mocker-1.6.1 lib/active_mocker/mock/records.rb
active_mocker-1.6 lib/active_mocker/mock/records.rb
active_mocker-1.5.2 lib/active_mocker/mock/records.rb
active_mocker-1.5.1 lib/active_mocker/mock/records.rb
active_mocker-1.5 lib/active_mocker/mock/records.rb