lib/mixed_gauge/model.rb in mixed_gauge-0.1.3 vs lib/mixed_gauge/model.rb in mixed_gauge-0.1.4
- old
+ new
@@ -46,10 +46,11 @@
# error.
# @param [Hash] attributes
# @return [ActiveRecord::Base] A sub class instance of included model
# @raise [MixedGauge::MissingDistkeyAttribute]
def put!(attributes)
+ raise '`distkey` is not defined. Use `def_distkey`.' unless distkey
@before_put_callback.call(attributes) if @before_put_callback
if key = attributes[distkey] || attributes[distkey.to_s]
shard_for(key).create!(attributes)
else
@@ -108,23 +109,31 @@
# User.all_shards.flat_map {|m| m.find_by(name: 'alice') }.compact
def all_shards
sub_model_repository.all
end
+ # @return [Mixedgauge::AllShardsInParallel]
+ # @example
+ # User.all_shards_in_parallel.map {|m| m.where.find_by(name: 'Alice') }.compact
+ def all_shards_in_parallel
+ AllShardsInParallel.new(all_shards)
+ end
+ alias_method :parallel, :all_shards_in_parallel
+
# Define utility methods which uses all shards or specific shard.
# These methods can be called from included model class.
# @example
# class User
# include MixedGauge::Model
# use_cluster :user
# def_distkey :name
# parent_methods do
# def all_count
- # all_shards.map {|m| m.count }.reduce(&:+)
+ # parallel.map {|m| m.count }.reduce(&:+)
# end
#
# def find_from_all_by(condition)
- # all_shards.flat_map {|m m.find_by(condition) }.compact.first
+ # parallel.flat_map {|m m.find_by(condition) }.compact.first
# end
# end
# end
#
# User.put!(email: 'a@m.com', name: 'a')