Module: XBar::Model::ClassMethods

Defined in:
lib/xbar/model.rb

Instance Method Summary collapse

Instance Method Details

#clean_table_nameObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/xbar/model.rb', line 96

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



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

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

#connection_proxyObject



75
76
77
# File 'lib/xbar/model.rb', line 75

def connection_proxy
  Thread.current[:connection_proxy] ||= XBar::Proxy.new
end

#connection_with_xbarObject



79
80
81
82
83
84
85
86
# File 'lib/xbar/model.rb', line 79

def connection_with_xbar
  if should_use_normal_connection?
    connection_without_xbar
  else
    connection_proxy.current_model = self  
    connection_proxy
  end
end

#should_use_normal_connection?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/xbar/model.rb', line 64

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



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

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

#unreplicated_model?Boolean

Returns:

  • (Boolean)


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

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

#using(shard_name) ⇒ Object



114
115
116
117
118
# File 'lib/xbar/model.rb', line 114

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



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

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



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

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



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

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