class Cranium::Transformation::Index

Public Class Methods

new() click to toggle source
# File lib/cranium/transformation/index.rb, line 3
def initialize
  @indexes = {}
end

Public Instance Methods

insert(field_name, options) click to toggle source
# File lib/cranium/transformation/index.rb, line 32
def insert(field_name, options)
  Cranium::DimensionManager.for(options[:table], [field_name]).insert(field_name, options[:record])
end
lookup(field_name, options) click to toggle source
# File lib/cranium/transformation/index.rb, line 9
def lookup(field_name, options)
  validate options

  cache = cache_for(options[:from_table], key_fields(options), field_name)

  if cache.has_key? keys(options)
    cache[keys(options)]
  elsif options.has_key? :if_not_found_then
    case options[:if_not_found_then]
      when Proc
        options[:if_not_found_then].call
      else
        options[:if_not_found_then]
    end
  elsif options.has_key? :if_not_found_then_insert
    cache[keys(options)] = Cranium::DimensionManager.for(options[:from_table], key_fields(options)).insert(field_name, default_value_record(options))
  else
    :not_found
  end
end
validate(options) click to toggle source
# File lib/cranium/transformation/index.rb, line 38
def validate(options)
  raise ArgumentError, "Cannot specify both :if_not_found_then and :if_not_found_then_insert options" if options.has_key? :if_not_found_then_insert and options.has_key? :if_not_found_then
end

Private Instance Methods

cache_for(table_name, key_fields, value_field) click to toggle source
# File lib/cranium/transformation/index.rb, line 57
def cache_for(table_name, key_fields, value_field)
  @indexes[[table_name, key_fields, value_field]] ||= Cranium::DimensionManager.for(table_name, key_fields).create_cache_for_field(value_field)
end
default_value_record(options) click to toggle source
# File lib/cranium/transformation/index.rb, line 46
def default_value_record(options)
  if options.has_key? :match
    key_values = options[:match]
  else
    key_values = {options[:match_column] => options[:to_value]}
  end
  options[:if_not_found_then_insert].merge(key_values)
end
key_fields(options) click to toggle source
# File lib/cranium/transformation/index.rb, line 63
def key_fields(options)
  if options.has_key? :match
    key_fields = options[:match].keys
  else
    key_fields = [options[:match_column]]
  end
  key_fields
end
keys(options) click to toggle source
# File lib/cranium/transformation/index.rb, line 74
def keys(options)
  if options.has_key? :match
    keys = options[:match].values
  else
    keys = [options[:to_value]]
  end
  keys
end