Sha256: a68e548264b92f24480963934e39f93c7750c95436de3b253a21f52379fb0369

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

module ActiveMocker
  module Mock
    class Records

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

      attr_reader :records
      private     :records

      def initialize(records = [])
        @records = records
      end

      def insert(record)
        records << validate_unique_id((record.id ||= next_id), record)
      end

      def delete(record)
        raise RecordNotFound, 'Record has not been created.' unless records.delete(record)
      end

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

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

      def persisted?(id)
        ids.include?(id)
      end

      def reset
        records.clear
      end

      private

      def ids
        records.map(&:id)
      end

      def next_id
        max_record.succ
      rescue NoMethodError
        1
      end

      def max_record
        ids.max
      end

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

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
active_mocker-2.0.0.beta1 lib/active_mocker/mock/records.rb
active_mocker-1.8.4 lib/active_mocker/mock/records.rb
active_mocker-1.8.3 lib/active_mocker/mock/records.rb
active_mocker-1.8.2 lib/active_mocker/mock/records.rb
active_mocker-1.8.1 lib/active_mocker/mock/records.rb
active_mocker-1.8 lib/active_mocker/mock/records.rb