Class: Vedeu::Repository
- Inherits:
-
Object
- Object
- Vedeu::Repository
- Includes:
- Common, Registerable, Store
- Defined in:
- lib/vedeu/repositories/repository.rb
Overview
Provides common methods for accessing the various repositories Vedeu uses.
Direct Known Subclasses
Borders::Repository, Buffers::Repository, Cursors::Repository, Editor::Documents, Events::Repository, Geometry::Repository, Input::Keymaps, Menus::Repository, Models::Groups, Models::Interfaces
Instance Attribute Summary collapse
- #model ⇒ void readonly
- #storage ⇒ void readonly
Instance Method Summary collapse
-
#all ⇒ Array<void>
Return all the registered models.
-
#by_name(name) ⇒ void
Return the named model or a null object if not registered.
-
#current ⇒ String|NilClass
Return the model for the interface currently in focus.
-
#find(name) ⇒ Hash<String => Object>|NilClass
Find the model by name.
-
#find!(name) ⇒ Hash<String => Object>
Find the model attributes by name, raises an exception when the model cannot be found.
-
#find_or_create(name) ⇒ void
Find a model by name, registers the model by name when not found.
-
#initialize(model = nil, storage = {}) ⇒ Vedeu::Repository
constructor
Returns a new instance of Vedeu::Repository.
- #inspect ⇒ String
- #log_store(model) ⇒ String private
-
#registered?(name) ⇒ Boolean
Returns a boolean indicating whether the named model is registered.
-
#remove(name) ⇒ Hash|FalseClass
(also: #destroy, #delete, #deregister)
Returns the storage with the named model removed, or false when the model does not exist.
-
#repository ⇒ Class
Returns the repository class.
-
#store(model) ⇒ void
(also: #register, #add)
Stores the model instance by name in the repository of the model.
Methods included from Store
#each, #empty?, #exists?, #in_memory, #registered, #reset, #size
Methods included from Registerable
Methods included from Common
#demodulize, #present?, #snake_case
Constructor Details
#initialize(model = nil, storage = {}) ⇒ Vedeu::Repository
Returns a new instance of Vedeu::Repository.
30 31 32 33 |
# File 'lib/vedeu/repositories/repository.rb', line 30 def initialize(model = nil, storage = {}) @model = model @storage = storage end |
Instance Attribute Details
#model ⇒ void (readonly)
19 20 21 |
# File 'lib/vedeu/repositories/repository.rb', line 19 def model @model end |
#storage ⇒ void (readonly)
23 24 25 |
# File 'lib/vedeu/repositories/repository.rb', line 23 def storage @storage end |
Instance Method Details
#all ⇒ Array<void>
Return all the registered models.
45 46 47 48 49 |
# File 'lib/vedeu/repositories/repository.rb', line 45 def all return storage.values if storage.is_a?(Hash) registered end |
#by_name(name) ⇒ void
This method returns an undefined value.
Return the named model or a null object if not registered.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vedeu/repositories/repository.rb', line 63 def by_name(name) name ||= Vedeu.focus if registered?(name) find(name) else null_model.new(name: name) end end |
#current ⇒ String|NilClass
Return the model for the interface currently in focus.
78 79 80 |
# File 'lib/vedeu/repositories/repository.rb', line 78 def current find_or_create(Vedeu.focus) if Vedeu.focus end |
#find(name) ⇒ Hash<String => Object>|NilClass
Find the model by name.
86 87 88 |
# File 'lib/vedeu/repositories/repository.rb', line 86 def find(name) storage[name] end |
#find!(name) ⇒ Hash<String => Object>
Find the model attributes by name, raises an exception when the model cannot be found.
97 98 99 100 |
# File 'lib/vedeu/repositories/repository.rb', line 97 def find!(name) find(name) || fail(Vedeu::Error::ModelNotFound, "Cannot find model by name: '#{name}'") end |
#find_or_create(name) ⇒ void
This method returns an undefined value.
Find a model by name, registers the model by name when not found.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/vedeu/repositories/repository.rb', line 107 def find_or_create(name) if registered?(name) find(name) else Vedeu.log(type: :store, message: "Model (#{model}) not found, registering: '#{name}'") model.new(name).store end end |
#inspect ⇒ String
120 121 122 |
# File 'lib/vedeu/repositories/repository.rb', line 120 def inspect "<#{self.class.name}>" end |
#log_store(model) ⇒ String (private)
180 181 182 183 184 |
# File 'lib/vedeu/repositories/repository.rb', line 180 def log_store(model) type = registered?(model.name) ? :update : :create Vedeu.log(type: type, message: "#{model.class.name}: '#{model.name}'") end |
#registered?(name) ⇒ Boolean
Returns a boolean indicating whether the named model is registered.
129 130 131 132 133 134 |
# File 'lib/vedeu/repositories/repository.rb', line 129 def registered?(name) return false if name.nil? || name.empty? return false if empty? storage.include?(name) end |
#remove(name) ⇒ Hash|FalseClass Also known as: destroy, delete, deregister
Returns the storage with the named model removed, or false when the model does not exist.
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/vedeu/repositories/repository.rb', line 141 def remove(name) return false if empty? if registered?(name) storage.delete(name) storage unless storage.is_a?(Set) else false end end |
#repository ⇒ Class
Returns the repository class.
38 39 40 |
# File 'lib/vedeu/repositories/repository.rb', line 38 def repository self.class end |
#store(model) ⇒ void Also known as: register, add
This method returns an undefined value.
Stores the model instance by name in the repository of the model.
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/vedeu/repositories/repository.rb', line 164 def store(model) unless present?(model.name) fail Vedeu::Error::MissingRequired, "Cannot store model '#{model.class}' without a name attribute." end log_store(model) storage[model.name] = model end |