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.
26 27 28 |
# File 'lib/kotoshu/dictionary/repository.rb', line 26 def initialize(dictionaries = {}) @dictionaries = dictionaries.dup end |
Instance Attribute Details
#dictionaries ⇒ Hash (readonly)
Returns The dictionary storage.
21 22 23 |
# File 'lib/kotoshu/dictionary/repository.rb', line 21 def dictionaries @dictionaries end |
Class Method Details
.clear ⇒ Repository
Clear the global repository.
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.
211 212 213 |
# File 'lib/kotoshu/dictionary/repository.rb', line 211 def self.get(key) instance.get(key) end |
.instance ⇒ Repository
Global repository instance.
188 189 190 |
# File 'lib/kotoshu/dictionary/repository.rb', line 188 def self.instance @instance ||= new end |
.keys ⇒ Array<Symbol>
Get all keys from the global repository.
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.
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.
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.
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.
55 56 57 |
# File 'lib/kotoshu/dictionary/repository.rb', line 55 def get(key) @dictionaries[key.to_sym] end |
#[]= ⇒ self
Register a dictionary.
43 44 45 46 |
# File 'lib/kotoshu/dictionary/repository.rb', line 43 def register(key, dictionary) @dictionaries[key.to_sym] = dictionary self end |
#add ⇒ self
Register a dictionary.
42 43 44 45 |
# File 'lib/kotoshu/dictionary/repository.rb', line 42 def register(key, dictionary) @dictionaries[key.to_sym] = dictionary self end |
#clear ⇒ self
Clear all dictionaries.
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.
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.
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.
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.
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.
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.
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.
68 69 70 |
# File 'lib/kotoshu/dictionary/repository.rb', line 68 def registered?(key) @dictionaries.key?(key.to_sym) end |
#keys ⇒ Array<Symbol>
Get all registered 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.
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.
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.
64 65 66 |
# File 'lib/kotoshu/dictionary/repository.rb', line 64 def registered?(key) @dictionaries.key?(key.to_sym) end |
#remove ⇒ Base?
Unregister a dictionary.
80 81 82 |
# File 'lib/kotoshu/dictionary/repository.rb', line 80 def unregister(key) @dictionaries.delete(key.to_sym) end |
#size ⇒ Integer Also known as: count, length
Get the number of registered dictionaries.
117 118 119 |
# File 'lib/kotoshu/dictionary/repository.rb', line 117 def size @dictionaries.size end |
#to_h ⇒ Hash
Convert to hash.
170 171 172 |
# File 'lib/kotoshu/dictionary/repository.rb', line 170 def to_h @dictionaries.dup end |
#to_s ⇒ String Also known as: inspect
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.
77 78 79 |
# File 'lib/kotoshu/dictionary/repository.rb', line 77 def unregister(key) @dictionaries.delete(key.to_sym) end |
#values ⇒ Array<Base>
Get all dictionaries.
110 111 112 |
# File 'lib/kotoshu/dictionary/repository.rb', line 110 def values @dictionaries.values end |