Class: KLookup::Database

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

Overview

Database access modules:

  • FlatFile

  • Unihan

Direct Known Subclasses

FlatFile, SQLite, Unihan

Defined Under Namespace

Classes: FlatFile, SQLite, Unihan

Class Method Summary collapse

Class Method Details

.open_resource(path, mod = 'klookup') ⇒ Object

Opens a resource.

Priority: LOOKUP_PATH environment variable, Gem load path.



52
53
54
55
56
57
58
59
60
61
# File 'lib/klookup/database.rb', line 52

def self.open_resource(path, mod='klookup')
  # Open a file
  path = resource_path(path, mod)
  file = open(path)
  if block_given?
    yield file
  else
    return file
  end
end

.resource_path(path, mod = 'klookup') ⇒ Object

Raises:

  • (Errno::ENOENT)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/klookup/database.rb', line 26

def self.resource_path(path, mod='klookup')
  # Choose a directory
  env=ENV['KLOOKUP_PATH']
  if env and env != ''
    dir=env+"/#{mod}"
  else
    begin
      gem mod
      dir=Gem.datadir(mod)
    rescue NameError
      # This is a fallback for older versions of RubyGems (0.8)
      begin
 require_gem mod
        dir=Gem.datadir(mod)
      rescue NameError
        raise IOError, 'Could not find resource %s' % path
      end
    end
  end
  raise Errno::ENOENT if Dir["#{dir}/#{path}"].empty?
  "#{dir}/#{path}"
end