Class: OFX::Configuration
- Inherits:
-
Object
- Object
- OFX::Configuration
- Defined in:
- lib/ofx_kit/configuration/core.rb,
lib/ofx_kit/configuration/date_parser.rb,
lib/ofx_kit/configuration/section_proxy.rb,
lib/ofx_kit/configuration/mapping_applicator.rb
Overview
Manages XML-to-Ruby field mappings used during OFX document parsing.
Mappings are split into two layers:
-
Core (core_mappingscore_mappings.yml): OFX-standard fields whose Ruby attribute names are referenced by name inside Base::Builder. These cannot be overridden.
-
User (field_mappingsfield_mappings.yml): convenience mappings that can be added to or replaced at runtime via #load_mappings or the configure block.
Defined Under Namespace
Modules: DateParser, MappingApplicator Classes: SectionProxy
Constant Summary collapse
- CORE_MAPPINGS_PATH =
File.join(__dir__, '..', 'mappings', 'core_mappings.yml')
- MAPPINGS_PATH =
File.join(__dir__, '..', 'mappings', 'field_mappings.yml')
- RAILS_MAPPINGS_PATH =
Conventional path for user mappings in a Rails application. Auto-loaded on boot when present. Ejected via rails generate ofx:eject.
'config/initializers/ofx_mappings.yml'
Instance Attribute Summary collapse
-
#default_currency ⇒ Object
Returns the value of attribute default_currency.
-
#multi_statement_warnings ⇒ Object
writeonly
Sets the attribute multi_statement_warnings.
Instance Method Summary collapse
-
#initialize(auto_load_path: File.expand_path(RAILS_MAPPINGS_PATH)) ⇒ Configuration
constructor
A new instance of Configuration.
-
#load_mappings(path) ⇒ Object
Merges additional field mappings from a YAML file into the user-layer configuration.
- #multi_statement_warnings? ⇒ Boolean
-
#xml_mappings_for(section_name) ⇒ Hash{String => String}
Returns the merged hash of XML tag → Ruby attribute mappings for the given section.
-
#xml_tag_for(section_name) ⇒ String?
Returns the OFX XML tag name corresponding to the given section identifier.
Constructor Details
#initialize(auto_load_path: File.expand_path(RAILS_MAPPINGS_PATH)) ⇒ Configuration
Returns a new instance of Configuration.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ofx_kit/configuration/core.rb', line 28 def initialize(auto_load_path: File.(RAILS_MAPPINGS_PATH)) @multi_statement_warnings = true @default_currency = 'USD' core = YAML.safe_load_file(CORE_MAPPINGS_PATH) @sections = core.fetch('SECTIONS', {}) @core_fields = core.fetch('FIELDS', {}) @section_to_tag = @sections.invert user = YAML.safe_load_file(MAPPINGS_PATH) @user_fields = user.fetch('FIELDS', {}) load_mappings(auto_load_path) if File.exist?(auto_load_path) end |
Instance Attribute Details
#default_currency ⇒ Object
Returns the value of attribute default_currency.
22 23 24 |
# File 'lib/ofx_kit/configuration/core.rb', line 22 def default_currency @default_currency end |
#multi_statement_warnings=(value) ⇒ Object (writeonly)
Sets the attribute multi_statement_warnings
21 22 23 |
# File 'lib/ofx_kit/configuration/core.rb', line 21 def multi_statement_warnings=(value) @multi_statement_warnings = value end |
Instance Method Details
#load_mappings(path) ⇒ Object
Merges additional field mappings from a YAML file into the user-layer configuration. The file must have a top-level FIELDS key. Core OFX fields cannot be overridden.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ofx_kit/configuration/core.rb', line 72 def load_mappings(path) raise ConfigurationError, "Mappings file not found: #{path}" unless File.exist?(path) raw = YAML.safe_load_file(path) raise ConfigurationError, 'Invalid mappings file: expected a Hash' unless raw.is_a?(Hash) fields = raw.fetch('FIELDS') do raise ConfigurationError, "Invalid mappings file: missing top-level 'FIELDS' key" end fields.each { |tag, mappings| merge_user_section(tag, mappings) } end |
#multi_statement_warnings? ⇒ Boolean
24 25 26 |
# File 'lib/ofx_kit/configuration/core.rb', line 24 def multi_statement_warnings? @multi_statement_warnings end |
#xml_mappings_for(section_name) ⇒ Hash{String => String}
Returns the merged hash of XML tag → Ruby attribute mappings for the given section. Core mappings take precedence; user mappings extend them.
60 61 62 63 64 65 |
# File 'lib/ofx_kit/configuration/core.rb', line 60 def xml_mappings_for(section_name) tag = xml_tag_for(section_name) return {} unless tag (@core_fields[tag] || {}).merge(@user_fields[tag] || {}) end |
#xml_tag_for(section_name) ⇒ String?
Returns the OFX XML tag name corresponding to the given section identifier.
52 53 54 |
# File 'lib/ofx_kit/configuration/core.rb', line 52 def xml_tag_for(section_name) @section_to_tag[section_name.to_s] end |