Class: ActiveMocker::Mock::Records
- Inherits:
-
Object
- Object
- ActiveMocker::Mock::Records
- Extended by:
- Forwardable
- Defined in:
- lib/active_mocker/mock/records.rb
Instance Method Summary collapse
- #delete(record) ⇒ Object
- #exists?(record) ⇒ Boolean
-
#initialize(records = []) ⇒ Records
constructor
A new instance of Records.
- #insert(record) ⇒ Object
- #new_record?(record) ⇒ Boolean
- #persisted?(id) ⇒ Boolean
- #reset ⇒ Object
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
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
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
33 34 35 |
# File 'lib/active_mocker/mock/records.rb', line 33 def new_record?(record) !exists?(record) end |
#persisted?(id) ⇒ Boolean
37 38 39 |
# File 'lib/active_mocker/mock/records.rb', line 37 def persisted?(id) records.map(&:id).include?(id) end |
#reset ⇒ Object
41 42 43 44 |
# File 'lib/active_mocker/mock/records.rb', line 41 def reset record_index.clear records.clear end |