Class: Shoji::UTF8File

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, type) ⇒ UTF8File

Returns a new instance of UTF8File.



20
21
22
23
24
25
26
27
# File 'lib/shoji/utf8_file.rb', line 20

def initialize(source, type)
  @filename = nil; @tempfile = nil
  case type
  when :filename then @filename = source
  when :tempfile then @tempfile = source
  else raise "Unexpected type=#{type}"
  end
end

Instance Attribute Details

#tempfileObject (readonly)

Returns the value of attribute tempfile.



19
20
21
# File 'lib/shoji/utf8_file.rb', line 19

def tempfile
  @tempfile
end

Class Method Details

.convert(filename, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shoji/utf8_file.rb', line 5

def self.convert(filename, &block)
  encoding = guess_encoding(filename)
  raise "Couldn't detect encoding" unless encoding
  fp = make_instance(filename, encoding)
  if block_given?
    begin
      yield(fp.path)
    ensure
      fp.delete
    end
  else
    fp
  end
end

.guess_encoding(filename) ⇒ Object



38
39
40
# File 'lib/shoji/utf8_file.rb', line 38

def self.guess_encoding(filename)
  NKF2ICONV[NKF.guess(read_lines(filename, 3))]
end

Instance Method Details

#deleteObject



32
33
34
35
36
37
# File 'lib/shoji/utf8_file.rb', line 32

def delete
  return false unless @tempfile
  @tempfile.close(true)
  @tempfile = nil
  true
end

#pathObject



28
29
30
31
# File 'lib/shoji/utf8_file.rb', line 28

def path
  return @filename if @filename
  @tempfile.path
end