Module: Wepo::Repo
- Defined in:
- lib/wepo/repo.rb
Overview
Repo base class
Instance Attribute Summary collapse
- #adapter ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #create(entity) ⇒ Object
- #delete(entity) ⇒ Object
- #find(id) ⇒ Object
- #find_or_initialize_by(params) ⇒ Object
- #initialize(adapter) ⇒ Object
- #save(entity) ⇒ Object
- #update(entity) ⇒ Object
- #where(params) ⇒ Object
Instance Attribute Details
#adapter ⇒ Object (readonly)
12 13 14 |
# File 'lib/wepo/repo.rb', line 12 def adapter @adapter end |
Class Method Details
.included(base) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/wepo/repo.rb', line 18 def self.included(base) base.instance_eval do extend Uber::InheritableAttr inheritable_attr :model_class inheritable_attr :entity_class self.model_class = nil self.entity_class = nil def model(*args) if args[0] self.model_class = args[0] else self.model_class end end def entity(*args) if args[0] self.entity_class = args[0] else self.entity_class end end end end |
Instance Method Details
#all ⇒ Object
45 46 47 |
# File 'lib/wepo/repo.rb', line 45 def all adapter.all(model_class).map(&method(:map_model)) end |
#create(entity) ⇒ Object
65 66 67 68 |
# File 'lib/wepo/repo.rb', line 65 def create(entity) raise ArgumentError, 'entity cannot be nil' if entity.nil? map_model adapter.create(map_entity(entity)) end |
#delete(entity) ⇒ Object
75 76 77 78 |
# File 'lib/wepo/repo.rb', line 75 def delete(entity) raise ArgumentError, 'entity cannot be nil' if entity.nil? map_model adapter.delete(map_entity(entity)) end |
#find(id) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/wepo/repo.rb', line 49 def find(id) begin adapter.find(model_class, id) rescue raise EntityNotFound, "Could not find #{model_class} with id: #{id}" end end |
#find_or_initialize_by(params) ⇒ Object
57 58 59 |
# File 'lib/wepo/repo.rb', line 57 def find_or_initialize_by(params) map_model adapter.find_or_initialize_by(model_class, params) end |
#initialize(adapter) ⇒ Object
14 15 16 |
# File 'lib/wepo/repo.rb', line 14 def initialize(adapter) @adapter = adapter end |
#save(entity) ⇒ Object
80 81 82 |
# File 'lib/wepo/repo.rb', line 80 def save(entity) entity.id.nil? ? create(entity) : update(entity) end |
#update(entity) ⇒ Object
70 71 72 73 |
# File 'lib/wepo/repo.rb', line 70 def update(entity) raise ArgumentError, 'entity cannot be nil' if entity.nil? map_model adapter.update(map_entity(entity)) end |
#where(params) ⇒ Object
61 62 63 |
# File 'lib/wepo/repo.rb', line 61 def where(params) adapter.where(model_class, params).map(&method(:map_model)) end |