Class: Lingo::Database
- Inherits:
-
Object
show all
- Defined in:
- lib/lingo/database.rb,
lib/lingo/database/source.rb,
lib/lingo/database/crypter.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/show_progress.rb,
lib/lingo/database/source/multi_key.rb,
lib/lingo/database/source/key_value.rb,
lib/lingo/database/source/word_class.rb,
lib/lingo/database/source/single_word.rb,
lib/lingo/database/source/multi_value.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, ShowProgress, Source
Constant Summary
- FLD_SEP =
'|'
- IDX_REF =
'^'
- KEY_REF =
'*'
- SYS_KEY =
'~'
- IDX_REF_ESC =
Regexp.escape(IDX_REF)
- KEY_REF_ESC =
Regexp.escape(KEY_REF)
- INDEX_PATTERN =
%r{\A#{IDX_REF_ESC}\d+\z}
- BACKENDS =
[]
- BACKEND_BY_EXT =
{}
Instance Attribute Summary (collapse)
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
Constructor Details
- (Database) initialize(id, lingo)
A new instance of Database
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/lingo/database.rb', line 73
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.has_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
Returns the value of attribute backend
71
72
73
|
# File 'lib/lingo/database.rb', line 71
def backend
@backend
end
|
Class Method Details
+ (Object) open(*args, &block)
65
66
67
|
# File 'lib/lingo/database.rb', line 65
def open(*args, &block)
new(*args).open(&block)
end
|
+ (Object) register(klass, ext, prio = -1,, meth = true)
57
58
59
60
61
62
63
|
# File 'lib/lingo/database.rb', line 57
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)
126
127
128
129
130
131
132
133
134
|
# File 'lib/lingo/database.rb', line 126
def [](key)
val = _val(key) unless closed?
return unless val
val.split(FLD_SEP).map { |v|
v =~ INDEX_PATTERN ? _val(v) : v
}.compact.join(FLD_SEP).split(FLD_SEP)
end
|
- (Object) []=(key, val)
136
137
138
139
140
141
142
143
144
|
# File 'lib/lingo/database.rb', line 136
def []=(key, val)
return if closed?
val = @val[key].concat(val).sort!
val.uniq!
val = val.join(FLD_SEP)
@crypter ? _set(*@crypter.encode(key, val)) : _set(key, val)
end
|
- (Object) close
109
110
111
112
113
114
|
# File 'lib/lingo/database.rb', line 109
def close
_close unless closed?
@db = nil
self
end
|
- (Boolean) closed?
96
97
98
|
# File 'lib/lingo/database.rb', line 96
def closed?
!@db || _closed?
end
|
- (Object) each
122
123
124
|
# File 'lib/lingo/database.rb', line 122
def each
_each { |key, val| yield _encode!(key), _encode!(val) } unless closed?
end
|
- (Object) open
100
101
102
103
104
105
106
107
|
# File 'lib/lingo/database.rb', line 100
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
116
117
118
119
120
|
# File 'lib/lingo/database.rb', line 116
def to_h
hash = {}
each { |key, val| hash[key.freeze] = val }
hash
end
|
- (Object) warn(*msg)
146
147
148
|
# File 'lib/lingo/database.rb', line 146
def warn(*msg)
@lingo.warn(*msg)
end
|