Method: Fech::Filing#map

Defined in:
lib/fech/filing.rb

#map(row, opts = {}) ⇒ Object

Maps a raw row to a labeled hash following any rules given in the filing’s Translator based on its version and row type. Finds the correct map for a given row, performs any matching Translations on the individual values, and returns either the entire dataset, or just those fields requested.

Parameters:

  • row (String, Regexp)

    a partial or complete name of the type of row desired

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :include (Array)

    list of field names that should be included in the returned hash



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fech/filing.rb', line 119

def map(row, opts={})
  data = Fech::Mapped.new(self, row.first)
  full_row_map = map_for(row.first)

  # If specific fields were asked for, return only those
  if opts[:include]
    row_map = full_row_map.select { |k| opts[:include].include?(k) }
  else
    row_map = full_row_map
  end

  # Inserts the row into data, performing any specified preprocessing
  # on individual cells along the way
  row_map.each_with_index do |field, index|
    value = row[full_row_map.index(field)]
    if translator
      translator.get_translations(:row => row.first,
          :version => filing_version, :action => :convert,
          :field => field).each do |translation|
        # User's Procs should be given each field's value as context
        value = translation[:proc].call(value)
      end
    end
    data[field] = value
  end

  # Performs any specified group preprocessing / combinations
  if translator
    combinations = translator.get_translations(:row => row.first,
          :version => filing_version, :action => :combine)
    row_hash = hash_zip(row_map, row) if combinations
    combinations.each do |translation|
      # User's Procs should be given the entire row as context
      value = translation[:proc].call(row_hash)
      field = translation[:field].source.gsub(/[\^\$]*/, "").to_sym
      data[field] = value
    end
  end
  data
end