Module: XBar::Model::ClassMethods

Defined in:
lib/xbar/model.rb

Instance Method Summary collapse

Instance Method Details

#clean_table_nameObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/xbar/model.rb', line 105

def clean_table_name
  return unless connection_proxy.should_clean_table_name?
  if self != ActiveRecord::Base && self.respond_to?(:reset_table_name) &&
    (if XBar.rails32?
       !self._reset_table_name
     else
       !self.read_inheritable_attribute(:_reset_table_name)
     end
    )
    self.reset_table_name
  end

  if XBar.rails3?
    self.reset_column_information
    self.instance_variable_set(:@quoted_table_name, nil)
  end
end

#connection_pool_with_xbarObject



97
98
99
100
101
102
103
# File 'lib/xbar/model.rb', line 97

def connection_pool_with_xbar
  if should_use_normal_connection?
    connection_pool_without_xbar
  else 
    connection_proxy.connection_pool
  end
end

#connection_proxyObject



80
81
82
83
# File 'lib/xbar/model.rb', line 80

def connection_proxy
  puts "Model allocating new connection proxy"  unless Thread.current[:connection_proxy]
  Thread.current[:connection_proxy] ||= XBar::Proxy.new
end

#connection_with_xbarObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/xbar/model.rb', line 85

def connection_with_xbar
  if should_use_normal_connection?
    connection_without_xbar
  else
    #puts "Model connection with octopus" if XBar.debug
    #if (connection_proxy.current_model.nil?) || (self !=  ActiveRecord::Base)
      connection_proxy.current_model = self  
    #end
    connection_proxy
  end
end

#should_use_normal_connection?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
# File 'lib/xbar/model.rb', line 69

def should_use_normal_connection?
  (defined?(Rails) && XBar.config &&
    !XBar.environments.include?(Rails.env.to_s)) || 
    (if XBar.rails32?
       _establish_connection
     else
       self.read_inheritable_attribute(:_establish_connection)
     end
    )
end

#unreplicated_modelObject



138
139
140
141
142
143
144
# File 'lib/xbar/model.rb', line 138

def unreplicated_model
  if XBar.rails32?
    self._unreplicated = true
  else
    write_inheritable_attribute(:_unreplicated, true)
  end
end

#unreplicated_model?Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
# File 'lib/xbar/model.rb', line 146

def unreplicated_model?
  if XBar.rails32?
     _unreplicated
   else
     read_inheritable_attribute(:_unreplicated)
  end
end

#using(shard_name) ⇒ Object



123
124
125
126
127
# File 'lib/xbar/model.rb', line 123

def using(shard_name)
  return self if defined?(::Rails) && !XBar.environments.include?(Rails.env.to_s)
  clean_table_name
  return XBar::ScopeProxy.new(shard_name, self)
end

#using_any(shard_name = nil) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/xbar/model.rb', line 129

def using_any(shard_name = nil)
  connection_proxy.slave_read_allowed = true
  if shard_name
    using(shard_name)
  else
    self
  end
end

#xbar_establish_connection(spec = nil) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/xbar/model.rb', line 154

def xbar_establish_connection(spec = nil)
  if XBar.rails32?
    self._establish_connection = true
  else
    write_inheritable_attribute(:_establish_connection, true)
  end
  establish_connection(spec)
end

#xbar_set_table_name(value = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/xbar/model.rb', line 163

def xbar_set_table_name(value = nil)
  if XBar.rails32?
    self._reset_table_name = true
    self.table_name = value
  else
    write_inheritable_attribute(:_reset_table_name, true)
    set_table_name(value)
  end
end