Module: Yogo::DataMapper::RepositoryManager::Resource

Defined in:
lib/yogo/datamapper/repository_manager/resource.rb

Instance Method Summary collapse

Instance Method Details

#adapterObject



13
14
15
16
17
18
19
20
# File 'lib/yogo/datamapper/repository_manager/resource.rb', line 13

def adapter
  begin
    ::DataMapper.repository(managed_repository_name).adapter
  rescue ::DataMapper::RepositoryNotSetupError
    ::DataMapper.setup(managed_repository_name, adapter_config)
    retry
  end
end

#adapter_configObject

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/yogo/datamapper/repository_manager/resource.rb', line 9

def adapter_config
  raise NotImplementedError
end

#build_managed(model_klass, attributes = {}) ⇒ Object

TODO:

Refactor into module in yogo-project

Builds a “new”, unsaved datamapper resource, that is explicitly bound to the Project#managed_repository. If you want to create a new resource that will be saved inside the repository of a Project, you should always use this method.

Boring Details:

Initially "new" model resources do not bind themselves to any repository.
At some point a "new" resource will persist itself and bind itself exclusively
to the repository that it "persisted into". This step is fiddly to catch, and
happens deep inside the DataMapper code. It is MUCH easier to explictly bind
the "new" resource to a particular repository immediately after calling #new.
This requires using reflection to modify the internal state of the resource object,
so it is best sealed inside a single method, rather than scattered throughout
the codebase.

Examples:

Create a new site that is stored in myProject.managed_repository

managedSite = myProject.build_managed(Voeis::Site, :name => ...)

Doing any of these will NOT work consistently (if at all)

managedSite1 = Voeis::Site.new(:name => ...)
managedSite1.save # WILL NOT save in myProject.managed_repository

managedSite2 = myProject.managed_repository{Voeis::Site.new(:name => ...)}
managedSite2.save # WILL NOT save in myProject.managed_repository

Author:

  • Ryan Heimbuch



79
80
81
82
83
84
85
86
87
# File 'lib/yogo/datamapper/repository_manager/resource.rb', line 79

def build_managed(model_klass, attributes={})
  unless self.class.managed_models.include? model_klass
    self.class.manage(model_klass)
    prepare_models
  end
  res = model_klass.new(attributes)
  res.instance_variable_set(:@_repository, managed_repository)
  res
end

#managed_repository(&block) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/yogo/datamapper/repository_manager/resource.rb', line 22

def managed_repository(&block)
  adapter # ensure the adapter get's setup or exists
  if block_given?
    ::DataMapper.repository(managed_repository_name, &block)
  else
    ::DataMapper.repository(managed_repository_name)
  end
end

#managed_repository_nameObject

Raises:

  • (NotImplementedError)


5
6
7
# File 'lib/yogo/datamapper/repository_manager/resource.rb', line 5

def managed_repository_name
  raise NotImplementedError
end

#prepare_modelsObject

TODO:

Refactor this method into a module in yogo-project

Ensure that models that models managed by the Project are properly migrated/upgraded inside the Project managed repository.

Author:

  • Ryan Heimbuch



36
37
38
39
40
41
42
43
44
# File 'lib/yogo/datamapper/repository_manager/resource.rb', line 36

def prepare_models
  adapter # ensure the adapter exists or is setup
  managed_repository.scope {
    self.class.finalize_managed_models!
    self.class.managed_models.each do |klass|
      klass.auto_upgrade!
    end
  }
end