Class: FixedWidth::Section
- Inherits:
-
Object
- Object
- FixedWidth::Section
- Defined in:
- lib/fixed_width/section.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#singular ⇒ Object
Returns the value of attribute singular.
Instance Method Summary collapse
- #column(name, length, options = {}) ⇒ Object
- #format(data) ⇒ Object
-
#initialize(name, options = {}) ⇒ Section
constructor
A new instance of Section.
- #match(raw_line) ⇒ Object
- #method_missing(method, *args) ⇒ Object
- #parse(line) ⇒ Object
- #spacer(length, spacer = nil) ⇒ Object
- #template(name) ⇒ Object
- #trap(&block) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Section
Returns a new instance of Section.
6 7 8 9 10 11 12 13 |
# File 'lib/fixed_width/section.rb', line 6 def initialize(name, ={}) @name = name @options = @columns = [] @trap = [:trap] @optional = [:optional] || false @singular = [:singular] || false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
76 77 78 |
# File 'lib/fixed_width/section.rb', line 76 def method_missing(method, *args) column(method, *args) end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
4 5 6 |
# File 'lib/fixed_width/section.rb', line 4 def columns @columns end |
#definition ⇒ Object
Returns the value of attribute definition.
3 4 5 |
# File 'lib/fixed_width/section.rb', line 3 def definition @definition end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/fixed_width/section.rb', line 4 def name @name end |
#optional ⇒ Object
Returns the value of attribute optional.
3 4 5 |
# File 'lib/fixed_width/section.rb', line 3 def optional @optional end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/fixed_width/section.rb', line 4 def @options end |
#singular ⇒ Object
Returns the value of attribute singular.
3 4 5 |
# File 'lib/fixed_width/section.rb', line 3 def singular @singular end |
Instance Method Details
#column(name, length, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fixed_width/section.rb', line 15 def column(name, length, ={}) if column_names_by_group([:group]).include?(name) raise FixedWidth::DuplicateColumnNameError.new("You have already defined a column named '#{name}' in the '#{[:group].inspect}' group.") end if column_names_by_group(nil).include?([:group]) raise FixedWidth::DuplicateGroupNameError.new("You have already defined a column named '#{[:group]}'; you cannot have a group and column of the same name.") end if group_names.include?(name) raise FixedWidth::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()) @columns << col col end |
#format(data) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/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) ⇒ Object
72 73 74 |
# File 'lib/fixed_width/section.rb', line 72 def match(raw_line) raw_line.nil? ? false : @trap.call(raw_line) end |
#parse(line) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fixed_width/section.rb', line 56 def parse(line) row = group_names.inject({}) {|h,g| h[g] = {}; h } cursor = 0 @columns.each do |c| unless c.name == :spacer assignee = c.group ? row[c.group] : row capture = line.mb_chars[cursor..cursor+c.length-1] || '' assignee[c.name] = c.parse(capture) end cursor += c.length end row end |
#spacer(length, spacer = nil) ⇒ Object
31 32 33 34 35 |
# File 'lib/fixed_width/section.rb', line 31 def spacer(length, spacer=nil) = {} [:padding] = spacer if spacer column(:spacer, length, ) end |
#template(name) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/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..merge(@options) end |
#trap(&block) ⇒ Object
37 38 39 |
# File 'lib/fixed_width/section.rb', line 37 def trap(&block) @trap = block end |