Class: Fech::Mappings

Inherits:
Object
  • Object
show all
Defined in:
lib/fech/mappings.rb

Overview

Fech::Mappings loads a set of master mappings between labels and where their values can be found in Electronic Filings for various row types and versions. To access a map, call Mappings.for_row with the row_type, and optionally the version:

Mappings.for_row("SA", :version => 6.1)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ver = Fech::DEFAULT_VERSION) ⇒ Mappings

Returns a new instance of Mappings.



14
15
16
17
18
# File 'lib/fech/mappings.rb', line 14

def initialize(ver = Fech::DEFAULT_VERSION)
  @version  = ver
  @map      = load_map
  @cache    = {}
end

Instance Attribute Details

#mapObject

Returns the value of attribute map.



12
13
14
# File 'lib/fech/mappings.rb', line 12

def map
  @map
end

#versionObject

Returns the value of attribute version.



12
13
14
# File 'lib/fech/mappings.rb', line 12

def version
  @version
end

Class Method Details

.for_row(row_type, opts = {}) ⇒ Object

Given a row type, first find the entire block of maps for that row type. Then, use the filing’s version to choose which specific map set to use, and return it.

Parameters:

  • row_type (Symbol, String, Regex)

    the row whose map to find



42
43
44
45
46
# File 'lib/fech/mappings.rb', line 42

def self.for_row(row_type, opts={})
  opts[:version] ||= Fech::DEFAULT_VERSION
  map = key_by_regex(load_map, row_type)
  key_by_regex(map, opts[:version])
end

.key_by_regex(hash, label) ⇒ Object

Given a Hash whose keys are string representations of regular expressions, return the value whose key best matches the given label.

Parameters:

  • hash (Hash)

    a Hash with string regular expressions for keys

  • label (String, Symbol, Regexp)

    return the key that best matches this

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fech/mappings.rb', line 53

def self.key_by_regex(hash, label)
  label = label.source if label.is_a?(Regexp)
  
  # Try matching longer keys first, to ensure more accurate keys are
  # prioritized over less accurate ones.
  hash.keys.sort { |x, y| x.length <=> y.length }.reverse.each do |key|
    return hash[key] if Regexp.new(key, Regexp::IGNORECASE).match(label.to_s)
  end
  
  raise VersionError, "Attempted to access mapping that has not been generated (#{label}). " +
        "Supported keys match the format: #{hash.keys.join(', ')}"
end

.load_mapObject



33
34
35
# File 'lib/fech/mappings.rb', line 33

def self.load_map
  Fech::RENDERED_MAPS
end

Instance Method Details

#for_row(row_type) ⇒ Object

Returns a hash of mappings for row with given row_type

Parameters:

  • row_type (String, Symbol)

    the row type whose map to find



23
24
25
# File 'lib/fech/mappings.rb', line 23

def for_row(row_type)
  @cache[row_type] ||= self.class.for_row(row_type, :version => @version)
end

#load_mapObject

Returns the basic, default mappings hash by reading in a mappings file and saving the variable to the class’s context.



29
30
31
# File 'lib/fech/mappings.rb', line 29

def load_map
  self.class.load_map
end