forked from thiagopradi/octopus
/
octopus.rb
109 lines (87 loc) · 2.59 KB
/
octopus.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
if ActiveSupport::VERSION::MAJOR == 3 && ActiveSupport::VERSION::MINOR > 1 # ActiveSupport 3.2
require 'active_support/core_ext/class/attribute'
else # ActiveSupport 3.0, 3.1
require 'active_support/core_ext/class/inheritable_attributes'
end
require "yaml"
require "erb"
module Octopus
def self.env()
@env ||= 'octopus'
end
def self.rails_env()
@rails_env ||= self.rails? ? Rails.env.to_s : 'shards'
end
def self.config()
@config ||= begin
file_name = Octopus.directory() + "/config/shards.yml"
if File.exists? file_name
config ||= HashWithIndifferentAccess.new(YAML.load(ERB.new(File.open(file_name).read()).result))[Octopus.env()]
if config && config['environments']
self.environments = config['environments']
end
else
config ||= HashWithIndifferentAccess.new
end
config
end
end
# Returns the Rails.root_to_s when you are using rails
# Running the current directory in a generic Ruby process
def self.directory()
@directory ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd
end
# This is the default way to do Octopus Setup
# Available variables:
# :enviroments => the enviroments that octopus will run. default: 'production'
def self.setup
yield self
end
def self.environments=(environments)
@environments = environments.map { |element| element.to_s }
end
def self.environments
@environments || ['production']
end
def self.rails3?
ActiveRecord::VERSION::MAJOR == 3
end
def self.rails31?
ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR >= 1
end
def self.rails?
defined?(Rails)
end
def self.shards=(shards)
@config ||= HashWithIndifferentAccess.new
@config[rails_env()] = HashWithIndifferentAccess.new(shards)
ActiveRecord::Base.connection.initialize_shards(@config)
end
def self.using(shard, &block)
conn = ActiveRecord::Base.connection
if conn.is_a?(Octopus::Proxy)
conn.run_queries_on_shard(shard, &block)
else
yield
end
end
end
require "octopus/model"
require "octopus/migration"
require "octopus/association_collection"
require "octopus/has_and_belongs_to_many_association"
require "octopus/association"
if Octopus.rails3?
require "octopus/rails3/association"
require "octopus/rails3/persistence"
require "octopus/rails3/arel"
else
require "octopus/rails2/association"
require "octopus/rails2/persistence"
end
if Octopus.rails31?
require "octopus/rails3.1/singular_association"
end
require "octopus/proxy"
require "octopus/scope_proxy"
require "octopus/logger"