Class: Textfile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Textfile

options

  • :bufsiz - Passed to GNU sort to optimize performance.

  • :debug - Suppress deletion of temp files.

  • :lang - Collation sequence.

  • :logger - Logs shell commands and resulting ouput (default: STDOUT).



13
14
15
16
17
18
19
# File 'lib/textfile.rb', line 13

def initialize(path, options = {})
  @bufize = options[:bufsiz]
  @debug = options[:debug]
  @lang = options[:lang] || 'en_US.UTF-8'
  @logger = options[:logger] || Logger.new(STDOUT)
  @path = path
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



6
7
8
# File 'lib/textfile.rb', line 6

def debug
  @debug
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/textfile.rb', line 6

def logger
  @logger
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/textfile.rb', line 6

def path
  @path
end

#tmpdirObject

Returns the value of attribute tmpdir.



6
7
8
# File 'lib/textfile.rb', line 6

def tmpdir
  @tmpdir
end

Instance Method Details

#clearObject

Removes all records.



22
23
24
# File 'lib/textfile.rb', line 22

def clear
  sh "cat /dev/null > #{@path}"
end

#intersection(textfile) ⇒ Object

Removes records not present in other textfile.



27
28
29
# File 'lib/textfile.rb', line 27

def intersection(textfile)
  comm(textfile, '-12')
end

#merge(*textfiles) ⇒ Object

Merges the contents of other textfiles and returns self.



32
33
34
35
# File 'lib/textfile.rb', line 32

def merge(*textfiles)
  sh "cat #{textfiles.map(&:path).join(' ')} >> #{@path}"
  self.sort
end

#subtract(textfile) ⇒ Object

Remove records present in other textfile.



38
39
40
41
# File 'lib/textfile.rb', line 38

def subtract(textfile)
  # --nocheck-order, see https://bugzilla.redhat.com/show_bug.cgi?id=1001775
  comm(textfile, '--nocheck-order -23')
end