Class: Tzispa::Data::Repository
- Inherits:
-
Object
- Object
- Tzispa::Data::Repository
- Defined in:
- lib/tzispa/data/repository.rb
Constant Summary collapse
- LOCAL_REPO_ROOT =
:repository
Instance Attribute Summary collapse
-
#adapters ⇒ Object
readonly
Returns the value of attribute adapters.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #[](model, repo_id = nil) ⇒ Object
-
#initialize(config, root = LOCAL_REPO_ROOT) ⇒ Repository
constructor
A new instance of Repository.
- #load! ⇒ Object
- #register(model_id, model_class, repo_id, config) ⇒ Object
Constructor Details
#initialize(config, root = LOCAL_REPO_ROOT) ⇒ Repository
Returns a new instance of Repository.
21 22 23 24 25 26 |
# File 'lib/tzispa/data/repository.rb', line 21 def initialize(config, root = LOCAL_REPO_ROOT) @config = config @root = root @pool = Hash.new @adapters = AdapterPool.new config end |
Instance Attribute Details
#adapters ⇒ Object (readonly)
Returns the value of attribute adapters.
17 18 19 |
# File 'lib/tzispa/data/repository.rb', line 17 def adapters @adapters end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
17 18 19 |
# File 'lib/tzispa/data/repository.rb', line 17 def root @root end |
Instance Method Details
#[](model, repo_id = nil) ⇒ Object
28 29 30 31 32 |
# File 'lib/tzispa/data/repository.rb', line 28 def [](model, repo_id=nil) selected_repo = repo_id || @adapters.default raise UnknownModel.new("The '#{model}' model does not exists in the adapter '#{selected_repo}'") unless @pool.has_key? self.class.key(model, selected_repo) @pool[self.class.key(model.to_sym, selected_repo)] end |
#load! ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tzispa/data/repository.rb', line 34 def load! @config.each { |id, cfg| Mutex.new.synchronize { Sequel::Model.db = @adapters[id] if cfg.local build_local_repo id, cfg else require cfg.gem repo_mod, build_method = cfg.register.split('#') (TzString.constantize repo_mod).send build_method, self, id, cfg end } } self end |
#register(model_id, model_class, repo_id, config) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/tzispa/data/repository.rb', line 50 def register(model_id, model_class, repo_id, config) model_class.db = @adapters[repo_id] config.extensions.split(',').each { |ext| model_class.db.extension ext.to_sym } if config.respond_to? :extensions @pool[self.class.key(model_id, repo_id)] = model_class end |