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/sdbm_store.rb,
lib/lingo/database/hash_store.rb,
lib/lingo/database/gdbm_store.rb,
lib/lingo/database/libcdb_store.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/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

#initialize(id, lingo) ⇒ Database

Returns a new instance of Database



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

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.nest { [] }

  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

#backendObject (readonly)

Returns the value of attribute backend



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

def backend
  @backend
end

#configObject (readonly)

Returns the value of attribute config



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

def config
  @config
end

#lingoObject (readonly)

Returns the value of attribute lingo



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

def lingo
  @lingo
end

Class Method Details

.open(*args, &block) ⇒ Object



61
62
63
# File 'lib/lingo/database.rb', line 61

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

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



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

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

#[](key) ⇒ Object



122
123
124
125
# File 'lib/lingo/database.rb', line 122

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

#[]=(key, val) ⇒ Object



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

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

#closeObject



105
106
107
108
109
110
# File 'lib/lingo/database.rb', line 105

def close
  _close unless closed?
  @db = nil

  self
end

#closed?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/lingo/database.rb', line 92

def closed?
  !@db || _closed?
end

#eachObject



118
119
120
# File 'lib/lingo/database.rb', line 118

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

#openObject



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

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

#to_hObject



112
113
114
115
116
# File 'lib/lingo/database.rb', line 112

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

#warn(*msg) ⇒ Object



137
138
139
# File 'lib/lingo/database.rb', line 137

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