Module: Octopus

Defined in:
lib/octopus/rails3/log_subscriber.rb,
lib/octopus.rb,
lib/octopus/version.rb,
lib/octopus/rails3/persistence.rb,
lib/octopus/rails3.2/persistence.rb,
lib/octopus/rails3/abstract_adapter.rb

Overview

Implementation courtesy of db-charmer.

Defined Under Namespace

Modules: AbstractAdapter, Association, AssociationCollection, HasAndBelongsToManyAssociation, LogSubscriber, Migration, MigrationProxy, Migrator, Model, Rails3, Rails32, SingularAssociation, UnknownMigrationVersionError Classes: Proxy, ScopeProxy

Constant Summary collapse

VERSION =
'0.7.0'

Class Method Summary collapse

Class Method Details

.configObject


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/octopus.rb', line 17

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.read(file_name)).result))[Octopus.env()]
    else
      config ||= HashWithIndifferentAccess.new
    end

    config
  end
end

.directoryObject

Returns the Rails.root_to_s when you are using rails Running the current directory in a generic Ruby process


48
49
50
# File 'lib/octopus.rb', line 48

def self.directory()
  @directory ||= defined?(Rails) ?  Rails.root.to_s : Dir.pwd
end

.enabled?Boolean

Public: Whether or not Octopus is configured and should hook into the current environment. Checks the environments config option for the Rails environment by default.

Returns a boolean


36
37
38
39
40
41
42
43
44
# File 'lib/octopus.rb', line 36

def self.enabled?
  if defined?(::Rails)
    Octopus.environments.include?(Rails.env.to_s)
  else
    # TODO: This doens't feel right but !Octopus.config.blank? is breaking a
    #       test. Also, Octopus.config is always returning a hash.
    Octopus.config
  end
end

.envObject


9
10
11
# File 'lib/octopus.rb', line 9

def self.env()
  @env ||= 'octopus'
end

.environmentsObject


63
64
65
# File 'lib/octopus.rb', line 63

def self.environments
  @environments ||= config['environments'] || ['production']
end

.environments=(environments) ⇒ Object


59
60
61
# File 'lib/octopus.rb', line 59

def self.environments=(environments)
  @environments = environments.map { |element| element.to_s }
end

.rails31?Boolean


67
68
69
# File 'lib/octopus.rb', line 67

def self.rails31?
  ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 1
end

.rails32?Boolean


71
72
73
# File 'lib/octopus.rb', line 71

def self.rails32?
  ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 2
end

.rails?Boolean


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

def self.rails?
  defined?(Rails)
end

.rails_envObject


13
14
15
# File 'lib/octopus.rb', line 13

def self.rails_env()
  @rails_env ||= self.rails? ? Rails.env.to_s : 'shards'
end

.setup {|_self| ... } ⇒ Object

This is the default way to do Octopus Setup Available variables: :enviroments => the enviroments that octopus will run. default: 'production'

Yields:

  • (_self)

Yield Parameters:

  • _self (Octopus)

    the object that the method was called on


55
56
57
# File 'lib/octopus.rb', line 55

def self.setup
  yield self
end

.shards=(shards) ⇒ Object


79
80
81
82
# File 'lib/octopus.rb', line 79

def self.shards=(shards)
  config[rails_env()] = HashWithIndifferentAccess.new(shards)
  ActiveRecord::Base.connection.initialize_shards(@config)
end

.using(shard, &block) ⇒ Object


84
85
86
87
88
89
90
91
92
# File 'lib/octopus.rb', line 84

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