Class: EmptyEye::ShardCollection

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

Instance Method Summary collapse

Constructor Details

#initialize(primary_shard_klass) ⇒ ShardCollection

a collection of all the view_shards these are wranglers for the shards uses ‘array’ as a proxy performs array methods by passing things off in method missing



9
10
11
12
13
# File 'lib/empty_eye/shard_collection.rb', line 9

def initialize(primary_shard_klass)
  @master_class = primary_shard_klass.master_class
  @primary = PrimaryShard.new(primary_shard_klass)
  @array = [@primary]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

delegate to the array proxy when the method is missing



77
78
79
80
81
82
83
# File 'lib/empty_eye/shard_collection.rb', line 77

def method_missing(m, *args, &block)
  if respond_to?(m)
    array.send(m, *args, &block)
  else
    super
  end
end

Instance Method Details

#arrayObject

the proxy object for instances



16
17
18
# File 'lib/empty_eye/shard_collection.rb', line 16

def array
  @array
end

#create_view_sqlObject

generates view sql



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/empty_eye/shard_collection.rb', line 90

def create_view_sql
  #determine what shard will handle what columns
  map_attribute_management
  #start with primary table
  query = primary_arel_table
  
  #build select clause with correct table handling the appropriate columns
  query = query.project(*arel_columns)
  
  #build joins
  each do |shard|
    next if shard.primary
    current = shard.arel_table
    key = shard.foreign_key.to_sym
    if shard.type_column
      query.join(current).on(
        primary.key.eq(current[key]), shard.type_column.eq(shard.type_value)
      )
    else
      query.join(current).on(
        primary.key.eq(current[key])
      )
    end
  end
  
  #we dont need to keep this data
  free_arel_columns
  
  #STI condition if needed
  if primary.sti_also?
    query.where(primary.type_column.eq(primary.type_value))
  end
  
  #build view creation statement
  @view_sql = "CREATE VIEW #{primary.klass.compute_view_name} AS\n#{query.to_sql}"
end

#create_with(assoc) ⇒ Object

add shard based on association from master_class



36
37
38
39
40
41
# File 'lib/empty_eye/shard_collection.rb', line 36

def create_with(assoc)
  new_shard = Shard.new(assoc)
  reject! {|shard| shard.name == new_shard.name}
  push(new_shard)
  new_shard
end

#delegate_map(name, hash) ⇒ Object

takes the name of shard and a hash of intended updates from master instance returns a subset of hash with only values the shard handles



49
50
51
52
53
54
55
# File 'lib/empty_eye/shard_collection.rb', line 49

def delegate_map(name, hash)
  keys = update_mapping[name] & hash.keys
  keys.inject({}) do |res, col|
    res[col] = hash[col] if hash[col]
    res
  end
end

#descend(klass) ⇒ Object



30
31
32
33
# File 'lib/empty_eye/shard_collection.rb', line 30

def descend(klass)
  @master_class = klass
  self
end

#inspectObject

we want to see the proxy object not the class info



21
22
23
# File 'lib/empty_eye/shard_collection.rb', line 21

def inspect
  array.inspect
end

#klassesObject

array of shard classes



63
64
65
# File 'lib/empty_eye/shard_collection.rb', line 63

def klasses
  map(&:klass)
end

#master_classObject

the class to which the shards belongs



26
27
28
# File 'lib/empty_eye/shard_collection.rb', line 26

def master_class
  @master_class
end

#namesObject



67
68
69
# File 'lib/empty_eye/shard_collection.rb', line 67

def names
  map(&:name)
end

#primaryObject

the primary shard



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

def primary
  @primary
end

#respond_to?(m) ⇒ Boolean

this object responds to array methods

Returns:

  • (Boolean)


72
73
74
# File 'lib/empty_eye/shard_collection.rb', line 72

def respond_to?(m)
  super || array.respond_to?(m)
end

#schema_versionObject



43
44
45
# File 'lib/empty_eye/shard_collection.rb', line 43

def schema_version
  @schema_version
end

#view_sqlObject



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

def view_sql
  @view_sql
end