Class: Wukong::Store::FlatFileStore
- Defined in:
- lib/wukong/store/flat_file_store.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#filemode ⇒ Object
Returns the value of attribute filemode.
-
#filename ⇒ Object
Returns the value of attribute filename.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
delegates to
#save– writes the object to the file. -
#close ⇒ Object
Close the dump file.
- #each(&block) ⇒ Object
-
#file ⇒ Object
Open the timestamped file, ensuring its directory exists.
- #flush ⇒ Object
-
#initialize(options = {}) ⇒ FlatFileStore
constructor
filename_root: first part of name for files. -
#mkdir! ⇒ Object
Ensure the file’s directory exists.
-
#save(obj) ⇒ Object
write to the file.
-
#size ⇒ Object
returns the size of the current file.
-
#skip!(n_lines) ⇒ Object
Read ahead n_lines lines in the file.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ FlatFileStore
filename_root : first part of name for files
12 13 14 15 16 17 |
# File 'lib/wukong/store/flat_file_store.rb', line 12 def initialize ={} super self.filename = [:filename] or raise "Missing filename in #{self.class}" self.filemode = [:filemode] || 'r' skip!([:skip]) if [:skip] end |
Instance Attribute Details
#filemode ⇒ Object
Returns the value of attribute filemode.
7 8 9 |
# File 'lib/wukong/store/flat_file_store.rb', line 7 def filemode @filemode end |
#filename ⇒ Object
Returns the value of attribute filename.
7 8 9 |
# File 'lib/wukong/store/flat_file_store.rb', line 7 def filename @filename end |
Instance Method Details
#<<(obj) ⇒ Object
delegates to #save – writes the object to the file. Returns self for chaining on the stream.
81 82 83 84 |
# File 'lib/wukong/store/flat_file_store.rb', line 81 def <<(obj) save obj self end |
#close ⇒ Object
Close the dump file
51 52 53 54 |
# File 'lib/wukong/store/flat_file_store.rb', line 51 def close @file.close if @file @file = nil end |
#each(&block) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/wukong/store/flat_file_store.rb', line 22 def each &block file.each do |line| attrs = line.chomp.split("\t") next if attrs.blank? yield *attrs end end |
#file ⇒ Object
Open the timestamped file, ensuring its directory exists
44 45 46 47 48 |
# File 'lib/wukong/store/flat_file_store.rb', line 44 def file return @file if @file Log.info "Opening file #{filename} with mode #{filemode}" @file = File.open(filename, filemode) end |
#flush ⇒ Object
56 57 58 |
# File 'lib/wukong/store/flat_file_store.rb', line 56 def flush @file.flush if @file end |
#mkdir! ⇒ Object
Ensure the file’s directory exists
61 62 63 64 65 66 |
# File 'lib/wukong/store/flat_file_store.rb', line 61 def mkdir! dir = File.dirname(filename) return if File.directory?(dir) Log.info "Making directory #{dir}" FileUtils.mkdir_p dir end |
#save(obj) ⇒ Object
write to the file
69 70 71 72 |
# File 'lib/wukong/store/flat_file_store.rb', line 69 def save obj file.puts obj obj end |
#size ⇒ Object
returns the size of the current file
75 76 77 78 |
# File 'lib/wukong/store/flat_file_store.rb', line 75 def size return 0 if !@file File.size(filename) end |
#skip!(n_lines) ⇒ Object
Read ahead n_lines lines in the file
33 34 35 36 37 38 |
# File 'lib/wukong/store/flat_file_store.rb', line 33 def skip! n_lines Log.info "Skipping #{n_lines} in #{self.class}:#{filename}" n_lines.times do file.readline end end |