Class: Shoji::TextBase

Inherits:
Base
  • Object
show all
Defined in:
lib/shoji/text_base.rb

Direct Known Subclasses

CSV, TSV

Constant Summary collapse

ENCMAP =
Hash.new('n').merge({
  'UTF-8' => 'u',
  'SHIFT-JIS' => 's',
  'EUC-JP' => 'e',
  'CP932' => 's'
})

Class Method Summary collapse

Methods inherited from Base

#each, foreach_hash, valid_content?

Class Method Details

.foreach(filename, opts = {}, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/shoji/text_base.rb', line 12

def self.foreach(filename, opts = {}, &block)
  Shoji::UTF8File.convert filename do |path|
    limit = opts[:limit].to_i
    index = 0
    CSV.foreach(path, fastercsv_opts) do |row|
      block.call(row)
      index += 1
      break if (limit > 0 && limit <= index)
    end
  end
end

.row_size(filename, opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/shoji/text_base.rb', line 34

def self.row_size(filename, opts = {})
  enc = ENCMAP[Shoji::UTF8File.guess_encoding(filename)]
  index = 0
  CSV.foreach(filename, fastercsv_opts.merge({:encoding => enc})) do |row|
    index += 1
  end
  index
end

.rows(filename, opts = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/shoji/text_base.rb', line 27

def self.rows(filename, opts = {})
  rows = []
  self.foreach(filename, opts) do |row|
    rows << row
  end
  rows
end

.valid_file?(filename, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/shoji/text_base.rb', line 23

def self.valid_file?(filename, opts = {})
  Shoji::UTF8File.convert filename do |path|
  end
end