class Shoji
meta class of excel, ods, csv and tsv processor.
Public Class Methods
new(filename, opts = {}) { |self| ... }
click to toggle source
# File lib/shoji_main.rb, line 23 def initialize(filename, opts = {}, &block) @filename = filename @opts = opts if block_given? yield(self) end end
Private Class Methods
binary_file?(filename)
click to toggle source
# File lib/shoji_main.rb, line 90 def self.binary_file?(filename) buf = nil File.open(filename, 'rb') do |f| buf = f.read(256) end buf.index("\0") ? true : false end
build_ext2class()
click to toggle source
# File lib/shoji_main.rb, line 57 def self.build_ext2class { '.XLS' => Shoji::Excel, '.CSV' => Shoji::CSV, '.TSV' => Shoji::TSV, '.ODS' => Shoji::ODS } end
class_from_params(*args)
click to toggle source
# File lib/shoji_main.rb, line 79 def self.class_from_params(*args) filename = args[0] opts = args[1] || {} case opts[:type] when nil, :auto then detect_class_from_filename_or_content(filename) when :excel, :xls then Shoji::Excel when :csv then Shoji::CSV when :tsv, :tabtext, :tab_text, :tab then Shoji::TSV else "Unexpected type value=#{opts[:type]}" end end
detect_class_from_content(filename)
click to toggle source
# File lib/shoji_main.rb, line 65 def self.detect_class_from_content(filename) if binary_file? filename # Try to check valid xls. return Shoji::Excel if Shoji::Excel.valid_file? filename else line = first_line(filename) case line when /\t/ then Shoji::TSV when /,/ then Shoji::CSV else nil end end end
detect_class_from_filename(filename)
click to toggle source
# File lib/shoji_main.rb, line 53 def self.detect_class_from_filename(filename) @@ext2class ||= build_ext2class @@ext2class[File.extname(filename).upcase] end
detect_class_from_filename_or_content(filename)
click to toggle source
# File lib/shoji_main.rb, line 48 def self.detect_class_from_filename_or_content(filename) klass = detect_class_from_filename(filename) return klass if klass detect_class_from_content(filename) end
first_line(filename)
click to toggle source
# File lib/shoji_main.rb, line 97 def self.first_line(filename) line = nil File.foreach(filename, encoding: 'BINARY') do |l| line = l break end line end
Public Instance Methods
foreach(opts = {}, &block)
click to toggle source
# File lib/shoji_main.rb, line 30 def foreach(opts = {}, &block) self.clcass.foreach(@filename, opts, &block) end
row_size(opts = {})
click to toggle source
# File lib/shoji_main.rb, line 40 def row_size(opts = {}) self.class.row_size(@filename, opts) end
rows(opts = {})
click to toggle source
# File lib/shoji_main.rb, line 43 def rows(opts = {}) self.class.rows(@filename, opts) end
valid_content?(opts = {})
click to toggle source
# File lib/shoji_main.rb, line 36 def valid_content?(opts = {}) self.class.valid_content?(@filename, opts) end
valid_file?(opts = {})
click to toggle source
# File lib/shoji_main.rb, line 33 def valid_file?(opts = {}) self.class.valid_file?(@filename, opts) end