Class: Cranium::DimensionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cranium/dimension_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, key_fields) ⇒ DimensionManager

Returns a new instance of DimensionManager.



14
15
16
17
18
19
# 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

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



3
4
5
# File 'lib/cranium/dimension_manager.rb', line 3

def rows
  @rows
end

Class Method Details

.for(table_name, key_fields) ⇒ Object



7
8
9
10
# 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

Instance Method Details

#create_cache_for_field(value_field) ⇒ Object



32
33
34
35
# 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

#flushObject



39
40
41
42
# 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) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
# 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