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



131
132
133
134
# File 'lib/switchman/active_record/attribute_methods.rb', line 131

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



146
147
148
149
150
151
# File 'lib/switchman/active_record/attribute_methods.rb', line 146

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



153
154
155
156
157
158
159
160
161
# File 'lib/switchman/active_record/attribute_methods.rb', line 153

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



163
164
165
166
167
168
169
# File 'lib/switchman/active_record/attribute_methods.rb', line 163

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



138
139
140
141
142
143
# File 'lib/switchman/active_record/attribute_methods.rb', line 138

def id
  return super if is_a?(Shard)

  self.class.define_attribute_methods
  super
end

#local_attribute(attr_name) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/switchman/active_record/attribute_methods.rb', line 171

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