Class: FixedWidth::Generator
- Inherits:
-
Object
- Object
- FixedWidth::Generator
- Defined in:
- lib/fixed_width/generator.rb
Instance Method Summary collapse
- #generate(data) ⇒ Object
-
#initialize(definition) ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize(definition) ⇒ Generator
Returns a new instance of Generator.
4 5 6 |
# File 'lib/fixed_width/generator.rb', line 4 def initialize(definition) @definition = definition end |
Instance Method Details
#generate(data) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/fixed_width/generator.rb', line 8 def generate(data) @builder = [] @definition.sections.each do |section| content = data[section.name] arrayed_content = content.is_a?(Array) ? content : [content] raise FixedWidth::RequiredSectionEmptyError.new("Required section '#{section.name}' was empty.") if (content.nil? || content.empty?) && !section.optional arrayed_content.each {|row| @builder << section.format(row) } end @builder.join("\n") end |