Class: Tzispa::Data::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/tzispa/data/repository.rb

Constant Summary collapse

LOCAL_REPO_ROOT =
:repository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, root = LOCAL_REPO_ROOT) ⇒ Repository



23
24
25
26
27
28
# File 'lib/tzispa/data/repository.rb', line 23

def initialize(config, root = LOCAL_REPO_ROOT)
  @config = config
  @root = root
  @pool = Hash.new
  @adapters = AdapterPool.new config
end

Instance Attribute Details

#adaptersObject (readonly)

Returns the value of attribute adapters.



19
20
21
# File 'lib/tzispa/data/repository.rb', line 19

def adapters
  @adapters
end

#rootObject (readonly)

Returns the value of attribute root.



19
20
21
# File 'lib/tzispa/data/repository.rb', line 19

def root
  @root
end

Instance Method Details

#[](model, repo_id = nil) ⇒ Object

Raises:



30
31
32
33
34
# File 'lib/tzispa/data/repository.rb', line 30

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tzispa/data/repository.rb', line 36

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
        self.class.include "Repository::#{id.to_s.camelize}".constantize
        self.class.send "load_#{id}", self, id, cfg
      end
    }
  }
  self
end

#register(model_id, model_class, repo_id, config) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/tzispa/data/repository.rb', line 52

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