Class: RRepo::Repository

Inherits:
Object
  • Object
show all
Includes:
Abstractize
Defined in:
lib/rrepo/repository.rb

Overview

An abstract repository implementation with common functions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ Repository

Returns a new instance of Repository.



25
26
27
28
29
30
31
32
33
# File 'lib/rrepo/repository.rb', line 25

def initialize(adapter)
  @adapter = adapter
  class_name = self.class.name.demodulize
  @collection = (self.class.collection || class_name.underscore).to_sym
  return if class_name == 'Repository'
  @model_class = (
    self.class.model_class_name || class_name.singularize
  ).constantize
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



23
24
25
# File 'lib/rrepo/repository.rb', line 23

def adapter
  @adapter
end

#collectionObject (readonly)

Returns the value of attribute collection.



23
24
25
# File 'lib/rrepo/repository.rb', line 23

def collection
  @collection
end

#model_classObject (readonly)

Returns the value of attribute model_class.



23
24
25
# File 'lib/rrepo/repository.rb', line 23

def model_class
  @model_class
end

Class Method Details

.collection(collection = nil) ⇒ Object



12
13
14
15
# File 'lib/rrepo/repository.rb', line 12

def collection(collection = nil)
  return @collection = collection if collection.present?
  @collection
end

.model_class_name(name = nil) ⇒ Object



17
18
19
20
# File 'lib/rrepo/repository.rb', line 17

def model_class_name(name = nil)
  return @model_class_name = name if name.present?
  @model_class_name
end

Instance Method Details

#allObject



56
57
58
59
# File 'lib/rrepo/repository.rb', line 56

def all
  result = adapter.all(collection)
  result.map(&model_class.method(:new))
end

#clearObject



66
67
68
# File 'lib/rrepo/repository.rb', line 66

def clear
  adapter.clear(collection)
end

#create(model) ⇒ Object



35
36
37
38
39
40
# File 'lib/rrepo/repository.rb', line 35

def create(model)
  return model if model._id.present?
  id = adapter.create(collection, model)
  model._id = id
  model
end

#delete(model) ⇒ Object



49
50
51
52
53
54
# File 'lib/rrepo/repository.rb', line 49

def delete(model)
  unless model._id.present?
    fail Errors::NonPersistedModelError, "#{model} is not persisted"
  end
  adapter.delete(collection, model)
end

#find(id) ⇒ Object



61
62
63
64
# File 'lib/rrepo/repository.rb', line 61

def find(id)
  result = adapter.find(collection, id)
  model_class.new(result)
end

#query(&block) ⇒ Object



70
71
72
# File 'lib/rrepo/repository.rb', line 70

def query(&block)
  adapter.query(collection, &block)
end

#update(model) ⇒ Object



42
43
44
45
46
47
# File 'lib/rrepo/repository.rb', line 42

def update(model)
  unless model._id.present?
    fail Errors::NonPersistedModelError, "#{model} is not persisted"
  end
  adapter.update(collection, model)
end