Module: Octopus::Model::InstanceMethods

Includes:
SharedMethods
Defined in:
lib/octopus/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedMethods

#using

Class Method Details

.included(base) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/octopus/model.rb', line 32

def self.included(base)
  base.send(:alias_method, :equality_without_octopus, :==)
  base.send(:alias_method, :==, :equality_with_octopus)
  base.send(:alias_method, :eql?, :==)
  base.send(:alias_method, :perform_validations_without_octopus, :perform_validations)
  base.send(:alias_method, :perform_validations, :perform_validations_with_octopus)
end

Instance Method Details

#equality_with_octopus(comparison_object) ⇒ Object



65
66
67
# File 'lib/octopus/model.rb', line 65

def equality_with_octopus(comparison_object)
  equality_without_octopus(comparison_object) && comparison_object.current_shard.to_s == current_shard.to_s
end

#init_with(coder) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/octopus/model.rb', line 46

def init_with(coder)
  obj = super

  return obj unless Octopus.enabled?
  return obj if obj.class.connection_proxy.current_model_replicated?

  current_shard_value = coder['attributes']['current_shard'].value if coder['attributes']['current_shard'].present? && coder['attributes']['current_shard'].value.present?

  coder['attributes'].send(:attributes).send(:values).delete('current_shard')
  coder['attributes'].send(:attributes).send(:delegate_hash).delete('current_shard')

  obj.current_shard = current_shard_value if current_shard_value.present?
  obj
end

#perform_validations_with_octopus(*args) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/octopus/model.rb', line 69

def perform_validations_with_octopus(*args)
  if Octopus.enabled? && should_set_current_shard?
    Octopus.using(current_shard) do
      perform_validations_without_octopus(*args)
    end
  else
    perform_validations_without_octopus(*args)
  end
end

#set_current_shardObject



40
41
42
43
44
# File 'lib/octopus/model.rb', line 40

def set_current_shard
  return unless Octopus.enabled?
  shard = self.class.connection_proxy.current_shard
  self.current_shard = shard if self.class.allowed_shard?(shard)
end

#should_set_current_shard?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/octopus/model.rb', line 61

def should_set_current_shard?
  self.respond_to?(:current_shard) && !current_shard.nil?
end