Module: FileDefinitions

Includes:
Enumerable
Defined in:
lib/ar_loader/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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_lineObject

Returns the value of attribute current_line.



59
60
61
# File 'lib/ar_loader/file_definitions.rb', line 59

def current_line
  @current_line
end

#field_delimObject

Return the field delimiter used when splitting a line



80
81
82
# File 'lib/ar_loader/file_definitions.rb', line 80

def field_delim
  @field_delim || ','
end

#keyObject

Returns the value of attribute key.



58
59
60
# File 'lib/ar_loader/file_definitions.rb', line 58

def key
  @key
end

Class Method Details

.included(base) ⇒ Object



69
70
71
72
# File 'lib/ar_loader/file_definitions.rb', line 69

def self.included(base)
  base.extend(ClassMethods)
  subclasses << base
end

.subclassesObject



74
75
76
# File 'lib/ar_loader/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



86
87
88
89
90
91
# File 'lib/ar_loader/file_definitions.rb', line 86

def each( file )
  File::new(file).each_line do |line|
    parse( line )
    yield self
  end
end

#fieldsObject



93
94
95
96
# File 'lib/ar_loader/file_definitions.rb', line 93

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.



346
347
348
349
350
351
# File 'lib/ar_loader/file_definitions.rb', line 346

def file_set_field( file_name, field, old_value, new_value, regex = nil )

  map = {old_value => new_value}

  return file_set_field_by_map(file_name, field, map, regex)
end

#initialize(line = nil) ⇒ Object



64
65
66
67
# File 'lib/ar_loader/file_definitions.rb', line 64

def initialize( line  = nil )
  @key   = String.new
  parse(line) unless line.nil?
end

#to_sObject



98
99
100
# File 'lib/ar_loader/file_definitions.rb', line 98

def to_s
  fields.join(',')
end