Module: EmptyEye::ShardWrangler::ClassMethods
- Defined in:
- lib/empty_eye/shard_wrangler.rb
Instance Method Summary collapse
-
#cascade_delete_all(conditions) ⇒ Object
batch deletion when there are conditions kill indiscriminately otherwise.
- #cascade_update_all(updates, conditions, options) ⇒ Object
-
#compute_view_name ⇒ Object
we need a name for the view need to have a way to set this.
-
#create_reflection(macro, name, options, active_record) ⇒ Object
the wrangler uses special reflection; overriden here.
-
#find_by_id(val) ⇒ Object
overriding find_by_id this is used to retrieve the wrangler instance for the master instance the type column is removed.
-
#has_one(name, options = {}) ⇒ Object
the wrangler uses a special association builder.
-
#master_class ⇒ Object
reflection on master class; this should never change.
-
#master_class=(klass) ⇒ Object
the master_class value is set with this setter; should happen only once.
-
#primary_shard ⇒ Object
the primary shard.
-
#reset_column_information ⇒ Object
overriding to reset the special instance variable.
- #shards ⇒ Object
-
#type_condition(table = arel_table) ⇒ Object
finder methods should use the master class’s type not the wrangler’s.
-
#wrangle_shards(mti_ancestors) ⇒ Object
we know the associations and we know what they can do we will make a mti class accordingly here.
Instance Method Details
#cascade_delete_all(conditions) ⇒ Object
batch deletion when there are conditions kill indiscriminately otherwise
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/empty_eye/shard_wrangler.rb', line 173 def cascade_delete_all(conditions) mti_clear_identity_map affected = 0 ids = [] ids = conditions ? select(arel_table[primary_key.to_sym]).where(conditions).collect(&:id) : [] transaction do begin batch = ids.pop(10000) shards.each do |shard| result = if conditions.nil? shard.klass.delete_all elsif shard.polymorphic_type shard.klass.delete_all(shard.foreign_key => batch, shard.polymorphic_type => shard.type_value) else shard.klass.delete_all(shard.foreign_key => batch) end affected = [affected, result].max end end until ids.to_a.empty? end affected end |
#cascade_update_all(updates, conditions, options) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/empty_eye/shard_wrangler.rb', line 196 def cascade_update_all(updates, conditions, ) mti_clear_identity_map affected = 0 stringified_updates = updates.stringify_keys ids = conditions ? select(arel_table[primary_key.to_sym]).where(conditions).(.slice(:limit, :order)).collect(&:id) : [] transaction do begin batch = ids.pop(10000) shards.each do |shard| cols = shards.delegate_map(shard.name, stringified_updates) next if cols.empty? result = if conditions.nil? shard.klass.update_all(cols) elsif shard.polymorphic_type shard.klass.update_all(cols, shard.foreign_key => batch, shard.polymorphic_type => shard.type_value) else shard.klass.update_all(cols, shard.foreign_key => batch) end affected = [affected, result].max end end until ids.to_a.empty? end affected end |
#compute_view_name ⇒ Object
we need a name for the view need to have a way to set this
227 228 229 230 231 232 233 |
# File 'lib/empty_eye/shard_wrangler.rb', line 227 def compute_view_name if master_class.descends_from_active_record? master_class.send(:compute_table_name) else master_class.name.underscore.pluralize end end |
#create_reflection(macro, name, options, active_record) ⇒ Object
the wrangler uses special reflection; overriden here
110 111 112 113 114 115 116 117 |
# File 'lib/empty_eye/shard_wrangler.rb', line 110 def create_reflection(macro, name, , active_record) raise(EmptyEye::NotYetSupported, "through associations are not yet spported") if [:through] klass = [:through] ? ShardThroughReflection : ShardAssociationReflection reflection = klass.new(macro, name, , active_record) self.reflections = self.reflections.merge(name => reflection) reflection end |
#find_by_id(val) ⇒ Object
overriding find_by_id this is used to retrieve the wrangler instance for the master instance the type column is removed
129 130 131 132 133 |
# File 'lib/empty_eye/shard_wrangler.rb', line 129 def find_by_id(val) query = columns_except_type query = query.where(arel_table[:id].eq(val)) find_by_sql(query.to_sql).first end |
#has_one(name, options = {}) ⇒ Object
the wrangler uses a special association builder
136 137 138 |
# File 'lib/empty_eye/shard_wrangler.rb', line 136 def has_one(name, = {}) Associations::Builder::ShardHasOne.build(self, name, ) end |
#master_class ⇒ Object
reflection on master class; this should never change
141 142 143 |
# File 'lib/empty_eye/shard_wrangler.rb', line 141 def master_class @master_class end |
#master_class=(klass) ⇒ Object
the master_class value is set with this setter; should happen only once
146 147 148 |
# File 'lib/empty_eye/shard_wrangler.rb', line 146 def master_class=(klass) @master_class = klass end |
#primary_shard ⇒ Object
the primary shard
157 158 159 |
# File 'lib/empty_eye/shard_wrangler.rb', line 157 def primary_shard shards.primary end |
#reset_column_information ⇒ Object
overriding to reset the special instance variable
151 152 153 154 |
# File 'lib/empty_eye/shard_wrangler.rb', line 151 def reset_column_information @columns_except_type = nil super end |
#shards ⇒ Object
221 222 223 |
# File 'lib/empty_eye/shard_wrangler.rb', line 221 def shards @shards ||= EmptyEye::ShardCollection.new(self) end |
#type_condition(table = arel_table) ⇒ Object
finder methods should use the master class’s type not the wrangler’s
120 121 122 123 124 |
# File 'lib/empty_eye/shard_wrangler.rb', line 120 def type_condition(table = arel_table) sti_column = table[inheritance_column.to_sym] sti_column.eq(master_class.name) end |
#wrangle_shards(mti_ancestors) ⇒ Object
we know the associations and we know what they can do we will make a mti class accordingly here
163 164 165 166 167 168 169 |
# File 'lib/empty_eye/shard_wrangler.rb', line 163 def wrangle_shards(mti_ancestors) mti_ancestors.each do |assoc| shards.create_with(assoc) end create_view master_class.reset_column_information end |