Class: Tzispa::Data::Repository
- Inherits:
-
Object
- Object
- Tzispa::Data::Repository
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/tzispa/data/repository.rb
Instance Attribute Summary collapse
-
#adapters ⇒ Object
readonly
Returns the value of attribute adapters.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Class Method Summary collapse
- .[](model, repo_id = nil) ⇒ Object
- .active_repo(repo_id = nil) ⇒ Object
- .disconnect ⇒ Object
- .known?(model, repo) ⇒ Boolean
- .load!(domain) ⇒ Object
- .models(repo_id = nil) ⇒ Object
- .module_const(repo_id = nil) ⇒ Object
- .register(model_id, model_class, repo_id, config) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Repository
constructor
A new instance of Repository.
- #load_config_repo(id, cfg) ⇒ Object
- #repository_module(repo_id) ⇒ Object
- #setup_model(model_class, repo_id, config) ⇒ Object
Constructor Details
#initialize ⇒ Repository
Returns a new instance of Repository.
30 31 32 33 34 |
# File 'lib/tzispa/data/repository.rb', line 30 def initialize @config = Config.new(Tzispa::Environment.environment)&.to_h @pool = {} @adapters = AdapterPool.new config end |
Instance Attribute Details
#adapters ⇒ Object (readonly)
Returns the value of attribute adapters.
27 28 29 |
# File 'lib/tzispa/data/repository.rb', line 27 def adapters @adapters end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
27 28 29 |
# File 'lib/tzispa/data/repository.rb', line 27 def config @config end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
27 28 29 |
# File 'lib/tzispa/data/repository.rb', line 27 def pool @pool end |
Class Method Details
.[](model, repo_id = nil) ⇒ Object
37 38 39 40 41 |
# File 'lib/tzispa/data/repository.rb', line 37 def [](model, repo_id = nil) repo = active_repo(repo_id) raise UnknownModel.new(model, repo) unless known?(model, repo) instance.pool[repo][model.to_sym] end |
.active_repo(repo_id = nil) ⇒ Object
47 48 49 |
# File 'lib/tzispa/data/repository.rb', line 47 def active_repo(repo_id = nil) repo_id || instance.adapters.default_repo end |
.disconnect ⇒ Object
43 44 45 |
# File 'lib/tzispa/data/repository.rb', line 43 def disconnect instance.disconnect end |
.known?(model, repo) ⇒ Boolean
51 52 53 |
# File 'lib/tzispa/data/repository.rb', line 51 def known?(model, repo) instance.pool.key?(repo) && instance.pool[repo].key?(model.to_sym) end |
.load!(domain) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/tzispa/data/repository.rb', line 64 def load!(domain) instance.config.each do |id, cfg| instance.pool[id] = {} instance.load_config_repo(id, cfg) domain.include module_const(id) end end |
.models(repo_id = nil) ⇒ Object
55 56 57 |
# File 'lib/tzispa/data/repository.rb', line 55 def models(repo_id = nil) instance.pool[active_repo(repo_id)].values end |
.module_const(repo_id = nil) ⇒ Object
59 60 61 62 |
# File 'lib/tzispa/data/repository.rb', line 59 def module_const(repo_id = nil) repo = active_repo(repo_id) instance.pool[repo][:__repository_module] ||= instance.repository_module(repo) end |
.register(model_id, model_class, repo_id, config) ⇒ Object
72 73 74 75 76 |
# File 'lib/tzispa/data/repository.rb', line 72 def register(model_id, model_class, repo_id, config) return if known?(model_id, repo_id) instance.setup_model(model_class, repo_id, config) instance.pool[repo_id][model_id.to_sym] = model_class end |
Instance Method Details
#load_config_repo(id, cfg) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/tzispa/data/repository.rb', line 94 def load_config_repo(id, cfg) if cfg.local local_local(id, cfg) else require cfg.gem repo_module = id.to_s.camelize.constantize self.class.include repo_module self.class.send "load_#{id}", id, cfg end end |
#repository_module(repo_id) ⇒ Object
88 89 90 91 92 |
# File 'lib/tzispa/data/repository.rb', line 88 def repository_module(repo_id) rm = @pool[repo_id].first[1].name.split('::') rm.pop rm.join('::').constantize end |
#setup_model(model_class, repo_id, config) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/tzispa/data/repository.rb', line 79 def setup_model(model_class, repo_id, config) return if model_class.db == adapters[repo_id] model_class.db = adapters[repo_id] return unless config.caching model_class.plugin :caching, adapters.cache[repo_id], ttl: config.ttl || DEFAULT_CACHE_TTL, ignore_exceptions: true end |