Class: RRepo::Repository
- Inherits:
-
Object
- Object
- RRepo::Repository
- Includes:
- Abstractize
- Defined in:
- lib/rrepo/repository.rb
Overview
An abstract repository implementation with common functions
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #clear ⇒ Object
- #create(model) ⇒ Object
- #delete(model) ⇒ Object
- #find(id = nil, &block) ⇒ Object
- #find_all(&block) ⇒ Object
-
#initialize(adapter) ⇒ Repository
constructor
A new instance of Repository.
- #model_class(attributes = {}) ⇒ Object
- #model_for(attributes) ⇒ Object
- #update(model) ⇒ Object
Constructor Details
#initialize(adapter) ⇒ Repository
Returns a new instance of Repository.
26 27 28 29 30 |
# File 'lib/rrepo/repository.rb', line 26 def initialize(adapter) @adapter = adapter class_name = self.class.name.demodulize @collection = (self.class.collection || class_name.underscore).to_sym end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
24 25 26 |
# File 'lib/rrepo/repository.rb', line 24 def adapter @adapter end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
24 25 26 |
# File 'lib/rrepo/repository.rb', line 24 def collection @collection 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, &block) ⇒ Object
17 18 19 20 21 |
# File 'lib/rrepo/repository.rb', line 17 def model_class_name(name = nil, &block) return @model_class_name = name if name.present? return @model_class_name = block if block_given? @model_class_name end |
Instance Method Details
#all ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rrepo/repository.rb', line 53 def all result = adapter.all(collection) Enumerator.new do |y| loop do doc = result.next_document break if doc.nil? y << model_for(doc) end end end |
#clear ⇒ Object
75 76 77 |
# File 'lib/rrepo/repository.rb', line 75 def clear adapter.clear(collection) end |
#create(model) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/rrepo/repository.rb', line 32 def create(model) return model if model._id.present? id = adapter.create(collection, model) model._id = id model end |
#delete(model) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rrepo/repository.rb', line 46 def delete(model) unless model._id.present? fail Errors::NonPersistedModelError, "#{model} is not persisted" end adapter.delete(collection, model) end |
#find(id = nil, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rrepo/repository.rb', line 64 def find(id = nil, &block) # TODO: rewrite to make this db agnostic if block_given? result = adapter.query(collection, &block).run.next_document else result = adapter.find(collection, id).next_document end return if result.blank? model_for(result) end |
#find_all(&block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rrepo/repository.rb', line 79 def find_all(&block) result = adapter.query(collection, &block).run Enumerator.new do |y| loop do doc = result.next_document break if doc.nil? y << model_for(doc) end end end |
#model_class(attributes = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rrepo/repository.rb', line 90 def model_class(attributes = {}) if self.class.model_class_name.respond_to?(:call) model_class_name = self.class.model_class_name.call(attributes) else model_class_name = ( self.class.model_class_name || self.class.name.demodulize.singularize ) end model_class_name.constantize end |
#model_for(attributes) ⇒ Object
101 102 103 |
# File 'lib/rrepo/repository.rb', line 101 def model_for(attributes) model_class(attributes).new(attributes) end |
#update(model) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/rrepo/repository.rb', line 39 def update(model) unless model._id.present? fail Errors::NonPersistedModelError, "#{model} is not persisted" end adapter.update(collection, model) end |