Class: NdrImport::NonTabular::Record
- Inherits:
-
Object
- Object
- NdrImport::NonTabular::Record
- Defined in:
- lib/ndr_import/non_tabular/record.rb
Overview
This class behaves like an array of NdrImport::NonTabular::Line elements that contains all the source lines of text that relate to a single record of data. It also encapsulates the logic that tabulates the data.
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Instance Method Summary collapse
- #<<(line) ⇒ Object
- #empty? ⇒ Boolean
-
#get_matches(column_mapping) ⇒ Object
returns an array of matches from within the captured lines.
-
#initialize ⇒ Record
constructor
A new instance of Record.
-
#not_a_record! ⇒ Object
Call this if it turns out that this is not a record.
-
#tabulate(mappings) ⇒ Object
Returns an array of “cells” for a given array of lines of a file that represent a single “row” of data.
Constructor Details
#initialize ⇒ Record
Returns a new instance of Record.
11 12 13 |
# File 'lib/ndr_import/non_tabular/record.rb', line 11 def initialize @lines = [] end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
9 10 11 |
# File 'lib/ndr_import/non_tabular/record.rb', line 9 def lines @lines end |
Instance Method Details
#<<(line) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/ndr_import/non_tabular/record.rb', line 15 def <<(line) return if line.removed line.in_a_record = true line.record_line_number = @lines.length @lines << line end |
#empty? ⇒ Boolean
22 23 24 |
# File 'lib/ndr_import/non_tabular/record.rb', line 22 def empty? @lines.empty? end |
#get_matches(column_mapping) ⇒ Object
returns an array of matches from within the captured lines
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ndr_import/non_tabular/record.rb', line 89 def get_matches(column_mapping) matching_lines = column_mapping.matching_lines(@lines) # loop through the specified line (or lines) matches = Array(@lines[matching_lines]).map do |line| line.captured_for(column_mapping.name) value = column_mapping.capture_value(line) line.matches_for(column_mapping.name, value) value end matches end |
#not_a_record! ⇒ Object
Call this if it turns out that this is not a record. All lines will be flagged accordingly.
28 29 30 |
# File 'lib/ndr_import/non_tabular/record.rb', line 28 def not_a_record! @lines.each { |line| line.in_a_record = false } end |
#tabulate(mappings) ⇒ Object
Returns an array of “cells” for a given array of lines of a file that represent a single “row” of data. Allowing the output to be mapped by the standard mapper.
Signature
tabulate(mappings)
Examples
If the YAML mapping is
---
- standard_mapping: nhsnumber
non_tabular_cell:
lines: 0
capture:
- !ruby/regexp /^D\|([^|]*).*/
- column: fulltextreport
non_tabular_cell:
lines: !ruby/range
begin: 1
end: -1
excl: false
capture: !ruby/regexp /^(?:R|\d+)\|(.*)$/i
join: \n
lines = [
"D|1111111111|...",
"R|This is a",
"1|multiline report"
]
tabulate(mappings)
# =>
[
"1111111111",
"This is a\nmultiline report"
]
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ndr_import/non_tabular/record.rb', line 71 def tabulate(mappings) cells = [] mappings.each do |column_mapping| begin matches = get_matches(column_mapping) # Join the non-blank lines together and add to the array of cells lines = matches.select do |value| column_mapping.preserve_blank_lines ? value : value.present? end cells << lines.join(column_mapping.join || '') rescue RegexpRange::PatternMatchError cells << nil end end cells end |