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.



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

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



56
57
58
# File 'lib/couchrest/model/proxyable.rb', line 56

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.



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

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
27
# 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
  proxy_method_names   << assoc_name.to_sym    unless proxy_method_names.include?(assoc_name.to_sym)
  proxied_model_names  << options[:class_name] unless proxied_model_names.include?(options[:class_name])
  class_eval <<-EOS, __FILE__, __LINE__ + 1
    def #{assoc_name}
      @#{assoc_name} ||= CouchRest::Model::Proxyable::ModelProxy.new(::#{options[:class_name]}, self, self.class.to_s.underscore, #{db_method})
    end
  EOS
end

#proxy_method_namesObject



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

def proxy_method_names
  @proxy_method_names ||= []
end

#proxy_owner_methodObject



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

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.



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

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