Class: Athena::Formats::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/athena/formats.rb

Overview

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

Direct Known Subclasses

DBM, Ferret, Lingo, MYSQL, PGSQL, Sisis, XML

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

The input format’s configuration hash.



278
279
280
# File 'lib/athena/formats.rb', line 278

def config
  @config
end

#outputObject (readonly)

The output format’s output target.



285
286
287
# File 'lib/athena/formats.rb', line 285

def output
  @output
end

#record_elementObject (readonly)

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



282
283
284
# File 'lib/athena/formats.rb', line 282

def record_element
  @record_element
end

Class Method Details

.directionsObject

call-seq:

Athena::Formats::Base.directions -> anArray

Returns an array of the directions supported by this class.



241
242
243
# File 'lib/athena/formats.rb', line 241

def directions
  @directions ||= []
end

.formatObject

call-seq:

Athena::Formats::Base.format -> aString

Returns this class’s format name.



233
234
235
# File 'lib/athena/formats.rb', line 233

def format
  @format ||= Formats.format_name(name)
end

.has_direction?(direction) ⇒ Boolean

call-seq:

Athena::Formats::Base.has_direction?(direction) -> true | false

Indicates whether this class supports direction.

Returns:

  • (Boolean)


249
250
251
# File 'lib/athena/formats.rb', line 249

def has_direction?(direction)
  directions.include?(direction)
end

.init(direction, *args) ⇒ Object

call-seq:

Athena::Formats::Base.init(direction, *args) -> aFormat

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



258
259
260
# File 'lib/athena/formats.rb', line 258

def init(direction, *args)
  new.init(direction, *args)
end

Instance Method Details

#convert(record) ⇒ Object

call-seq:

format.convert(record) -> aString | anArray | void

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.

Raises:

  • (NotImplementedError)


325
326
327
# File 'lib/athena/formats.rb', line 325

def convert(record)
  raise NotImplementedError, 'must be defined by sub-class'
end

#deferred?Boolean

call-seq:

format.deferred? -> true | false

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

Returns:

  • (Boolean)


342
343
344
# File 'lib/athena/formats.rb', line 342

def deferred?
  false
end

#init(direction, *args) ⇒ Object

call-seq:

format.init(direction, *args) -> format

Initializes format for direction with args (see #init_in and #init_out), while making sure that direction is actually supported by format. Returns format.



293
294
295
296
297
298
299
300
301
# 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

#parse(input) ⇒ Object

call-seq:

format.parse(input) { |record| ... } -> anInteger

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.

Raises:

  • (NotImplementedError)


312
313
314
# File 'lib/athena/formats.rb', line 312

def parse(input)
  raise NotImplementedError, 'must be defined by sub-class'
end

#raw?Boolean

call-seq:

format.raw? -> true | false

Indicates whether output is written directly in #convert.

Returns:

  • (Boolean)


333
334
335
# File 'lib/athena/formats.rb', line 333

def raw?
  false
end

#run(spec, input) ⇒ Object

call-seq:

format.run(spec, input) -> anInteger

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.



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# 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