Class: Sengiri::Model::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/sengiri/model/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil, options = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/sengiri/model/base.rb', line 7

def initialize(attributes = nil, options = {})
  @shard_name = self.class.shard_name
  super
end

Instance Attribute Details

#current_shardObject (readonly)

Returns the value of attribute current_shard.



5
6
7
# File 'lib/sengiri/model/base.rb', line 5

def current_shard
  @current_shard
end

Class Method Details

.dbconfsObject



56
57
58
59
60
61
62
# File 'lib/sengiri/model/base.rb', line 56

def dbconfs
  if defined? Rails
    @dbconfs ||= Rails.application.config.database_configuration.select {|name| /^#{@sharding_group_name}/ =~ name }

  end
  @dbconfs
end

.establish_shard_connection(name) ⇒ Object



101
102
103
# File 'lib/sengiri/model/base.rb', line 101

def establish_shard_connection(name)
  establish_connection dbconfs["#{@sharding_group_name}_shards_#{name}"]
end

.shard(name) ⇒ Object



71
72
73
74
75
76
# File 'lib/sengiri/model/base.rb', line 71

def shard(name)
  if block_given?
    yield @shard_class_hash[name.to_s]
  end
  @shard_class_hash[name.to_s]
end

.shard_classesObject



24
25
26
27
# File 'lib/sengiri/model/base.rb', line 24

def shard_classes
  return @shard_class_hash.values if @shard_class_hash
  []
end

.shard_nameObject



21
# File 'lib/sengiri/model/base.rb', line 21

def shard_name         ; @shard_name          end

.shard_namesObject



64
65
66
67
68
69
# File 'lib/sengiri/model/base.rb', line 64

def shard_names
  @shard_names ||= dbconfs.map do |k,v|
    k.gsub("#{@sharding_group_name}_shards_", '')
  end
  @shard_names
end

.sharding_group(name, confs = nil) ⇒ Object



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
# File 'lib/sengiri/model/base.rb', line 29

def sharding_group(name, confs=nil)
  @dbconfs = confs if confs
  @sharding_group_name = name
  @shard_class_hash = {}
  first = true
  shard_names.each do |s|
    klass = Class.new(self)
    Object.const_set self.name + s, klass
    klass.instance_variable_set :@shard_name, s
    klass.instance_variable_set :@dbconfs,    dbconfs
    klass.instance_variable_set :@sharding_group_name, name
    #
    # first shard shares connection with base class
    #
    if first
      establish_shard_connection s
    else
      klass.establish_shard_connection s
    end
    first = false
    if defined? Ardisconnector::Middleware
      Ardisconnector::Middleware.models << klass
    end
    @shard_class_hash[s] = klass
  end
end

.sharding_group_nameObject



22
# File 'lib/sengiri/model/base.rb', line 22

def sharding_group_name; @sharding_group_name end

.shards(&block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/sengiri/model/base.rb', line 78

def shards(&block)
  transaction do
    shard_names.each do |shard_name|
      block.call shard(shard_name)
    end
  end
end

.transaction(klasses = shard_classes, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sengiri/model/base.rb', line 86

def transaction(klasses=shard_classes, &block)
  if shard_name
    super &block
  else
    if klasses.length > 0
      klass = klasses.pop
      klass.transaction do
        transaction klasses, &block
      end
    else
      yield
    end
  end
end

Instance Method Details

#shard_nameObject



16
17
18
# File 'lib/sengiri/model/base.rb', line 16

def shard_name
  self.class.instance_variable_get :@shard_name
end

#sharding_group_nameObject



12
13
14
# File 'lib/sengiri/model/base.rb', line 12

def sharding_group_name
  self.class.instance_variable_get :@sharding_group_name
end