Module: Mongoid::Shardable::ClassMethods
- Defined in:
- lib/mongoid/shardable.rb
Overview
Instance Method Summary collapse
-
#shard_key(*args) ⇒ Object
Specifies a shard key with the field(s) specified.
Instance Method Details
#shard_key(*args) ⇒ Object
Specifies a shard key with the field(s) specified.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mongoid/shardable.rb', line 86 def shard_key(*args) unless args.first.is_a?(Hash) # Shorthand syntax if args.last.is_a?(Hash) raise ArgumentError, 'Shorthand shard_key syntax does not permit options' end spec = Hash[args.map do |name| [name, 1] end] return shard_key(spec) end if args.length > 2 raise ArgumentError, 'Full shard_key syntax requires 1 or 2 arguments' end spec, = args spec = Hash[spec.map do |name, value| if value.is_a?(Symbol) value = value.to_s end [database_field_name(name).to_sym, value] end] self.shard_key_fields = spec.keys self.shard_config = { key: spec.freeze, options: ( || {}).dup.freeze, }.freeze end |