Class: FixedWidth
- Inherits:
-
Object
- Object
- FixedWidth
- Defined in:
- lib/fixed_width/fixed_width.rb,
lib/fixed_width/column.rb,
lib/fixed_width/parser.rb,
lib/fixed_width/section.rb,
lib/fixed_width/generator.rb,
lib/fixed_width/definition.rb
Overview
DESCRIPTION:
A simple, clean DSL for describing, writing, and parsing fixed-width text files.
FEATURES:
-
Easy DSL syntax
-
Can parse and format fixed width files
-
Templated sections for reuse
For examples, see examples/*.rb or the README.
Defined Under Namespace
Classes: Column, ColumnMismatchError, Definition, DuplicateColumnNameError, DuplicateGroupNameError, DuplicateSectionNameError, FormattedStringExceedsLengthError, Generator, Parser, ParserError, RequiredSectionEmptyError, RequiredSectionNotFoundError, Section
Class Method Summary collapse
-
.define(name, options = {}) {|definition| ... } ⇒ Object
- name
-
a symbol to reference this file definition later [option] a hash of default options for all sub-elements and a block that defines the sections of the file.
-
.generate(definition_name, data) ⇒ Object
- data
-
nested hash describing the contents of the sections [definition_name] symbol
name
used indefine
.
-
.parse(file, definition_name) ⇒ Object
- file
-
IO object from which to read the fixed-width text records [definition_name] symbol
name
used indefine
.
-
.write(file, definition_name, data) ⇒ Object
- file
-
IO object to write the generated data [definition_name] symbol
name
used indefine
[data] nested hash describing the contents of the sections.
Class Method Details
.define(name, options = {}) {|definition| ... } ⇒ Object
- name
-
a symbol to reference this file definition later
- option
-
a hash of default options for all sub-elements
and a block that defines the sections of the file.
returns: Definition
instance for this file description.
31 32 33 34 35 36 |
# File 'lib/fixed_width/fixed_width.rb', line 31 def self.define(name, ={}) # yields definition definition = Definition.new() yield(definition) definitions[name] = definition definition end |
.generate(definition_name, data) ⇒ Object
- data
-
nested hash describing the contents of the sections
- definition_name
-
symbol
name
used indefine
returns: string of the transformed data
(into fixed-width records).
44 45 46 47 48 49 |
# File 'lib/fixed_width/fixed_width.rb', line 44 def self.generate(definition_name, data) definition = definition(definition_name) raise ArgumentError.new("Definition name '#{name}' was not found.") unless definition generator = Generator.new(definition) generator.generate(data) end |
.parse(file, definition_name) ⇒ Object
- file
-
IO object from which to read the fixed-width text records
- definition_name
-
symbol
name
used indefine
returns: parsed text records in a nested hash.
68 69 70 71 72 73 |
# File 'lib/fixed_width/fixed_width.rb', line 68 def self.parse(file, definition_name) definition = definition(definition_name) raise ArgumentError.new("Definition name '#{definition_name}' was not found.") unless definition parser = Parser.new(definition, file) parser.parse end |
.write(file, definition_name, data) ⇒ Object
- file
-
IO object to write the generated data
- definition_name
-
symbol
name
used indefine
- data
-
nested hash describing the contents of the sections
writes transformed data to file
object as fixed-width records.
58 59 60 |
# File 'lib/fixed_width/fixed_width.rb', line 58 def self.write(file, definition_name, data) file.write(generate(definition_name, data)) end |