Class: Kotoshu::Dictionary::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/dictionary/repository.rb

Overview

Repository for managing multiple dictionary instances.

This class provides a centralized registry for dictionaries, allowing them to be registered and retrieved by key.

Examples:

Registering and retrieving dictionaries

repo = Repository.new
repo.register(:en_US, unix_dict)
repo.register(:custom, custom_dict)
repo.get(:en_US)  # => unix_dict

Using the global repository

Repository.register(:en_US, dict)
Repository.get(:en_US)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionaries = {}) ⇒ Repository

Create a new repository.

Parameters:

  • dictionaries (Hash) (defaults to: {})

    Initial dictionaries (optional)



28
29
30
# File 'lib/kotoshu/dictionary/repository.rb', line 28

def initialize(dictionaries = {})
  @dictionaries = dictionaries.dup
end

Instance Attribute Details

#dictionariesHash (readonly)

Returns The dictionary storage.

Returns:

  • (Hash)

    The dictionary storage



23
24
25
# File 'lib/kotoshu/dictionary/repository.rb', line 23

def dictionaries
  @dictionaries
end

Class Method Details

.clearRepository

Clear the global repository.

Returns:



228
229
230
# File 'lib/kotoshu/dictionary/repository.rb', line 228

def self.clear
  instance.clear
end

.get(key) ⇒ Base?

Get a dictionary from the global repository.

Examples:

Repository.get(:en_US)

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Base, nil)

    The dictionary or nil



213
214
215
# File 'lib/kotoshu/dictionary/repository.rb', line 213

def self.get(key)
  instance.get(key)
end

.instanceRepository

Global repository instance.

Examples:

Using the global repository

Repository.instance.register(:en_US, dict)

Returns:



190
191
192
# File 'lib/kotoshu/dictionary/repository.rb', line 190

def self.instance
  @instance ||= new
end

.keysArray<Symbol>

Get all keys from the global repository.

Returns:

  • (Array<Symbol>)

    All keys



235
236
237
# File 'lib/kotoshu/dictionary/repository.rb', line 235

def self.keys
  instance.keys
end

.register(key, dictionary) ⇒ Repository

Register a dictionary in the global repository.

Examples:

Repository.register(:en_US, dict)

Parameters:

  • key (Symbol, String)

    The key

  • dictionary (Base)

    The dictionary

Returns:



202
203
204
# File 'lib/kotoshu/dictionary/repository.rb', line 202

def self.register(key, dictionary)
  instance.register(key, dictionary)
end

.registered?(key) ⇒ Boolean

Check if a key is registered in the global repository.

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Boolean)

    True if the key exists



243
244
245
# File 'lib/kotoshu/dictionary/repository.rb', line 243

def self.registered?(key)
  instance.registered?(key)
end

.unregister(key) ⇒ Base?

Unregister a dictionary from the global repository.

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Base, nil)

    The removed dictionary or nil



221
222
223
# File 'lib/kotoshu/dictionary/repository.rb', line 221

def self.unregister(key)
  instance.unregister(key)
end

Instance Method Details

#[]Base?

Get a dictionary by key.

Examples:

repo.get(:en_US)

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Base, nil)

    The dictionary or nil if not found



57
58
59
# File 'lib/kotoshu/dictionary/repository.rb', line 57

def get(key)
  @dictionaries[key.to_sym]
end

#[]=self

Register a dictionary.

Examples:

repo.register(:en_US, unix_dict)

Parameters:

  • key (Symbol, String)

    The key to register under

  • dictionary (Base)

    The dictionary instance

Returns:

  • (self)

    Self for chaining



45
46
47
48
# File 'lib/kotoshu/dictionary/repository.rb', line 45

def register(key, dictionary)
  @dictionaries[key.to_sym] = dictionary
  self
end

#addself

Register a dictionary.

Examples:

repo.register(:en_US, unix_dict)

Parameters:

  • key (Symbol, String)

    The key to register under

  • dictionary (Base)

    The dictionary instance

Returns:

  • (self)

    Self for chaining



44
45
46
47
# File 'lib/kotoshu/dictionary/repository.rb', line 44

def register(key, dictionary)
  @dictionaries[key.to_sym] = dictionary
  self
end

#clearself

Clear all dictionaries.

Returns:

  • (self)

    Self for chaining



87
88
89
90
# File 'lib/kotoshu/dictionary/repository.rb', line 87

def clear
  @dictionaries.clear
  self
end

#each {|key, dictionary| ... } ⇒ Enumerator

Iterate over dictionaries.

Yields:

  • (key, dictionary)

    Each key and dictionary

Returns:

  • (Enumerator)

    Enumerator if no block given



136
137
138
139
140
# File 'lib/kotoshu/dictionary/repository.rb', line 136

def each(&block)
  return enum_for(:each) unless block_given?

  @dictionaries.each(&block)
end

#each_key {|key| ... } ⇒ Enumerator

Iterate over registered keys.

Yields:

  • (key)

    Block to execute for each key

Returns:

  • (Enumerator)

    Enumerator if no block given



103
104
105
106
107
# File 'lib/kotoshu/dictionary/repository.rb', line 103

def each_key(&block)
  return enum_for(:each_key) unless block_given?

  @dictionaries.each_key(&block)
end

#empty?Boolean

Check if the repository is empty.

Returns:

  • (Boolean)

    True if empty



128
129
130
# File 'lib/kotoshu/dictionary/repository.rb', line 128

def empty?
  @dictionaries.empty?
end

#find_by_language(language_code) ⇒ Array<Base>

Find dictionaries by language code.

Examples:

repo.find_by_language("en-US")

Parameters:

  • language_code (String)

    The language code

Returns:

  • (Array<Base>)

    Matching dictionaries



163
164
165
166
167
# File 'lib/kotoshu/dictionary/repository.rb', line 163

def find_by_language(language_code)
  @dictionaries.values.select do |dict|
    dict.language_code.casecmp(language_code).zero?
  end
end

#get(key) ⇒ Base?

Get a dictionary by key.

Examples:

repo.get(:en_US)

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Base, nil)

    The dictionary or nil if not found



54
55
56
# File 'lib/kotoshu/dictionary/repository.rb', line 54

def get(key)
  @dictionaries[key.to_sym]
end

#has_key?Boolean

Check if a key is registered.

Examples:

repo.registered?(:en_US)  # => true

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Boolean)

    True if the key exists



69
70
71
# File 'lib/kotoshu/dictionary/repository.rb', line 69

def registered?(key)
  @dictionaries.key?(key.to_sym)
end

#key?Boolean

Check if a key is registered.

Examples:

repo.registered?(:en_US)  # => true

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Boolean)

    True if the key exists



70
71
72
# File 'lib/kotoshu/dictionary/repository.rb', line 70

def registered?(key)
  @dictionaries.key?(key.to_sym)
end

#keysArray<Symbol>

Get all registered keys.

Returns:

  • (Array<Symbol>)

    All keys



95
96
97
# File 'lib/kotoshu/dictionary/repository.rb', line 95

def keys
  @dictionaries.keys
end

#merge(other) ⇒ self

Merge another repository into this one.

Examples:

repo1.merge(repo2)

Parameters:

  • other (Repository, Hash)

    The repository or hash to merge

Returns:

  • (self)

    Self for chaining



149
150
151
152
153
154
# File 'lib/kotoshu/dictionary/repository.rb', line 149

def merge(other)
  dicts_to_merge = other.is_a?(Repository) ? other.dictionaries : other

  @dictionaries.merge!(dicts_to_merge)
  self
end

#register(key, dictionary) ⇒ self

Register a dictionary.

Examples:

repo.register(:en_US, unix_dict)

Parameters:

  • key (Symbol, String)

    The key to register under

  • dictionary (Base)

    The dictionary instance

Returns:

  • (self)

    Self for chaining



40
41
42
43
# File 'lib/kotoshu/dictionary/repository.rb', line 40

def register(key, dictionary)
  @dictionaries[key.to_sym] = dictionary
  self
end

#registered?(key) ⇒ Boolean

Check if a key is registered.

Examples:

repo.registered?(:en_US)  # => true

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Boolean)

    True if the key exists



66
67
68
# File 'lib/kotoshu/dictionary/repository.rb', line 66

def registered?(key)
  @dictionaries.key?(key.to_sym)
end

#removeBase?

Unregister a dictionary.

Examples:

repo.unregister(:en_US)

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Base, nil)

    The removed dictionary or nil



82
83
84
# File 'lib/kotoshu/dictionary/repository.rb', line 82

def unregister(key)
  @dictionaries.delete(key.to_sym)
end

#sizeInteger Also known as: count, length

Get the number of registered dictionaries.

Returns:

  • (Integer)

    Dictionary count



119
120
121
# File 'lib/kotoshu/dictionary/repository.rb', line 119

def size
  @dictionaries.size
end

#to_hHash

Convert to hash.

Returns:

  • (Hash)

    Hash representation



172
173
174
# File 'lib/kotoshu/dictionary/repository.rb', line 172

def to_h
  @dictionaries.dup
end

#to_sString Also known as: inspect

String representation.

Returns:

  • (String)

    String representation



179
180
181
# File 'lib/kotoshu/dictionary/repository.rb', line 179

def to_s
  "Repository(size: #{size})"
end

#unregister(key) ⇒ Base?

Unregister a dictionary.

Examples:

repo.unregister(:en_US)

Parameters:

  • key (Symbol, String)

    The key

Returns:

  • (Base, nil)

    The removed dictionary or nil



79
80
81
# File 'lib/kotoshu/dictionary/repository.rb', line 79

def unregister(key)
  @dictionaries.delete(key.to_sym)
end

#valuesArray<Base>

Get all dictionaries.

Returns:

  • (Array<Base>)

    All dictionaries



112
113
114
# File 'lib/kotoshu/dictionary/repository.rb', line 112

def values
  @dictionaries.values
end