Class: Kotoshu::Dictionary::Repository
- Inherits:
-
Object
- Object
- Kotoshu::Dictionary::Repository
- 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.
Instance Attribute Summary collapse
-
#dictionaries ⇒ Hash
readonly
The dictionary storage.
Class Method Summary collapse
-
.clear ⇒ Repository
Clear the global repository.
-
.get(key) ⇒ Base?
Get a dictionary from the global repository.
-
.instance ⇒ Repository
Global repository instance.
-
.keys ⇒ Array<Symbol>
Get all keys from the global repository.
-
.register(key, dictionary) ⇒ Repository
Register a dictionary in the global repository.
-
.registered?(key) ⇒ Boolean
Check if a key is registered in the global repository.
-
.unregister(key) ⇒ Base?
Unregister a dictionary from the global repository.
Instance Method Summary collapse
-
#[] ⇒ Base?
Get a dictionary by key.
-
#[]= ⇒ self
Register a dictionary.
-
#add ⇒ self
Register a dictionary.
-
#clear ⇒ self
Clear all dictionaries.
-
#each {|key, dictionary| ... } ⇒ Enumerator
Iterate over dictionaries.
-
#each_key {|key| ... } ⇒ Enumerator
Iterate over registered keys.
-
#empty? ⇒ Boolean
Check if the repository is empty.
-
#find_by_language(language_code) ⇒ Array<Base>
Find dictionaries by language code.
-
#get(key) ⇒ Base?
Get a dictionary by key.
-
#has_key? ⇒ Boolean
Check if a key is registered.
-
#initialize(dictionaries = {}) ⇒ Repository
constructor
Create a new repository.
-
#key? ⇒ Boolean
Check if a key is registered.
-
#keys ⇒ Array<Symbol>
Get all registered keys.
-
#merge(other) ⇒ self
Merge another repository into this one.
-
#register(key, dictionary) ⇒ self
Register a dictionary.
-
#registered?(key) ⇒ Boolean
Check if a key is registered.
-
#remove ⇒ Base?
Unregister a dictionary.
-
#size ⇒ Integer
(also: #count, #length)
Get the number of registered dictionaries.
-
#to_h ⇒ Hash
Convert to hash.
-
#to_s ⇒ String
(also: #inspect)
String representation.
-
#unregister(key) ⇒ Base?
Unregister a dictionary.
-
#values ⇒ Array<Base>
Get all dictionaries.
Constructor Details
#initialize(dictionaries = {}) ⇒ Repository
Create a new repository.
28 29 30 |
# File 'lib/kotoshu/dictionary/repository.rb', line 28 def initialize(dictionaries = {}) @dictionaries = dictionaries.dup end |
Instance Attribute Details
#dictionaries ⇒ Hash (readonly)
Returns The dictionary storage.
23 24 25 |
# File 'lib/kotoshu/dictionary/repository.rb', line 23 def dictionaries @dictionaries end |
Class Method Details
.clear ⇒ Repository
Clear the global repository.
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.
213 214 215 |
# File 'lib/kotoshu/dictionary/repository.rb', line 213 def self.get(key) instance.get(key) end |
.instance ⇒ Repository
Global repository instance.
190 191 192 |
# File 'lib/kotoshu/dictionary/repository.rb', line 190 def self.instance @instance ||= new end |
.keys ⇒ Array<Symbol>
Get all keys from the global repository.
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.
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.
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.
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.
57 58 59 |
# File 'lib/kotoshu/dictionary/repository.rb', line 57 def get(key) @dictionaries[key.to_sym] end |
#[]= ⇒ self
Register a dictionary.
45 46 47 48 |
# File 'lib/kotoshu/dictionary/repository.rb', line 45 def register(key, dictionary) @dictionaries[key.to_sym] = dictionary self end |
#add ⇒ self
Register a dictionary.
44 45 46 47 |
# File 'lib/kotoshu/dictionary/repository.rb', line 44 def register(key, dictionary) @dictionaries[key.to_sym] = dictionary self end |
#clear ⇒ self
Clear all dictionaries.
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.
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.
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.
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.
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.
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.
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.
70 71 72 |
# File 'lib/kotoshu/dictionary/repository.rb', line 70 def registered?(key) @dictionaries.key?(key.to_sym) end |
#keys ⇒ Array<Symbol>
Get all registered 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.
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.
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.
66 67 68 |
# File 'lib/kotoshu/dictionary/repository.rb', line 66 def registered?(key) @dictionaries.key?(key.to_sym) end |
#remove ⇒ Base?
Unregister a dictionary.
82 83 84 |
# File 'lib/kotoshu/dictionary/repository.rb', line 82 def unregister(key) @dictionaries.delete(key.to_sym) end |
#size ⇒ Integer Also known as: count, length
Get the number of registered dictionaries.
119 120 121 |
# File 'lib/kotoshu/dictionary/repository.rb', line 119 def size @dictionaries.size end |
#to_h ⇒ Hash
Convert to hash.
172 173 174 |
# File 'lib/kotoshu/dictionary/repository.rb', line 172 def to_h @dictionaries.dup end |
#to_s ⇒ String Also known as: inspect
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.
79 80 81 |
# File 'lib/kotoshu/dictionary/repository.rb', line 79 def unregister(key) @dictionaries.delete(key.to_sym) end |
#values ⇒ Array<Base>
Get all dictionaries.
112 113 114 |
# File 'lib/kotoshu/dictionary/repository.rb', line 112 def values @dictionaries.values end |