Class: SO2DB::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/so2db/formatter.rb

Overview

Formats data from one stream into another stream.

Instance Method Summary collapse

Constructor Details

#initialize(path = '', delimiter = 11.chr.to_s) ⇒ Formatter

Infrastructure. Do not call this from your code.



33
34
35
36
37
# File 'lib/so2db/formatter.rb', line 33

def initialize(path = '', delimiter = 11.chr.to_s)
  @delimiter = delimiter
  @path = path
  @name = "row"
end

Instance Method Details

#file_nameObject



57
58
59
# File 'lib/so2db/formatter.rb', line 57

def file_name
  File.basename(@path)
end

#format(outstream) ⇒ Object

Formats a file and prints the formatted output to the outstream.

The output is performed via a ‘puts’ method.

Example:

>> f = get_formatter  # assumed to be provided to you
>> cmd = get_cmd      # some shell command that accepts piped input
>> IO.popen(cmd, 'r+') do |s| 
>>   formatter.format(s)
>>   s.close_write
>> end


50
51
52
53
54
55
# File 'lib/so2db/formatter.rb', line 50

def format(outstream)
  file = File.basename(@path, '.*')
  req_attrs = Models::Lookup::get_required_attrs(file)

  format_from_stream(File.open(@path), req_attrs, outstream)
end

#value_strObject

Returns a string containing the data type and individual fields provided through formatting. The fields are sorted alphabetically.

This method is useful for building SQL statements for the formatted data.

Example:

>> f = get_formatter_for_badges # formatter should be provided to you
>> puts f.value_str
=> badges(id,date,name,user_id)


70
71
72
73
74
75
76
77
78
# File 'lib/so2db/formatter.rb', line 70

def value_str
  file = File.basename(@path, '.*')
  o = Models::Lookup::find_class(file)

  table = o.table_name
  values = o.exported_fields.sort.join(",")

  "#{table}(#{values})"
end