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



257
258
259
260
261
# File 'lib/switchman/active_record/attribute_methods.rb', line 257

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



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

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



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

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



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

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



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

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