class FixedWidthGenerator::Section
Attributes
columns[R]
definition[RW]
name[R]
optional[RW]
options[R]
singular[RW]
Public Class Methods
new(name, options={})
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 6 def initialize(name, options={}) @name = name @options = options @columns = [] @trap = options[:trap] @optional = options[:optional] || false @singular = options[:singular] || false end
Public Instance Methods
column(name, length, options={})
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 15 def column(name, length, options={}) if column_names_by_group(options[:group]).include?(name) raise FixedWidthGenerator::DuplicateColumnNameError.new("You have already defined a column named '#{name}' in the '#{options[:group].inspect}' group.") end if column_names_by_group(nil).include?(options[:group]) raise FixedWidthGenerator::DuplicateGroupNameError.new("You have already defined a column named '#{options[:group]}'; you cannot have a group and column of the same name.") end if group_names.include?(name) raise FixedWidthGenerator::DuplicateGroupNameError.new("You have already defined a group named '#{name}'; you cannot have a group and column of the same name.") end col = Column.new(name, length, @options.merge(options)) @columns << col col end
format(data)
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 49 def format(data) @columns.map do |c| hash = c.group ? data[c.group] : data c.format(hash[c.name]) end.join end
match(raw_line)
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 69 def match(raw_line) raw_line.nil? ? false : @trap.call(raw_line) end
method_missing(method, *args)
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 73 def method_missing(method, *args) column(method, *args) end
parse(line)
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 56 def parse(line) line_data = line.unpack(unpacker) row = group_names.inject({}) {|h,g| h[g] = {}; h } @columns.each_with_index do |c, i| next if c.name == :spacer assignee = c.group ? row[c.group] : row assignee[c.name] = c.parse(line_data[i]) end row end
spacer(length, spacer=nil)
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 31 def spacer(length, spacer=nil) options = {} options[:padding] = spacer if spacer column(:spacer, length, options) end
template(name)
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 41 def template(name) template = @definition.templates[name] raise ArgumentError.new("Template '#{name}' not found as a known template.") unless template @columns += template.columns # Section options should trump template options @options = template.options.merge(@options) end
trap(&block)
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 37 def trap(&block) @trap = block end
Private Instance Methods
column_names_by_group(group)
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 79 def column_names_by_group(group) @columns.select{|c| c.group == group }.map(&:name) - [:spacer] end
group_names()
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 83 def group_names @columns.map(&:group).compact.uniq end
unpacker()
click to toggle source
# File lib/aba_generator/fixed_width/section.rb, line 87 def unpacker @unpacker ||= @columns.map(&:unpacker).join end