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



218
219
220
221
222
# File 'lib/switchman/active_record/attribute_methods.rb', line 218

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



225
226
227
228
229
230
# File 'lib/switchman/active_record/attribute_methods.rb', line 225

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



232
233
234
235
236
237
238
239
240
# File 'lib/switchman/active_record/attribute_methods.rb', line 232

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(::Switchman::Shard.relative_id_for(new_value, ::Switchman::Shard.current(connection_class_for_self_for_reflection(reflection)), shard))
end

#global_attribute(attr_name) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/switchman/active_record/attribute_methods.rb', line 242

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



250
251
252
253
254
255
256
# File 'lib/switchman/active_record/attribute_methods.rb', line 250

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