Class: DataMiner::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/data_miner/dictionary.rb

Overview

An easy way to translate data before importing it using an intermediate table.

Constant Summary collapse

DEFAULT_CASE_SENSITIVE =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#case_sensitiveTrueClass, FalseClass (readonly)

Whether to be case-sensitive with lookups. Defaults to false.

Returns:

  • (TrueClass, FalseClass)


30
31
32
# File 'lib/data_miner/dictionary.rb', line 30

def case_sensitive
  @case_sensitive
end

#key_nameString (readonly)

What field in the dictionary holds the lookup key.

In other words, the column we scan down to find an entry.

Returns:

  • (String)


13
14
15
# File 'lib/data_miner/dictionary.rb', line 13

def key_name
  @key_name
end

#sprintfString (readonly)

A sprintf-style format to be applied.

Returns:

  • (String)


22
23
24
# File 'lib/data_miner/dictionary.rb', line 22

def sprintf
  @sprintf
end

#urlString (readonly)

The URL of the dictionary. It must be a CSV.

Returns:

  • (String)


26
27
28
# File 'lib/data_miner/dictionary.rb', line 26

def url
  @url
end

#value_nameString (readonly)

What field in the dictionary holds the final value.

Returns:

  • (String)


18
19
20
# File 'lib/data_miner/dictionary.rb', line 18

def value_name
  @value_name
end

Instance Method Details

#lookup(value) ⇒ nil, String

Look up a translation for a value.

Returns:

  • (nil, String)


46
47
48
49
50
51
# File 'lib/data_miner/dictionary.rb', line 46

def lookup(value)
  normalized_value = normalize_for_comparison value
  if match = table.detect { |entry| entry[key_name] == normalized_value }
    match[value_name].to_s
  end
end