Class: FixedWidth::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/fixed_width/section.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @name     = name
  @options  = options
  @columns  = []
  @trap     = options[:trap]
  @optional = options[:optional] || false
  @singular = options[: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

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/fixed_width/section.rb', line 4

def columns
  @columns
end

#definitionObject

Returns the value of attribute definition.



3
4
5
# File 'lib/fixed_width/section.rb', line 3

def definition
  @definition
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/fixed_width/section.rb', line 4

def name
  @name
end

#optionalObject

Returns the value of attribute optional.



3
4
5
# File 'lib/fixed_width/section.rb', line 3

def optional
  @optional
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/fixed_width/section.rb', line 4

def options
  @options
end

#singularObject

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, options={})
  if column_names_by_group(options[:group]).include?(name)
    raise FixedWidth::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 FixedWidth::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 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(options))
  @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)
  options           = {}
  options[:padding] = spacer if spacer
  column(:spacer, length, options)
end

#template(name) ⇒ Object

Raises:

  • (ArgumentError)


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.options.merge(@options)
end

#trap(&block) ⇒ Object



37
38
39
# File 'lib/fixed_width/section.rb', line 37

def trap(&block)
  @trap = block
end