Module: CouchRest::Model::Proxyable::ClassMethods

Defined in:
lib/couchrest/model/proxyable.rb

Instance Method Summary collapse

Instance Method Details

#proxied_by(model_name, options = {}) ⇒ Object

Tell this model which other model to use as a base for the database connection to use.



30
31
32
33
34
35
36
# File 'lib/couchrest/model/proxyable.rb', line 30

def proxied_by(model_name, options = {})
  raise "Model can only be proxied once or ##{model_name} already defined" if method_defined?(model_name) || !proxy_owner_method.nil?
  self.proxy_owner_method = model_name
  attr_accessor :model_proxy
  attr_accessor model_name
  overwrite_database_reader(model_name)
end

#proxied_model_namesObject



51
52
53
# File 'lib/couchrest/model/proxyable.rb', line 51

def proxied_model_names
  @proxied_model_names ||= []
end

#proxy_database_method(name = nil) ⇒ Object

Define the name of a method to call to determine the name of the database to use as a proxy.



46
47
48
49
# File 'lib/couchrest/model/proxyable.rb', line 46

def proxy_database_method(name = nil)
  @proxy_database_method = name if name
  @proxy_database_method
end

#proxy_for(assoc_name, options = {}) ⇒ Object

Define a collection that will use the base model for the database connection details.



17
18
19
20
21
22
23
24
25
26
# File 'lib/couchrest/model/proxyable.rb', line 17

def proxy_for(assoc_name, options = {})
  db_method = options[:database_method] || "proxy_database"
  options[:class_name] ||= assoc_name.to_s.singularize.camelize
  proxied_model_names << options[:class_name] unless proxied_model_names.include?(options[:class_name])
  class_eval "def \#{assoc_name}\n@\#{assoc_name} ||= CouchRest::Model::Proxyable::ModelProxy.new(::\#{options[:class_name]}, self, self.class.to_s.underscore, \#{db_method})\nend\n", __FILE__, __LINE__ + 1
end

#proxy_owner_methodObject



42
# File 'lib/couchrest/model/proxyable.rb', line 42

def proxy_owner_method; @proxy_owner_method; end

#proxy_owner_method=(name) ⇒ Object

Define an a class variable accessor ready to be inherited and unique for each Class using the base. Perhaps there is a shorter way of writing this.



41
# File 'lib/couchrest/model/proxyable.rb', line 41

def proxy_owner_method=(name); @proxy_owner_method = name; end