Class: Lingo::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/lingo/database.rb,
lib/lingo/database/source.rb,
lib/lingo/database/crypter.rb,
lib/lingo/database/progress.rb,
lib/lingo/database/gdbm_store.rb,
lib/lingo/database/hash_store.rb,
lib/lingo/database/sdbm_store.rb,
lib/lingo/database/libcdb_store.rb,
lib/lingo/database/source/key_value.rb,
lib/lingo/database/source/multi_key.rb,
lib/lingo/database/source/word_class.rb,
lib/lingo/database/source/multi_value.rb,
lib/lingo/database/source/single_word.rb

Overview

Die Klasse Database stellt eine einheitliche Schnittstelle auf Lingo-Datenbanken bereit. Die Identifizierung der Datenbank erfolgt über die ID der Datenbank, so wie sie in der Sprachkonfigurationsdatei de.lang unter language/dictionary/databases hinterlegt ist.

Das Lesen und Schreiben der Datenbank erfolgt über die Funktionen []() und []=().

Defined Under Namespace

Modules: GDBMStore, HashStore, LibCDBStore, SDBMStore Classes: Crypter, Progress, Source

Constant Summary

FLD_SEP =
'|'
KEY_REF =
'*'
SYS_KEY =
'~'
BACKENDS =
[]
BACKEND_BY_EXT =
{}

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Database) initialize(id, lingo)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lingo/database.rb', line 65

def initialize(id, lingo)
  @id, @lingo, @config, @db = id, lingo, lingo.database_config(id), nil

  @srcfile = Lingo.find(:dict, config['name'], relax: true)
  @crypter = config.key?('crypt') && Crypter.new

  @val = Hash.new { |h, k| h[k] = [] }

  begin
    @stofile = Lingo.find(:store, @srcfile)
    FileUtils.mkdir_p(File.dirname(@stofile))
  rescue SourceFileNotFoundError => err
    @stofile = skip_ext = err.id
    backend = backend_from_file(@stofile) unless err.name
  rescue NoWritableStoreError
    backend = HashStore
  end

  use_backend(backend, skip_ext)

  convert unless uptodate?
end

Instance Attribute Details

- (Object) backend (readonly)

Returns the value of attribute backend



88
89
90
# File 'lib/lingo/database.rb', line 88

def backend
  @backend
end

- (Object) config (readonly)

Returns the value of attribute config



88
89
90
# File 'lib/lingo/database.rb', line 88

def config
  @config
end

- (Object) lingo (readonly)

Returns the value of attribute lingo



88
89
90
# File 'lib/lingo/database.rb', line 88

def lingo
  @lingo
end

Class Method Details

+ (Object) open(*args, &block)



59
60
61
# File 'lib/lingo/database.rb', line 59

def open(*args, &block)
  new(*args).open(&block)
end

+ (Object) register(klass, ext, prio = -1,, meth = true)



51
52
53
54
55
56
57
# File 'lib/lingo/database.rb', line 51

def register(klass, ext, prio = -1, meth = true)
  BACKENDS.insert(prio, name = klass.name[/::(\w+)Store\z/, 1])
  Array(ext).each { |i| BACKEND_BY_EXT[i.insert(0, '.')] = name }

  klass.const_set(:EXT, ext)
  klass.class_eval('def store_ext; EXT; end', __FILE__, __LINE__) if meth
end

Instance Method Details

- (Object) [](key)



120
121
122
123
# File 'lib/lingo/database.rb', line 120

def [](key)
  val = _val(key) unless closed?
  val.split(FLD_SEP) if val
end

- (Object) []=(key, val)



125
126
127
128
129
130
131
132
133
# File 'lib/lingo/database.rb', line 125

def []=(key, val)
  return if closed?

  val = @val[key].concat(val)
  val.uniq!

  val = val.join(FLD_SEP)
  @crypter ? _set(*@crypter.encode(key, val)) : _set(key, val)
end

- (Object) close



103
104
105
106
107
108
# File 'lib/lingo/database.rb', line 103

def close
  _close unless closed?
  @db = nil

  self
end

- (Boolean) closed?



90
91
92
# File 'lib/lingo/database.rb', line 90

def closed?
  !@db || _closed?
end

- (Object) each



116
117
118
# File 'lib/lingo/database.rb', line 116

def each
  _each { |key, val| yield _encode!(key), _encode!(val) } unless closed?
end

- (Object) open



94
95
96
97
98
99
100
101
# File 'lib/lingo/database.rb', line 94

def open
  @db = _open if closed?
  block_given? ? yield(self) : self
rescue => err
  raise DatabaseError.new(:open, @stofile, err)
ensure
  close if @db && block_given?
end

- (Object) to_h



110
111
112
113
114
# File 'lib/lingo/database.rb', line 110

def to_h
  hash = {}
  each { |key, val| hash[key.freeze] = val }
  hash
end

- (Object) warn(*msg)



135
136
137
# File 'lib/lingo/database.rb', line 135

def warn(*msg)
  lingo.warn(*msg)
end