Class: DriveTime::FieldExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/drive_time/field_expander.rb

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ FieldExpander

Returns a new instance of FieldExpander.



9
10
11
# File 'lib/drive_time/field_expander.rb', line 9

def initialize(loader)
  @loader = loader
end

Instance Method Details

#expand(value, model_key) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/drive_time/field_expander.rb', line 13

def expand(value, model_key)
  filename = model_key
  # Check for token
  match = /\[(.*?)\]/.match(value)
  # Expand token into file
  # Is there a different filename defined in hard brackets [file_name]
  if match
    filename = match[1]
    value = value.split('[').first
  end

  if value == 'expand_file'
    file = @loader.load_file_direct(filename+'.txt');
    if file.blank?
      raise TokenExpansionError, "Missing file named: #{filename} when expanding from value: #{value} in model: #{model_key}"
    end
    value = expand_file(file)
  elsif value == 'expand_spreadsheet'
    spreadsheet = @loader.load_spreadsheet_direct(filename)
    if spreadsheet.blank?
      raise TokenExpansionError, "Missing spreadsheet named: #{filename} when expanding from value: #{value} in model: #{model_key}"
    end
    # Use first Worksheet
    worksheet = spreadsheet.worksheets[0]
    value = expand_worksheet(worksheet)
  else
    raise TokenExpansionError, "Don't know how to expand the value #{value} for model: #{model_key}"
  end
  return value
end