Module: ArtisanMemoryRepository::BaseRepository

Included in:
ChangeRepository, FutureUserRepository, IterationRepository, ProjectConfigurationRepository, ProjectRepository, StoryRepository, UserRepository
Defined in:
lib/artisan-memory-repository/base_repository.rb

Instance Method Summary collapse

Instance Method Details

#create(attrs = {}) ⇒ Object



9
10
11
12
# File 'lib/artisan-memory-repository/base_repository.rb', line 9

def create(attrs = {})
  project = model_class.new(attrs)
  self.save(project)
end

#delete(id) ⇒ Object



26
27
28
# File 'lib/artisan-memory-repository/base_repository.rb', line 26

def delete(id)
  records.delete(id)
end

#destroy_allObject



30
31
32
# File 'lib/artisan-memory-repository/base_repository.rb', line 30

def destroy_all
  @records = {}
end

#find(id) ⇒ Object

Raises:

  • (Artisan::RecordNotFound)


14
15
16
17
18
# File 'lib/artisan-memory-repository/base_repository.rb', line 14

def find(id)
  record = records[id.to_i]
  raise Artisan::RecordNotFound unless record
  record
end

#find_each(&block) ⇒ Object



20
21
22
23
24
# File 'lib/artisan-memory-repository/base_repository.rb', line 20

def find_each(&block)
  records.each do |id, model|
    block.call(model)
  end
end

#recordsObject



34
35
36
# File 'lib/artisan-memory-repository/base_repository.rb', line 34

def records
  @records ||= {}
end

#save(object) ⇒ Object



4
5
6
7
# File 'lib/artisan-memory-repository/base_repository.rb', line 4

def save(object)
  object.id = records.size + 1 if object.id.nil?
  records[object.id] = object
end

#where(attributes) ⇒ Object



38
39
40
41
42
# File 'lib/artisan-memory-repository/base_repository.rb', line 38

def where(attributes)
  records.values.select do |record|
    attributes.all? { |key, value| record.send(key) == value }
  end
end