Class: Vedeu::Repository
- Inherits:
-
Object
- Object
- Vedeu::Repository
- Includes:
- Enumerable, Common
- 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 ⇒ Object
readonly
Returns the value of attribute model.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
-
#all ⇒ Array|Hash|Set
Return the whole repository.
-
#current ⇒ String|NilClass
Return the model for the interface currently in focus.
- #each(&block) ⇒ Enumerator
-
#empty? ⇒ Boolean
Return a boolean indicating whether the storage is empty.
-
#find(name) ⇒ Hash
Find the model attributes by name.
-
#find_or_create(name) ⇒ void
(also: #by_name)
Find a model by name, registers the model by name if not found.
- #in_memory ⇒ Hash private
- #initialize(model = nil, storage = {}) ⇒ Vedeu::Repository constructor
- #log_type(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.
-
#reset ⇒ Array|Hash|Set
Reset the repository.
-
#store(model) ⇒ void
(also: #register)
Stores the model instance by name in the repository of the model.
-
#use(name) ⇒ |NilClass
Access a model by name.
Methods included from Common
Constructor Details
#initialize(model = nil, storage = {}) ⇒ Vedeu::Repository
24 25 26 27 |
# File 'lib/vedeu/repositories/repository.rb', line 24 def initialize(model = nil, storage = {}) @model = model @storage = storage end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
19 20 21 |
# File 'lib/vedeu/repositories/repository.rb', line 19 def model @model end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
19 20 21 |
# File 'lib/vedeu/repositories/repository.rb', line 19 def storage @storage end |
Instance Method Details
#all ⇒ Array|Hash|Set
Return the whole repository.
32 33 34 |
# File 'lib/vedeu/repositories/repository.rb', line 32 def all storage end |
#current ⇒ String|NilClass
Return the model for the interface currently in focus.
39 40 41 |
# File 'lib/vedeu/repositories/repository.rb', line 39 def current find_or_create(Vedeu.focus) if Vedeu.focus end |
#each(&block) ⇒ Enumerator
44 45 46 |
# File 'lib/vedeu/repositories/repository.rb', line 44 def each(&block) storage.each(&block) end |
#empty? ⇒ Boolean
Return a boolean indicating whether the storage is empty.
51 52 53 |
# File 'lib/vedeu/repositories/repository.rb', line 51 def empty? storage.empty? end |
#find(name) ⇒ Hash
Find the model attributes by name.
60 61 62 63 64 |
# File 'lib/vedeu/repositories/repository.rb', line 60 def find(name) storage.fetch(name) do fail ModelNotFound, "Cannot find model by name: '#{name}'" end end |
#find_or_create(name) ⇒ void Also known as: by_name
This method returns an undefined value.
Find a model by name, registers the model by name if not found.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/vedeu/repositories/repository.rb', line 70 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)
165 166 167 |
# File 'lib/vedeu/repositories/repository.rb', line 165 def in_memory {} end |
#log_type(model) ⇒ String (private)
170 171 172 173 174 175 176 177 178 |
# File 'lib/vedeu/repositories/repository.rb', line 170 def log_type(model) if registered?(model.name) :update else :create end end |
#registered ⇒ Array
Returns a collection of the names of all the registered entities.
85 86 87 88 89 90 91 92 |
# File 'lib/vedeu/repositories/repository.rb', line 85 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.
98 99 100 101 102 103 |
# File 'lib/vedeu/repositories/repository.rb', line 98 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.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/vedeu/repositories/repository.rb', line 110 def remove(name) return false if empty? if registered?(name) storage.delete(name) storage unless storage.is_a?(Set) else false end end |
#reset ⇒ Array|Hash|Set
Reset the repository.
129 130 131 |
# File 'lib/vedeu/repositories/repository.rb', line 129 def reset @storage = in_memory 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.
138 139 140 141 142 143 144 145 |
# File 'lib/vedeu/repositories/repository.rb', line 138 def store(model) fail MissingRequired, "Cannot store model '#{model.class}' without a " \ "name attribute." unless defined_value?(model.name) Vedeu.log(type: log_type(model), message: "#{model.class.name}: '#{model.name}'") storage[model.name] = model end |
#use(name) ⇒ |NilClass
Access a model by name.
152 153 154 155 156 157 158 159 160 |
# File 'lib/vedeu/repositories/repository.rb', line 152 def use(name) if registered?(name) find(name) else nil end end |