class Cranium::DimensionManager

Constants

INSERT_BATCH_SIZE

Attributes

rows[R]

Public Class Methods

for(table_name, key_fields) click to toggle source
# File lib/cranium/dimension_manager.rb, line 7
def self.for(table_name, key_fields)
  @instances ||= {}
  @instances[[table_name, key_fields]] ||= self.new table_name, key_fields
end
new(table_name, key_fields) click to toggle source
# File lib/cranium/dimension_manager.rb, line 14
def initialize(table_name, key_fields)
  @table_name, @key_fields = table_name, key_fields
  @rows = []

  Cranium.application.after_import { flush }
end

Public Instance Methods

create_cache_for_field(value_field) click to toggle source
# File lib/cranium/dimension_manager.rb, line 32
def create_cache_for_field(value_field)
  fields = @key_fields + [value_field]
  to_multi_key_cache(db.select(*fields).group(*fields).map &:values)
end
flush() click to toggle source
# File lib/cranium/dimension_manager.rb, line 39
def flush
  db.multi_insert(@rows, slice: INSERT_BATCH_SIZE) unless @rows.empty?
  @rows = []
end
insert(target_key, row) click to toggle source
# File lib/cranium/dimension_manager.rb, line 23
def insert(target_key, row)
  raise ArgumentError, "Required attribute '#{target_key}' missing" unless row.has_key? target_key

  @rows << resolve_sequence_values(row)
  row[target_key]
end

Private Instance Methods

db() click to toggle source
# File lib/cranium/dimension_manager.rb, line 66
def db
  Cranium::Database.connection[@table_name]
end
resolve_sequence_values(row) click to toggle source
# File lib/cranium/dimension_manager.rb, line 58
def resolve_sequence_values(row)
  row.each do |key, value|
    row[key] = value.next_value if value.is_a? Cranium::Transformation::Sequence
  end
end
to_multi_key_cache(table_data) click to toggle source
# File lib/cranium/dimension_manager.rb, line 52
def to_multi_key_cache(table_data)
  Hash[table_data.map { |row| [row[0..-2], row.last] }]
end