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

.included(klass) ⇒ Object



152
153
154
155
# File 'lib/switchman/active_record/attribute_methods.rb', line 152

def self.included(klass)
  klass.singleton_class.include(ClassMethods)
  klass.attribute_method_prefix 'global_', 'local_', 'original_'
end

Instance Method Details

#attribute(attr_name) ⇒ Object

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



167
168
169
170
171
172
# File 'lib/switchman/active_record/attribute_methods.rb', line 167

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_classes_for_reflection(reflection)))
end

#attribute=(attr_name, new_value) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/switchman/active_record/attribute_methods.rb', line 174

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_classes_for_reflection(reflection)), shard))
end

#global_attribute(attr_name) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/switchman/active_record/attribute_methods.rb', line 184

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

#idObject

ensure that we’re using the sharded attribute method and not the silly one in AR::AttributeMethods::PrimaryKey



159
160
161
162
163
164
# File 'lib/switchman/active_record/attribute_methods.rb', line 159

def id
  return super if is_a?(Shard)

  self.class.define_attribute_methods
  super
end

#local_attribute(attr_name) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/switchman/active_record/attribute_methods.rb', line 192

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