Class: Vedeu::Repositories::Repository
- Inherits:
-
Object
- Object
- Vedeu::Repositories::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::Repository, Events::Repository, Geometry::Repository, Groups::Repository, Input::Repository, Interfaces::Repository, Menus::Repository
Instance Attribute Summary collapse
- #model ⇒ void readonly
- #storage ⇒ void readonly
Instance Method Summary collapse
-
#absent?(variable) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether a variable is nil or empty.
-
#all ⇒ Array<void>
Return all the registered models.
-
#by_name(name = Vedeu.focus) ⇒ void
Return the named model or a null object if not registered.
-
#current ⇒ String|NilClass
Return the model for the interface currently in focus.
-
#demodulize(klass) ⇒ String
included
from Common
private
Removes the module part from the expression in the string.
- #each(&block) ⇒ Enumerator included from Store private
-
#empty? ⇒ Boolean
included
from Store
private
Return a boolean indicating whether the storage is empty.
-
#exists?(name) ⇒ Boolean
(also: #registered?)
included
from Store
private
Returns a boolean indicating whether the named model is registered.
-
#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.
- #in_memory ⇒ Hash included from Store private
-
#initialize(model = nil, storage = {}) ⇒ Vedeu::Repositories::Repository
constructor
Returns a new instance of Vedeu::Repositories::Repository.
- #inspect ⇒ String
- #log_store(model) ⇒ String private
-
#present?(variable) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether a variable has a useful value.
-
#registered ⇒ Array
included
from Store
private
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: #delete)
Returns the storage with the named model removed, or false when the model does not exist.
-
#repository ⇒ Class
Returns the repository class.
-
#reset ⇒ Array|Hash|Set
(also: #reset!, #clear)
included
from Store
private
Remove all currently stored data.
-
#size ⇒ Fixnum
included
from Store
private
Return the number of entries stored.
-
#snake_case(name) ⇒ String
included
from Common
private
Converts a class name to a lowercase snake case string.
-
#store(model) ⇒ void
(also: #register, #add)
Stores the model instance by name in the repository of the model.
Constructor Details
#initialize(model = nil, storage = {}) ⇒ Vedeu::Repositories::Repository
Returns a new instance of Vedeu::Repositories::Repository.
32 33 34 35 |
# File 'lib/vedeu/repositories/repository.rb', line 32 def initialize(model = nil, storage = {}) @model = model @storage = storage end |
Instance Attribute Details
#model ⇒ void (readonly)
This method returns an undefined value.
21 22 23 |
# File 'lib/vedeu/repositories/repository.rb', line 21 def model @model end |
#storage ⇒ void (readonly)
This method returns an undefined value.
25 26 27 |
# File 'lib/vedeu/repositories/repository.rb', line 25 def storage @storage end |
Instance Method Details
#absent?(variable) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether a variable is nil or empty.
#all ⇒ Array<void>
Return all the registered models.
47 48 49 50 51 |
# File 'lib/vedeu/repositories/repository.rb', line 47 def all return storage.values if storage.is_a?(Hash) registered end |
#by_name(name = Vedeu.focus) ⇒ void
This method returns an undefined value.
Return the named model or a null object if not registered.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/vedeu/repositories/repository.rb', line 65 def by_name(name = Vedeu.focus) return find(name) if present?(name) && registered?(name) attrs = if null_attributes.any? null_attributes.merge!(name: name) else { name: name } end null_model.new(attrs) end |
#current ⇒ String|NilClass
Return the model for the interface currently in focus.
84 85 86 |
# File 'lib/vedeu/repositories/repository.rb', line 84 def current find_or_create(Vedeu.focus) if Vedeu.focus end |
#demodulize(klass) ⇒ String Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes the module part from the expression in the string.
#each(&block) ⇒ Enumerator Originally defined in module Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
#empty? ⇒ Boolean Originally defined in module Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a boolean indicating whether the storage is empty.
#exists?(name) ⇒ Boolean Also known as: registered? Originally defined in module Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the named model is registered.
#find(name) ⇒ Hash<String => Object>|NilClass
Find the model by name.
92 93 94 |
# File 'lib/vedeu/repositories/repository.rb', line 92 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.
103 104 105 106 |
# File 'lib/vedeu/repositories/repository.rb', line 103 def find!(name) find(name) || fail(Vedeu::Error::ModelNotFound, "Cannot find model by name: '#{name}'".freeze) 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.
113 114 115 116 117 118 119 120 121 |
# File 'lib/vedeu/repositories/repository.rb', line 113 def find_or_create(name) return find(name) if registered?(name) Vedeu.log(type: :store, message: "Model (#{model}) not found, " \ "registering: '#{name}'".freeze) model.new(name).store end |
#in_memory ⇒ Hash Originally defined in module Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
#inspect ⇒ String
124 125 126 |
# File 'lib/vedeu/repositories/repository.rb', line 124 def inspect "<#{self.class.name}>".freeze end |
#log_store(model) ⇒ String (private)
179 180 181 182 183 184 |
# File 'lib/vedeu/repositories/repository.rb', line 179 def log_store(model) type = registered?(model.name) ? :update : :create Vedeu.log(type: type, message: "#{model.class.name}: '#{model.name}'".freeze) end |
#present?(variable) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether a variable has a useful value.
#registered ⇒ Array Originally defined in module Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a collection of the names of all the registered entities.
#registered?(name) ⇒ Boolean
Returns a boolean indicating whether the named model is registered.
133 134 135 136 137 138 |
# File 'lib/vedeu/repositories/repository.rb', line 133 def registered?(name) return false if absent?(name) return false if empty? storage.include?(name) end |
#remove(name) ⇒ Hash|FalseClass Also known as: delete
Returns the storage with the named model removed, or false when the model does not exist.
145 146 147 148 149 150 151 |
# File 'lib/vedeu/repositories/repository.rb', line 145 def remove(name) return false if empty? return false unless registered?(name) storage.delete(name) storage unless storage.is_a?(Set) end |
#repository ⇒ Class
Returns the repository class.
40 41 42 |
# File 'lib/vedeu/repositories/repository.rb', line 40 def repository self.class end |
#reset ⇒ Array|Hash|Set Also known as: reset!, clear Originally defined in module Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Remove all currently stored data.
#size ⇒ Fixnum Originally defined in module Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the number of entries stored.
#snake_case(name) ⇒ String Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts a class name to a lowercase snake case string.
#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.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/vedeu/repositories/repository.rb', line 161 def store(model) unless present?(model.name) fail Vedeu::Error::MissingRequired, "Cannot store model '#{model.class}' " \ 'without a name attribute.'.freeze end log_store(model) storage[model.name] = model end |