class Athena::Formats::Base

Base class for all format classes. See Athena::Formats for more information.

Attributes

config[R]

The input format’s configuration hash.

output[R]

The output format’s output target.

record_element[R]

The input format’s “record element” (interpreted differently by each format).

Public Class Methods

Athena::Formats::Base.directions → anArray click to toggle source

Returns an array of the directions supported by this class.

    # File lib/athena/formats.rb
241 def directions
242   @directions ||= []
243 end
Athena::Formats::Base.format → aString click to toggle source

Returns this class’s format name.

    # File lib/athena/formats.rb
233 def format
234   @format ||= Formats.format_name(name)
235 end
Athena::Formats::Base.has_direction?(direction) → true | false click to toggle source

Indicates whether this class supports direction.

    # File lib/athena/formats.rb
249 def has_direction?(direction)
250   directions.include?(direction)
251 end
Athena::Formats::Base.init(direction, *args) → aFormat click to toggle source

Returns a new instance of this class for direction initialized with args (see init).

    # File lib/athena/formats.rb
258 def init(direction, *args)
259   new.init(direction, *args)
260 end

Protected Class Methods

Athena::Formats::Base.register_format(name = nil, relax = false) → anArray | nil click to toggle source

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
271 def register_format(name = nil, relax = false)
272   Formats.register(self, name, relax)
273 end

Public Instance Methods

convert(record) → aString | anArray | void click to toggle source

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
325 def convert(record)
326   raise NotImplementedError, 'must be defined by sub-class'
327 end
deferred? → true | false click to toggle source

Indicates whether output is to be deferred and only be written after all records have been converted (see run).

    # File lib/athena/formats.rb
342 def deferred?
343   false
344 end
init(direction, *args) → format click to toggle source

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
293 def init(direction, *args)
294   if self.class.has_direction?(direction)
295     send("init_#{direction}", *args)
296   else
297     raise DirectionMismatchError.new(direction, self.class.directions)
298   end
299 
300   self
301 end
raw? → true | false click to toggle source

Indicates whether output is written directly in convert.

    # File lib/athena/formats.rb
333 def raw?
334   false
335 end
run(spec, input) → anInteger click to toggle source

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
352 def run(spec, input)
353   parsed, block = nil, if raw?
354     lambda { |record| record.to(self) }
355   elsif deferred?
356     deferred = []
357     lambda { |record| deferred << record.to(self) }
358   else
359     lambda { |record| output.puts(record.to(self)) }
360   end
361 
362   wrap { parsed = spec.parse(input, &block) }
363 
364   if deferred?
365     deferred.flatten!; deferred.sort!; deferred.uniq!
366     output.puts(deferred)
367   end
368 
369   parsed
370 end

Private Instance Methods

init_in(config) click to toggle source

Initialize input format (with config).

    # File lib/athena/formats.rb
378 def init_in(config)
379   @config = config
380 
381   case @record_element = @config.delete(:__record_element)
382     when *@__record_element_ok__ || String
383       # fine!
384     when nil
385       raise NoRecordElementError, 'no record element specified'
386     else
387       raise IllegalRecordElementError, "illegal record element #{@record_element.inspect}"
388   end
389 end
init_out(output = nil) click to toggle source

Initialize output format (with optional output).

    # File lib/athena/formats.rb
395 def init_out(output = nil)
396   @output = output
397 end
parse(input) { |record| ... } → anInteger click to toggle source

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
312 def parse(input)
313   raise NotImplementedError, 'must be defined by sub-class'
314 end
wrap { ... } click to toggle source

Hook for wrapping the output generation in run.

    # File lib/athena/formats.rb
403 def wrap
404   yield
405 end