Base class for all format classes. See Athena::Formats for more information.
The input format's configuration hash.
The output format's output target.
The input format's “record element” (interpreted differently by each format).
Returns an array of the directions supported by this class.
# File lib/athena/formats.rb, line 241 def directions @directions ||= [] end
Returns this class's format name.
# File lib/athena/formats.rb, line 233 def format @format ||= Formats.format_name(name) end
Indicates whether this class supports direction
.
# File lib/athena/formats.rb, line 249 def has_direction?(direction) directions.include?(direction) end
Returns a new instance of this class for direction
initialized
with args
(see init).
# File lib/athena/formats.rb, line 258 def init(direction, *args) new.init(direction, *args) end
Shortcut for Athena::Formats.register(self, name, relax)
. Must
be called at the end of or after the class definition (in order to
determine the supported direction(s), the relevant instance methods must be
available).
# File lib/athena/formats.rb, line 271 def register_format(name = nil, relax = false) Formats.register(self, name, relax) end
Converts record
(Athena::Record) according to the format
represented by this class. The return value may be different for each
class; it is irrelevant when raw?
has been defined as true
.
NOTE: Must be implemented by the sub-class in order to function as output format.
# File lib/athena/formats.rb, line 325 def convert(record) raise NotImplementedError, 'must be defined by sub-class' end
Indicates whether output is to be deferred and only be written after all records have been converted (see run).
# File lib/athena/formats.rb, line 342 def deferred? false end
Initializes format for direction
with
args
(see init_in and
init_out), while making sure that
direction
is actually supported by format. Returns
format.
# File lib/athena/formats.rb, line 293 def init(direction, *args) if self.class.has_direction?(direction) send("init_#{direction}", *args) else raise DirectionMismatchError.new(direction, self.class.directions) end self end
Indicates whether output is written directly in convert.
# File lib/athena/formats.rb, line 333 def raw? false end
Runs the output generation for input format
spec
(Athena::Formats::Base) on input
. Outputs a
sorted and unique list of records when deferred? is true
.
Returns the return value of parse.
# File lib/athena/formats.rb, line 352 def run(spec, input) parsed, block = nil, if raw? lambda { |record| record.to(self) } elsif deferred? deferred = [] lambda { |record| deferred << record.to(self) } else lambda { |record| output.puts(record.to(self)) } end wrap { parsed = spec.parse(input, &block) } if deferred? deferred.flatten!; deferred.sort!; deferred.uniq! output.puts(deferred) end parsed end
Initialize input format (with config
).
# File lib/athena/formats.rb, line 378 def init_in(config) @config = config case @record_element = @config.delete(:__record_element) when *@__record_element_ok__ || String # fine! when nil raise NoRecordElementError, 'no record element specified' else raise IllegalRecordElementError, "illegal record element #{@record_element.inspect}" end end
Initialize output format (with optional output
).
# File lib/athena/formats.rb, line 395 def init_out(output = nil) @output = output end
Parses input
according to the format represented by this class
and passes each record to the block. Should return the number of
records parsed.
NOTE: Must be implemented by the sub-class in order to function as input format.
# File lib/athena/formats.rb, line 312 def parse(input) raise NotImplementedError, 'must be defined by sub-class' end
Hook for wrapping the output generation in run.
# File lib/athena/formats.rb, line 403 def wrap yield end