Class: EmptyEye::PrimaryShard

Inherits:
Object
  • Object
show all
Defined in:
lib/empty_eye/primary_shard.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrangler) ⇒ PrimaryShard

primary shard for master_class class manages associations for database updates has many of the same interfaces as view shards



8
9
10
11
12
# File 'lib/empty_eye/primary_shard.rb', line 8

def initialize(wrangler)
  @table = wrangler.table_name
  @master_class = wrangler.master_class
  @klass = wrangler
end

Class Method Details

.connectionObject



14
15
16
# File 'lib/empty_eye/primary_shard.rb', line 14

def self.connection
  ActiveRecord::Base.connection
end

.exclude_alwaysObject

never include the type field as it shouldnt be needed and cant be updated anyway



19
20
21
# File 'lib/empty_eye/primary_shard.rb', line 19

def self.exclude_always
  ['type', 'mti_schema_version']
end

Instance Method Details

#arel_tableObject

arel table for generating the view



49
50
51
# File 'lib/empty_eye/primary_shard.rb', line 49

def arel_table
  @arel_table ||= Arel::Table.new(table)
end

#columnsObject

the table columns that will be extended in sql



94
95
96
# File 'lib/empty_eye/primary_shard.rb', line 94

def columns
  table_columns - exclude
end

#delegate_to(col, shard) ⇒ Object

delegate setters to appropriate associations



111
112
113
114
# File 'lib/empty_eye/primary_shard.rb', line 111

def delegate_to(col, shard)
  return if shard.primary
  klass.send(:delegate, "#{col}=", {:to => shard.name})
end

#excludeObject



89
90
91
# File 'lib/empty_eye/primary_shard.rb', line 89

def exclude
  self.class.exclude_always
end

#foreign_keyObject



58
59
60
# File 'lib/empty_eye/primary_shard.rb', line 58

def foreign_key
  "id"
end

#has_another(shard) ⇒ Object

create associations for shard class to mimic master_class



99
100
101
102
103
104
105
106
107
108
# File 'lib/empty_eye/primary_shard.rb', line 99

def has_another(shard)
  #this is myself; dont associate
  return if shard.primary
  mimic = shard.association
  return if klass.reflect_on_association(mimic.name)
  options = mimic.options.dup
  options.merge!(default_has_one_options)
  options.merge!(:foreign_key => shard.foreign_key)
  klass.send(mimic.macro, mimic.name, options)
end

#keyObject

this may change but for now the key is the primary id of the master_class and shard



54
55
56
# File 'lib/empty_eye/primary_shard.rb', line 54

def key
  arel_table[:id]
end

#klassObject

class that will mimic the associations of the master_class for updating db



34
35
36
# File 'lib/empty_eye/primary_shard.rb', line 34

def klass
  @klass
end

#master_classObject

class to which this shard belongs



24
25
26
# File 'lib/empty_eye/primary_shard.rb', line 24

def master_class
  @master_class
end

#nameObject

the alias for the table; for primary we just use the table name



44
45
46
# File 'lib/empty_eye/primary_shard.rb', line 44

def name
  @table
end

#polymorphic_typeObject

always null for primary



79
80
81
# File 'lib/empty_eye/primary_shard.rb', line 79

def polymorphic_type
  nil
end

#primaryObject

to let the outside word know it is primary



29
30
31
# File 'lib/empty_eye/primary_shard.rb', line 29

def primary
  true
end

#sti_also?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/empty_eye/primary_shard.rb', line 62

def sti_also?
  !master_class.descends_from_active_record?
end

#tableObject

the tablename



39
40
41
# File 'lib/empty_eye/primary_shard.rb', line 39

def table
  @table
end

#table_columnsObject

table columns



84
85
86
87
# File 'lib/empty_eye/primary_shard.rb', line 84

def table_columns
  klass.column_names
  #self.class.connection.columns(table).collect(&:name)
end

#type_columnObject

arel column of type field



67
68
69
70
71
# File 'lib/empty_eye/primary_shard.rb', line 67

def type_column
  if sti_also? 
    arel_table[master_class.inheritance_column.to_sym]
  end
end

#type_valueObject

value of the polymorphic column



74
75
76
# File 'lib/empty_eye/primary_shard.rb', line 74

def type_value
  master_class.name if type_column
end