Class: Fech::Mappings
- Inherits:
-
Object
- Object
- Fech::Mappings
- 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
-
#map ⇒ Object
Returns the value of attribute map.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.for_row(row_type, opts = {}) ⇒ Object
Given a row type, first find the entire block of maps for that row type.
-
.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.
- .load_map ⇒ Object
Instance Method Summary collapse
-
#for_row(row_type) ⇒ Object
Returns a hash of mappings for row with given row_type.
-
#initialize(ver = Fech::DEFAULT_VERSION) ⇒ Mappings
constructor
A new instance of Mappings.
-
#load_map ⇒ Object
Returns the basic, default mappings hash by reading in a mappings file and saving the variable to the class’s context.
Constructor Details
#initialize(ver = Fech::DEFAULT_VERSION) ⇒ Mappings
Returns a new instance of Mappings.
13 14 15 16 17 |
# File 'lib/fech/mappings.rb', line 13 def initialize(ver = Fech::DEFAULT_VERSION) @version = ver @map = load_map @cache = {} end |
Instance Attribute Details
#map ⇒ Object
Returns the value of attribute map.
11 12 13 |
# File 'lib/fech/mappings.rb', line 11 def map @map end |
#version ⇒ Object
Returns the value of attribute version.
11 12 13 |
# File 'lib/fech/mappings.rb', line 11 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.
41 42 43 44 45 |
# File 'lib/fech/mappings.rb', line 41 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.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fech/mappings.rb', line 52 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 "Attempted to access mapping that has not been generated (#{label}). " + "Supported keys match the format: #{hash.keys.join(', ')}" end |
.load_map ⇒ Object
32 33 34 |
# File 'lib/fech/mappings.rb', line 32 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
22 23 24 |
# File 'lib/fech/mappings.rb', line 22 def for_row(row_type) @cache[row_type] ||= self.class.for_row(row_type, :version => @version) end |
#load_map ⇒ Object
Returns the basic, default mappings hash by reading in a mappings file and saving the variable to the class’s context.
28 29 30 |
# File 'lib/fech/mappings.rb', line 28 def load_map self.class.load_map end |