Module: Switchman::ActiveRecord::AttributeMethods

Defined in:
lib/switchman/active_record/attribute_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(klass) ⇒ Object



262
263
264
265
266
# File 'lib/switchman/active_record/attribute_methods.rb', line 262

def self.prepended(klass)
  klass.singleton_class.prepend(ClassMethods)
  klass.attribute_method_prefix "global_", "local_", "original_"
  klass.attribute_method_affix prefix: "original_", suffix: "="
end

Instance Method Details

#attribute(attr_name) ⇒ Object

these are called if the specific methods haven't been defined yet



269
270
271
272
273
274
275
276
277
278
# File 'lib/switchman/active_record/attribute_methods.rb', line 269

def attribute(attr_name)
  return super unless self.class.sharded_column?(attr_name)

  reflection = self.class.send(:reflection_for_integer_attribute, attr_name)
  ::Switchman::Shard.relative_id_for(
    super,
    shard,
    ::Switchman::Shard.current(connection_class_for_self_for_reflection(reflection))
  )
end

#attribute=(attr_name, new_value) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/switchman/active_record/attribute_methods.rb', line 280

def attribute=(attr_name, new_value)
  unless self.class.sharded_column?(attr_name)
    super
    return
  end

  reflection = self.class.send(:reflection_for_integer_attribute, attr_name)
  super(attr_name, ::Switchman::Shard.relative_id_for(
    new_value,
    ::Switchman::Shard.current(connection_class_for_self_for_reflection(reflection)),
    shard
  ))
end

#global_attribute(attr_name) ⇒ Object



294
295
296
297
298
299
300
# File 'lib/switchman/active_record/attribute_methods.rb', line 294

def global_attribute(attr_name)
  if self.class.sharded_column?(attr_name)
    ::Switchman::Shard.global_id_for(attribute(attr_name), shard)
  else
    attribute(attr_name)
  end
end

#local_attribute(attr_name) ⇒ Object



302
303
304
305
306
307
308
# File 'lib/switchman/active_record/attribute_methods.rb', line 302

def local_attribute(attr_name)
  if self.class.sharded_column?(attr_name)
    ::Switchman::Shard.local_id_for(attribute(attr_name)).first
  else
    attribute(attr_name)
  end
end