Class: Inferno::Repositories::Repository Abstract
- Inherits:
-
Object
- Object
- Inferno::Repositories::Repository
- Extended by:
- Forwardable
- Defined in:
- lib/inferno/repositories/repository.rb
Overview
Base class for repositories. Subclass and override methods as needed.
Direct Known Subclasses
Headers, Messages, Requests, Results, SessionData, Tags, TestRuns, TestSessions, ValidatorSessions
Class Method Summary collapse
-
.db ⇒ Object
Return the db connection for this repository.
-
.table_name ⇒ String
Return the name of the database table for this repository.
Instance Method Summary collapse
-
#add_non_db_entities(hash) ⇒ Object
-
#build_entity(params) ⇒ Object
Creates an instance of the entity associated with this repository.
-
#create(params) ⇒ Inferno::Entities
Create a new record in the database.
-
#db_params(params) ⇒ Object
-
#entity_class ⇒ Class
Return the class of the entity which will be instantiated by a repository.
-
#entity_class_name ⇒ Class
Return the name of the entity class which will be instantiated by a repository.
-
#find(id) ⇒ Inferno::Entities
Find a database record by id, and instantiate an entity from that record.
-
#handle_non_db_params(params) ⇒ Object
-
#non_db_params(params) ⇒ Object
-
#update(entity_id, params = {}) ⇒ Object
Update a record in the database.
-
#update_non_db_entities_ids(hash, use_database_id: false) ⇒ Object
Class Method Details
.db ⇒ Object
Return the db connection for this repository.
13 14 15 |
# File 'lib/inferno/repositories/repository.rb', line 13 def self.db Application['db.connection'][table_name] end |
.table_name ⇒ String
Return the name of the database table for this repository. Override if the table name is not the snake case version of the name of the repository class.
22 23 24 |
# File 'lib/inferno/repositories/repository.rb', line 22 def self.table_name name.demodulize.underscore.to_sym end |
Instance Method Details
#add_non_db_entities(hash) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/inferno/repositories/repository.rb', line 57 def add_non_db_entities(hash) if hash.include? :test_id hash[:test] = Tests.new.find(hash[:test_id]) elsif hash.include? :test_group_id hash[:test_group] = TestGroups.new.find(hash[:test_group_id]) elsif hash.include? :test_suite_id hash[:test_suite] = TestSuites.new.find(hash[:test_suite_id]) end end |
#build_entity(params) ⇒ Object
Creates an instance of the entity associated with this repository. Override if any special logic is required to create the entity.
119 120 121 122 123 |
# File 'lib/inferno/repositories/repository.rb', line 119 def build_entity(params) add_non_db_entities(params) update_non_db_entities_ids(params) entity_class.new(params) end |
#create(params) ⇒ Inferno::Entities
Create a new record in the database.
94 95 96 97 98 99 |
# File 'lib/inferno/repositories/repository.rb', line 94 def create(params) update_non_db_entities_ids(params, use_database_id: true) result = self.class::Model.create(db_params(params)) build_entity(result.to_hash.merge(handle_non_db_params(params))) end |
#db_params(params) ⇒ Object
125 126 127 |
# File 'lib/inferno/repositories/repository.rb', line 125 def db_params(params) params.slice(*self.class::Model.columns) end |
#entity_class ⇒ Class
Return the class of the entity which will be instantiated by a
repository. Override if the entity class is not in the
Inferno::Entities
namespace.
40 41 42 |
# File 'lib/inferno/repositories/repository.rb', line 40 def entity_class Entities.const_get(entity_class_name) end |
#entity_class_name ⇒ Class
Return the name of the entity class which will be instantiated by a repository. Override if the entity class name is not the singular version of the repository name.
31 32 33 |
# File 'lib/inferno/repositories/repository.rb', line 31 def entity_class_name self.class.name.demodulize.singularize end |
#find(id) ⇒ Inferno::Entities
Find a database record by id, and instantiate an entity from that record.
50 51 52 53 54 55 |
# File 'lib/inferno/repositories/repository.rb', line 50 def find(id) result = self.class::Model.find(id:) return result if result.nil? build_entity(result.to_hash) end |
#handle_non_db_params(params) ⇒ Object
133 134 135 |
# File 'lib/inferno/repositories/repository.rb', line 133 def handle_non_db_params(params) non_db_params(params) end |
#non_db_params(params) ⇒ Object
129 130 131 |
# File 'lib/inferno/repositories/repository.rb', line 129 def non_db_params(params) params.except(*self.class::Model.columns) end |
#update(entity_id, params = {}) ⇒ Object
Update a record in the database.
108 109 110 111 112 |
# File 'lib/inferno/repositories/repository.rb', line 108 def update(entity_id, params = {}) self.class::Model .find(id: entity_id) .update(params.merge(updated_at: Time.now)) end |
#update_non_db_entities_ids(hash, use_database_id: false) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/inferno/repositories/repository.rb', line 67 def update_non_db_entities_ids(hash, use_database_id: false) key_map = { test_id: Tests.new, test_group_id: TestGroups.new, test_suite_id: TestSuites.new } key_map.each do |key, repo| next unless hash.key?(key) entity = repo.find(hash[key]) hash[key] = use_database_id ? (entity&.database_id || hash[key]) : (entity&.id || hash[key]) break end end |