Class: DatabaseRepository::Base
- Inherits:
-
Object
- Object
- DatabaseRepository::Base
- Defined in:
- lib/database_repository/base.rb
Class Attribute Summary collapse
-
.entity_class_name ⇒ Object
Returns the value of attribute entity_class_name.
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #build(**attributes) ⇒ Object
- #create(**attributes) ⇒ Object
- #create!(**attributes) ⇒ Object
- #delete(id) ⇒ Object
- #delete_all ⇒ Object
- #destroy(id) ⇒ Object
- #destroy!(id) ⇒ Object
- #destroy_all ⇒ Object
- #find(id) ⇒ Object
- #find_by(**attributes) ⇒ Object
- #find_or_create_by(**attributes) ⇒ Object
- #find_or_create_by!(**attributes) ⇒ Object
- #find_or_initialize_by(**attributes) ⇒ Object
- #first(limit = nil) ⇒ Object
- #first! ⇒ Object
- #last(limit = nil) ⇒ Object
- #last! ⇒ Object
- #update(id, **attributes) ⇒ Object
- #update!(id, **attributes) ⇒ Object
- #update_all(**attributes) ⇒ Object
Class Attribute Details
.entity_class_name ⇒ Object
Returns the value of attribute entity_class_name.
8 9 10 |
# File 'lib/database_repository/base.rb', line 8 def entity_class_name @entity_class_name end |
Class Method Details
.entity(entity_class_name) ⇒ Object
10 11 12 |
# File 'lib/database_repository/base.rb', line 10 def entity(entity_class_name) @entity_class_name = entity_class_name end |
Instance Method Details
#all ⇒ Object
15 16 17 |
# File 'lib/database_repository/base.rb', line 15 def all entity.all end |
#build(**attributes) ⇒ Object
19 20 21 |
# File 'lib/database_repository/base.rb', line 19 def build(**attributes) entity.new(attributes) end |
#create(**attributes) ⇒ Object
67 68 69 |
# File 'lib/database_repository/base.rb', line 67 def create(**attributes) entity.create(attributes) end |
#create!(**attributes) ⇒ Object
71 72 73 74 75 |
# File 'lib/database_repository/base.rb', line 71 def create!(**attributes) entity.create!(attributes) rescue ActiveRecord::RecordInvalid => e raise DatabaseRepository::RecordInvalid, e. end |
#delete(id) ⇒ Object
95 96 97 |
# File 'lib/database_repository/base.rb', line 95 def delete(id) find(id).delete end |
#delete_all ⇒ Object
109 110 111 |
# File 'lib/database_repository/base.rb', line 109 def delete_all entity.delete_all end |
#destroy(id) ⇒ Object
99 100 101 |
# File 'lib/database_repository/base.rb', line 99 def destroy(id) find(id).destroy end |
#destroy!(id) ⇒ Object
103 104 105 106 107 |
# File 'lib/database_repository/base.rb', line 103 def destroy!(id) find(id).destroy! rescue ActiveRecord::RecordNotDestroyed => e raise DatabaseRepository::RecordNotDestroyed, e. end |
#destroy_all ⇒ Object
113 114 115 |
# File 'lib/database_repository/base.rb', line 113 def destroy_all entity.destroy_all end |
#find(id) ⇒ Object
23 24 25 26 27 |
# File 'lib/database_repository/base.rb', line 23 def find(id) entity.find(id) rescue ActiveRecord::RecordNotFound => e raise DatabaseRepository::RecordNotFound, e. end |
#find_by(**attributes) ⇒ Object
29 30 31 |
# File 'lib/database_repository/base.rb', line 29 def find_by(**attributes) entity.find_by(attributes) end |
#find_or_create_by(**attributes) ⇒ Object
37 38 39 |
# File 'lib/database_repository/base.rb', line 37 def find_or_create_by(**attributes) entity.find_or_create_by(attributes) end |
#find_or_create_by!(**attributes) ⇒ Object
41 42 43 44 45 |
# File 'lib/database_repository/base.rb', line 41 def find_or_create_by!(**attributes) entity.find_or_create_by!(attributes) rescue ActiveRecord::RecordInvalid => e raise DatabaseRepository::RecordInvalid, e. end |
#find_or_initialize_by(**attributes) ⇒ Object
33 34 35 |
# File 'lib/database_repository/base.rb', line 33 def find_or_initialize_by(**attributes) entity.find_or_initialize_by(attributes) end |
#first(limit = nil) ⇒ Object
47 48 49 |
# File 'lib/database_repository/base.rb', line 47 def first(limit = nil) entity.first(limit) end |
#first! ⇒ Object
51 52 53 54 55 |
# File 'lib/database_repository/base.rb', line 51 def first! entity.first! rescue ActiveRecord::RecordNotFound => e raise DatabaseRepository::RecordNotFound, e. end |
#last(limit = nil) ⇒ Object
57 58 59 |
# File 'lib/database_repository/base.rb', line 57 def last(limit = nil) entity.last(limit) end |
#last! ⇒ Object
61 62 63 64 65 |
# File 'lib/database_repository/base.rb', line 61 def last! entity.last! rescue ActiveRecord::RecordNotFound => e raise DatabaseRepository::RecordNotFound, e. end |
#update(id, **attributes) ⇒ Object
77 78 79 80 81 |
# File 'lib/database_repository/base.rb', line 77 def update(id, **attributes) find(id).tap do |record| record.update(attributes) end end |
#update!(id, **attributes) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/database_repository/base.rb', line 83 def update!(id, **attributes) find(id).tap do |record| record.update!(attributes) end rescue ActiveRecord::RecordInvalid => e raise DatabaseRepository::RecordInvalid, e. end |
#update_all(**attributes) ⇒ Object
91 92 93 |
# File 'lib/database_repository/base.rb', line 91 def update_all(**attributes) entity.update_all(attributes) end |