Module: FileDefinitions
- Includes:
- Enumerable
- Defined in:
- lib/datashift/file_definitions.rb
Overview
- Copyright
-
© Autotelik Media Ltd 2011
- Author
-
Tom Statter
- Date
-
Jan 2011
- License
-
MIT
- Details
-
This module acts as helpers for defining input/output file formats as classes.
It provides a simple interface to define a file structure - field by field.
By defining the structure, following methods and attributes are mixed in :
An attribute, with accessor for each field/column.
Parse a line, assigning values to each attribute.
Parse an instance of that file line by line, accepts a block in which data can be processed.
Method to split a file by field.
Method to perform replace operations on a file by field and value.
Either delimited or a fixed width definition can be created via macro-like class methods :
create_field_definition [field_list]
create_fixed_definition {field => range }
Member attributes, with getters and setters, can be added for each field defined above via class method :
create_field_attr_accessors
USAGE :
Create a class that contains definition of a file.
class ExampleFixedWith < FileDefinitionBase
create_fixed_definition(:name => (0..7), :value => (8..15), :ccy => (16..18), :dr_or_cr => (19..19) )
create_field_attr_accessors
end
class ExampleCSV < FileDefinitionBase
create_field_definition %w{abc def ghi jkl}
create_field_attr_accessors
end
Any instance can then be used to parse the defined file type, with each field or column value being assigned automatically to the associated instance variable.
line = '1,2,3,4'
x = ExampleCSV.new( line )
assert x.responds_to? :jkl
assert_equal x.abc, '1'
assert_equal x.jkl.to_i, 4
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#current_line ⇒ Object
Returns the value of attribute current_line.
-
#field_delim ⇒ Object
Return the field delimiter used when splitting a line.
-
#key ⇒ Object
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
-
#each(file) ⇒ Object
Parse each line of a file based on the field definition, yields self for each successive line.
- #fields ⇒ Object
-
#file_set_field(file_name, field, old_value, new_value, regex = nil) ⇒ Object
Open and parse a file, replacing a value in the specfied field.
- #initialize(line = nil) ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#current_line ⇒ Object
Returns the value of attribute current_line.
59 60 61 |
# File 'lib/datashift/file_definitions.rb', line 59 def current_line @current_line end |
#field_delim ⇒ Object
Return the field delimiter used when splitting a line
79 80 81 |
# File 'lib/datashift/file_definitions.rb', line 79 def field_delim @field_delim || ',' end |
#key ⇒ Object
Returns the value of attribute key.
58 59 60 |
# File 'lib/datashift/file_definitions.rb', line 58 def key @key end |
Class Method Details
.included(base) ⇒ Object
69 70 71 72 |
# File 'lib/datashift/file_definitions.rb', line 69 def self.included(base) base.extend(ClassMethods) subclasses << base end |
.subclasses ⇒ Object
74 75 76 |
# File 'lib/datashift/file_definitions.rb', line 74 def self.subclasses @subclasses ||= [] end |
Instance Method Details
#each(file) ⇒ Object
Parse each line of a file based on the field definition, yields self for each successive line
85 86 87 88 89 90 |
# File 'lib/datashift/file_definitions.rb', line 85 def each( file ) File.new(file).each_line do |line| parse( line ) yield self end end |
#fields ⇒ Object
92 93 94 95 |
# File 'lib/datashift/file_definitions.rb', line 92 def fields @fields = self.class.field_definition.collect { |f| instance_variable_get "@#{f}" } @fields end |
#file_set_field(file_name, field, old_value, new_value, regex = nil) ⇒ Object
Open and parse a file, replacing a value in the specfied field. Does not update the file itself. Does not write a new output file.
Returns :
1) full collection of updated lines
2) collection of file def objects (self), with updated value.
Finds values matching old_value, and also accepts an optional regex for more powerful matching strategies of values on the specfified field.
Replaces matches with new_value.
Accepts more than one field, if files is either and array of strings or comma seperated list of fields.
351 352 353 354 355 356 |
# File 'lib/datashift/file_definitions.rb', line 351 def file_set_field( file_name, field, old_value, new_value, regex = nil ) map = { old_value => new_value } file_set_field_by_map(file_name, field, map, regex) end |
#initialize(line = nil) ⇒ Object
64 65 66 67 |
# File 'lib/datashift/file_definitions.rb', line 64 def initialize( line = nil ) @key = '' parse(line) unless line.nil? end |
#to_s ⇒ Object
97 98 99 |
# File 'lib/datashift/file_definitions.rb', line 97 def to_s fields.join(',') end |