Class: ActiveMocker::Mock::Records

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/active_mocker/mock/records.rb

Instance Method Summary collapse

Constructor Details

#initialize(records = []) ⇒ Records

Returns a new instance of Records.



10
11
12
13
# File 'lib/active_mocker/mock/records.rb', line 10

def initialize(records = [])
  @records      ||= records
  @record_index ||= {}
end

Instance Method Details

#delete(record) ⇒ Object

Raises:



22
23
24
25
26
27
# File 'lib/active_mocker/mock/records.rb', line 22

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

#exists?(record) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_mocker/mock/records.rb', line 29

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

#insert(record) ⇒ Object



15
16
17
18
19
20
# File 'lib/active_mocker/mock/records.rb', line 15

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

#new_record?(record) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/active_mocker/mock/records.rb', line 33

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

#persisted?(id) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/active_mocker/mock/records.rb', line 37

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

#resetObject



41
42
43
44
# File 'lib/active_mocker/mock/records.rb', line 41

def reset
  record_index.clear
  records.clear
end