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, Buffers, Cursors, EventsRepository, Geometries, Groups, InterfacesRepository, Keymaps, Menus
Instance Attribute Summary collapse
- #model ⇒ void readonly
- #storage ⇒ void readonly
Class Method Summary collapse
Instance Method Summary collapse
- #by_name(name) ⇒ void
-
#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 if the model cannot be found.
-
#find_or_create(name) ⇒ void
Find a model by name, registers the model by name if not found.
- #in_memory ⇒ Hash private
-
#initialize(model = nil, storage = {}) ⇒ Vedeu::Repository
constructor
Returns a new instance of Vedeu::Repository.
- #inspect ⇒ String
- #log_store(model) ⇒ String private
-
#registered ⇒ Array
Returns a collection of the names of all the registered entities.
-
#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 if the model does not exist.
-
#repository ⇒ Class
Returns the repository class.
-
#store(model) ⇒ void
(also: #register)
Stores the model instance by name in the repository of the model.
Methods included from Store
#each, #empty?, #exists?, #reset, #size
Methods included from Registerable
Methods included from Common
Constructor Details
#initialize(model = nil, storage = {}) ⇒ Vedeu::Repository
Returns a new instance of Vedeu::Repository.
39 40 41 42 |
# File 'lib/vedeu/repositories/repository.rb', line 39 def initialize(model = nil, storage = {}) @model = model @storage = storage end |
Instance Attribute Details
#model ⇒ void (readonly)
This method returns an undefined value.
19 20 21 |
# File 'lib/vedeu/repositories/repository.rb', line 19 def model @model end |
#storage ⇒ void (readonly)
This method returns an undefined value.
23 24 25 |
# File 'lib/vedeu/repositories/repository.rb', line 23 def storage @storage end |
Class Method Details
.register(model = nil, storage = {}) ⇒ Vedeu::Repository
28 29 30 31 32 |
# File 'lib/vedeu/repositories/repository.rb', line 28 def self.register(model = nil, storage = {}) new(model, storage).tap do |klass| Vedeu::Repositories.register(klass.repository) end end |
Instance Method Details
#by_name(name) ⇒ void
This method returns an undefined value.
53 54 55 56 57 58 59 60 61 |
# File 'lib/vedeu/repositories/repository.rb', line 53 def by_name(name) if registered?(name) find(name) else null_model.new(name) end end |
#current ⇒ String|NilClass
Return the model for the interface currently in focus.
66 67 68 |
# File 'lib/vedeu/repositories/repository.rb', line 66 def current find_or_create(Vedeu.focus) if Vedeu.focus end |
#find(name) ⇒ Hash<String => Object>|NilClass
Find the model by name.
74 75 76 |
# File 'lib/vedeu/repositories/repository.rb', line 74 def find(name) storage[name] end |
#find!(name) ⇒ Hash<String => Object>
Find the model attributes by name, raises an exception if the model cannot be found.
84 85 86 |
# File 'lib/vedeu/repositories/repository.rb', line 84 def find!(name) find(name) || fail(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 if not found.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/vedeu/repositories/repository.rb', line 92 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 |
#in_memory ⇒ Hash (private)
171 172 173 |
# File 'lib/vedeu/repositories/repository.rb', line 171 def in_memory {} end |
#inspect ⇒ String
105 106 107 |
# File 'lib/vedeu/repositories/repository.rb', line 105 def inspect "<#{self.class.name}: #{registered.inspect}>" end |
#log_store(model) ⇒ String (private)
176 177 178 179 180 |
# File 'lib/vedeu/repositories/repository.rb', line 176 def log_store(model) type = registered?(model.name) ? :update : :create Vedeu.log(type: type, message: "#{model.class.name}: '#{model.name}'") end |
#registered ⇒ Array
Returns a collection of the names of all the registered entities.
112 113 114 115 116 117 118 119 |
# File 'lib/vedeu/repositories/repository.rb', line 112 def registered return [] if empty? return storage.keys if storage.is_a?(Hash) return storage.to_a if storage.is_a?(Set) storage end |
#registered?(name) ⇒ Boolean
Returns a boolean indicating whether the named model is registered.
125 126 127 128 129 130 |
# File 'lib/vedeu/repositories/repository.rb', line 125 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 if the model does not exist.
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/vedeu/repositories/repository.rb', line 137 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.
47 48 49 |
# File 'lib/vedeu/repositories/repository.rb', line 47 def repository self.class # .name end |
#store(model) ⇒ void Also known as: register
This method returns an undefined value.
Stores the model instance by name in the repository of the model.
158 159 160 161 162 163 164 165 |
# File 'lib/vedeu/repositories/repository.rb', line 158 def store(model) fail MissingRequired, "Cannot store model '#{model.class}' without a " \ 'name attribute.' unless defined_value?(model.name) log_store(model) storage[model.name] = model end |