Module: EmptyEye::ShardWrangler

Defined in:
lib/empty_eye/shard_wrangler.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(master_class, t_name) ⇒ Object

this module method creates a ShardWrangler extended ActiveRecord inherited class the class will wrangle our shards



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/empty_eye/shard_wrangler.rb', line 13

def self.create(master_class, t_name)
  inherit_from = if master_class.base_class == master_class
    ActiveRecord::Base
  else
    master_class.superclass
  end
  
  table_name = if t_name
    t_name
  elsif master_class.descends_from_active_record?
    "#{master_class.name.underscore.pluralize}_core"
  else
    master_class.superclass.table_name
  end
  
  new_class = Class.new(inherit_from)
  new_class.send(:include, ShardWrangler)
  new_class.table_name = table_name
  new_class.master_class = master_class
  EmptyEye.const_set("#{master_class.to_s}Wrangler", new_class)
  new_class
end

.included(base) ⇒ Object

module which extends the class that serves as a pointer to the primary table when there is a superclass the shard will inherit from that, else it will inherit from ActiveRecord the primary shard manages all the MTI associated tables for the master class



7
8
9
# File 'lib/empty_eye/shard_wrangler.rb', line 7

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#cascade_saveObject

special save so that the wrangler can keep the master’s instance tables consistent



50
51
52
53
54
55
56
57
# File 'lib/empty_eye/shard_wrangler.rb', line 50

def cascade_save
  write_attributes
  #this will autosave shards
  save
  #reset the id and then reload
  master_instance.id = id
  master_instance.reload
end

#master_classObject

reflection on master class; this should never change



60
61
62
# File 'lib/empty_eye/shard_wrangler.rb', line 60

def master_class
  self.class.master_class
end

#master_instanceObject

the instance that owns this wrangler we usually know the master instance ahead of time so we should take care to set this manually we want to avoid the lookup



40
41
42
# File 'lib/empty_eye/shard_wrangler.rb', line 40

def master_instance
  @master_instance || master_class.find_by_id(id)
end

#master_instance=(instance) ⇒ Object

setter used to associate the wrangler with the master instance



45
46
47
# File 'lib/empty_eye/shard_wrangler.rb', line 45

def master_instance=(instance)
  @master_instance = instance
end

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/empty_eye/shard_wrangler.rb', line 64

def valid?(context = nil)
  context ||= (new_record? ? :create : :update)
  write_attributes
  output = super(context)
  errors.each do |attr, message|
    attr = attr.to_s.partition('.').last if attr.to_s =~ /\./
    master_instance.errors.add(attr, message)
  end
  errors.empty? && output
end