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, line 241
def directions
  @directions ||= []
end
Athena::Formats::Base.format → aString click to toggle source

Returns this class's format name.

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

Indicates whether this class supports direction.

# File lib/athena/formats.rb, line 249
def has_direction?(direction)
  directions.include?(direction)
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, line 258
def init(direction, *args)
  new.init(direction, *args)
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, line 271
def register_format(name = nil, relax = false)
  Formats.register(self, name, relax)
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, line 325
def convert(record)
  raise NotImplementedError, 'must be defined by sub-class'
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, line 342
def deferred?
  false
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, 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
raw? → true | false click to toggle source

Indicates whether output is written directly in convert.

# File lib/athena/formats.rb, line 333
def raw?
  false
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, 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

Private Instance Methods

init_in(config) click to toggle source

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
init_out(output = nil) click to toggle source

Initialize output format (with optional output).

# File lib/athena/formats.rb, line 395
def init_out(output = nil)
  @output = output
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, line 312
def parse(input)
  raise NotImplementedError, 'must be defined by sub-class'
end
wrap { ... } click to toggle source

Hook for wrapping the output generation in run.

# File lib/athena/formats.rb, line 403
def wrap
  yield
end