Class: Cranium::Transformation::Index

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

Instance Method Summary collapse

Constructor Details

#initializeIndex

Returns a new instance of Index.



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

def initialize
  @indexes = {}
end

Instance Method Details

#insert(field_name, options) ⇒ Object



32
33
34
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
# 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