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)



26
27
28
# File 'lib/kotoshu/dictionary/repository.rb', line 26

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

Instance Attribute Details

#dictionariesHash (readonly)

Returns The dictionary storage.

Returns:

  • (Hash)

    The dictionary storage



21
22
23
# File 'lib/kotoshu/dictionary/repository.rb', line 21

def dictionaries
  @dictionaries
end

Class Method Details

.clearRepository

Clear the global repository.

Returns:



226
227
228
# File 'lib/kotoshu/dictionary/repository.rb', line 226

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



211
212
213
# File 'lib/kotoshu/dictionary/repository.rb', line 211

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

.instanceRepository

Global repository instance.

Examples:

Using the global repository

Repository.instance.register(:en_US, dict)

Returns:



188
189
190
# File 'lib/kotoshu/dictionary/repository.rb', line 188

def self.instance
  @instance ||= new
end

.keysArray<Symbol>

Get all keys from the global repository.

Returns:

  • (Array<Symbol>)

    All keys



233
234
235
# File 'lib/kotoshu/dictionary/repository.rb', line 233

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:



200
201
202
# File 'lib/kotoshu/dictionary/repository.rb', line 200

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



241
242
243
# File 'lib/kotoshu/dictionary/repository.rb', line 241

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



219
220
221
# File 'lib/kotoshu/dictionary/repository.rb', line 219

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



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

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



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

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



42
43
44
45
# File 'lib/kotoshu/dictionary/repository.rb', line 42

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

#clearself

Clear all dictionaries.

Returns:

  • (self)

    Self for chaining



85
86
87
88
# File 'lib/kotoshu/dictionary/repository.rb', line 85

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



134
135
136
137
138
# File 'lib/kotoshu/dictionary/repository.rb', line 134

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

  @dictionaries.each(&)
end

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

Iterate over registered keys.

Yields:

  • (key)

    Block to execute for each key

Returns:

  • (Enumerator)

    Enumerator if no block given



101
102
103
104
105
# File 'lib/kotoshu/dictionary/repository.rb', line 101

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

  @dictionaries.each_key(&)
end

#empty?Boolean

Check if the repository is empty.

Returns:

  • (Boolean)

    True if empty



126
127
128
# File 'lib/kotoshu/dictionary/repository.rb', line 126

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



161
162
163
164
165
# File 'lib/kotoshu/dictionary/repository.rb', line 161

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



52
53
54
# File 'lib/kotoshu/dictionary/repository.rb', line 52

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



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

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



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

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

#keysArray<Symbol>

Get all registered keys.

Returns:

  • (Array<Symbol>)

    All keys



93
94
95
# File 'lib/kotoshu/dictionary/repository.rb', line 93

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



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

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



38
39
40
41
# File 'lib/kotoshu/dictionary/repository.rb', line 38

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



64
65
66
# File 'lib/kotoshu/dictionary/repository.rb', line 64

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



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

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



117
118
119
# File 'lib/kotoshu/dictionary/repository.rb', line 117

def size
  @dictionaries.size
end

#to_hHash

Convert to hash.

Returns:

  • (Hash)

    Hash representation



170
171
172
# File 'lib/kotoshu/dictionary/repository.rb', line 170

def to_h
  @dictionaries.dup
end

#to_sString Also known as: inspect

String representation.

Returns:

  • (String)

    String representation



177
178
179
# File 'lib/kotoshu/dictionary/repository.rb', line 177

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



77
78
79
# File 'lib/kotoshu/dictionary/repository.rb', line 77

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

#valuesArray<Base>

Get all dictionaries.

Returns:

  • (Array<Base>)

    All dictionaries



110
111
112
# File 'lib/kotoshu/dictionary/repository.rb', line 110

def values
  @dictionaries.values
end