Module: Dynashard::ActiveRecordExtensions::ClassMethods

Defined in:
lib/dynashard/model.rb

Instance Method Summary collapse

Instance Method Details

#arel_engine_with_dynashardObject

For sharded models, return a Arel::Sql::Engine for the shard class rather than ActiveRecord::Base.



85
86
87
88
89
90
91
# File 'lib/dynashard/model.rb', line 85

def arel_engine_with_dynashard
  if sharding_enabled?
    Arel::Sql::Engine.new(self)
  else
    arel_engine_without_dynashard
  end
end

#connection_with_dynashardObject

For sharded models, return the shard class’s connection

> conn = Dynashard.with_context(:owner => 'shard1'){ShardedModel.connection}
> conn == Dynashard::Shard0.connection
=> true


134
135
136
137
138
139
140
# File 'lib/dynashard/model.rb', line 134

def connection_with_dynashard
  if sharding_enabled?
    dynashard_sharded_subclass.connection
  else
    connection_without_dynashard
  end
end

#create_with_dynashard(attributes = nil, &block) ⇒ Object

For sharded models, return created model objects with the sharded subclass

> Dynashard.with_context(:owner => 'shard1'){ShardedModel.create(attrs)}
=> <#Dynashard::Shard0::ShardedModel id:2>


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

def create_with_dynashard(attributes = nil, &block)
  if sharding_enabled?
    dynashard_sharded_subclass.send(:create_without_dynashard, attributes, &block)
  else
    create_without_dynashard(attributes, &block)
  end
end

#dynashard_association_usingObject

Returns the method used to set the context used for associated models



79
80
81
# File 'lib/dynashard/model.rb', line 79

def dynashard_association_using
  @dynashard_association_using
end

#dynashard_contextObject

Returns the shard context for this model



74
75
76
# File 'lib/dynashard/model.rb', line 74

def dynashard_context
  @dynashard_context
end

#dynashard_model?Boolean

Returns true if the class was generated by Dynashard

Returns:

  • (Boolean)


58
59
60
# File 'lib/dynashard/model.rb', line 58

def dynashard_model?
  false
end

#dynashard_sharded_subclassObject

Return a subclass configured to connect to the appropriate shard



63
64
65
66
67
68
69
70
71
# File 'lib/dynashard/model.rb', line 63

def dynashard_sharded_subclass
  if sharding_enabled?
    spec = Dynashard.shard_context[dynashard_context]
    raise "Missing #{dynashard_context} shard context" if spec.nil?
    spec = spec.call if spec.respond_to?(:call)
    shard_klass = Dynashard.class_for(spec)
    Dynashard.sharded_model_class(shard_klass, self)
  end
end

#instantiate_with_dynashard(record) ⇒ Object

For sharded models, return instantiated model objects with the sharded subclass

> Dynashard.with_context(:owner => 'shard1'){ShardedModel.find(:first)}
=> <#Dynashard::Shard0::ShardedModel id:1>


109
110
111
112
113
114
115
# File 'lib/dynashard/model.rb', line 109

def instantiate_with_dynashard(record)
  if sharding_enabled?
    dynashard_sharded_subclass.send(:instantiate_without_dynashard, record)
  else
    instantiate_without_dynashard(record)
  end
end

#new_with_dynashard(*args) ⇒ Object

For sharded models, return new model objects with the sharded subclass

> Dynashard.with_context(:owner => 'shard1'){ShardedModel.new(attrs)}
=> <#Dynashard::Shard0::ShardedModel id:nil>


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

def new_with_dynashard(*args)
  if sharding_enabled?
    dynashard_sharded_subclass.send(:new_without_dynashard, *args)
  else
    new_without_dynashard(*args)
  end
end

#shard(*args) ⇒ Object

Configure sharding for the current model. The following options are supported:

  • :by => :context - the database connection for this model will be determined using a connection proxy with the specified context.

  • :associated, :using => :method - the database connection for associated models with sharding enabled will be determined using the association’s defined context after first setting that context using the specified method on the current instance

    class MyModel < ActiveRecord::Base

    shard :by => :context
    shard :assocated, :using => :sharding_method
    

    end

    def sharding_method

    # return a valid shard descriptor:
    # - a string key into the configurations in databases.yml
    # - a hash with connection parameters
    # - an object that responds to :call with one of the above
    

    end



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dynashard/model.rb', line 34

def shard(*args)
  if args.first == :associated
    using = args.last[:using] if args.last.respond_to?(:[])
    raise ArgumentError.new(":associated specified without :using") if using.nil? 
    @dynashard_association_using = using
  else
    shard_by = args.first[:by] if args.first.respond_to?(:[])
    raise ArgumentError.new("Invalid options") if shard_by.nil?
    @dynashard_context = shard_by
  end
end

#sharding_enabled?Boolean

Returns true if sharding has been globally enabled and has been configured for this model

Returns:

  • (Boolean)


47
48
49
# File 'lib/dynashard/model.rb', line 47

def sharding_enabled?
  Dynashard.enabled? && !@dynashard_context.nil?
end

#shards_associated?Boolean

Returns true if sharding has been globally enabled and is configured to be used for sharded models associated with this model

Returns:

  • (Boolean)


53
54
55
# File 'lib/dynashard/model.rb', line 53

def shards_associated?
  Dynashard.enabled? && !@dynashard_association_using.nil?
end