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



255
256
257
258
259
# File 'lib/switchman/active_record/attribute_methods.rb', line 255

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



262
263
264
265
266
267
268
269
270
271
# File 'lib/switchman/active_record/attribute_methods.rb', line 262

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



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/switchman/active_record/attribute_methods.rb', line 273

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



287
288
289
290
291
292
293
# File 'lib/switchman/active_record/attribute_methods.rb', line 287

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



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

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