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, Geometries::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.
-
#become(klass, attributes) ⇒ Class
included
from Common
private
Converts one class into another.
-
#boolean(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating the value was a boolean.
-
#boolean?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Boolean.
-
#by_name(name = nil) ⇒ void
Return the named model or a null object if not registered.
-
#current ⇒ String|NilClass
Return the model for the interface currently in focus.
- #each(&block) ⇒ Enumerator included from Store private
-
#empty? ⇒ Boolean
included
from Store
private
Return a boolean indicating whether the storage is empty.
-
#escape?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is an escape sequence object (e.g. Cells::Escape.).
-
#exists?(name) ⇒ Boolean
(also: #registered?)
included
from Store
private
Returns a boolean indicating whether the named model is registered.
-
#falsy?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value should be considered false.
-
#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.
-
#hash?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Hash.
- #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
-
#line_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::Line.
- #log_store(model) ⇒ String private
-
#numeric?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Fixnum.
-
#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|Boolean
(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! ⇒ void
(also: #reset)
included
from Storage
private
Remove all currently stored data for this repository.
-
#size ⇒ Fixnum
included
from Store
private
Return the number of entries stored.
-
#snake_case(klass) ⇒ String
included
from Common
private
Converts a class name to a lowercase snake case string.
-
#store(model, &block) ⇒ void
(also: #register, #add)
Stores the model instance by name in the repository of the model.
-
#stream_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::Stream.
-
#string?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Fixnum.
-
#truthy?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value should be considered true.
- #valid_model?(model) ⇒ Boolean private
-
#view_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::View.
Constructor Details
#initialize(model = nil, storage = {}) ⇒ Vedeu::Repositories::Repository
Returns a new instance of Vedeu::Repositories::Repository.
34 35 36 37 |
# File 'lib/vedeu/repositories/repository.rb', line 34 def initialize(model = nil, storage = {}) @model = model @storage = storage end |
Instance Attribute Details
#model ⇒ void (readonly)
This method returns an undefined value.
23 24 25 |
# File 'lib/vedeu/repositories/repository.rb', line 23 def model @model end |
#storage ⇒ void (readonly)
This method returns an undefined value.
27 28 29 |
# File 'lib/vedeu/repositories/repository.rb', line 27 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.
49 50 51 52 53 |
# File 'lib/vedeu/repositories/repository.rb', line 49 def all return storage.values if storage.is_a?(Hash) registered end |
#become(klass, attributes) ⇒ Class 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 one class into another.
#boolean(value) ⇒ 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 the value was a boolean.
#boolean?(value) ⇒ 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 the value is a Boolean.
#by_name(name = nil) ⇒ void
This method returns an undefined value.
Return the named model or a null object if not registered.
67 68 69 70 71 72 73 |
# File 'lib/vedeu/repositories/repository.rb', line 67 def by_name(name = nil) name = present?(name) ? name : Vedeu.focus return find(name) if registered?(name) null_model.new(null_attributes.merge(name: name)) if null_model? 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 |
#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.
#escape?(value) ⇒ 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 the value is an escape sequence object (e.g. Vedeu::Cells::Escape.)
#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.
#falsy?(value) ⇒ 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 the value should be considered false.
#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 |
# File 'lib/vedeu/repositories/repository.rb', line 107 def find_or_create(name) return find(name) if registered?(name) Vedeu.log(type: :store, message: "Model (#{model}) not found, " \ "registering: '#{name}'") model.new(name).store end |
#hash?(value) ⇒ 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 the value is a Hash.
#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
118 119 120 |
# File 'lib/vedeu/repositories/repository.rb', line 118 def inspect "<#{self.class.name}>" end |
#line_model? ⇒ 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 the model is a Views::Line.
#log_store(model) ⇒ String (private)
174 175 176 177 178 179 |
# File 'lib/vedeu/repositories/repository.rb', line 174 def log_store(model) type = registered?(model.name) ? :update : :create Vedeu.log(type: type, message: "#{model.class.name}: '#{model.name}'") end |
#numeric?(value) ⇒ 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 the value is a Fixnum.
#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.
127 128 129 130 131 132 |
# File 'lib/vedeu/repositories/repository.rb', line 127 def registered?(name) return false if absent?(name) return false if empty? storage.include?(name) end |
#remove(name) ⇒ Hash|Boolean Also known as: delete
Returns the storage with the named model removed, or false when the model does not exist.
139 140 141 142 143 144 145 |
# File 'lib/vedeu/repositories/repository.rb', line 139 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.
42 43 44 |
# File 'lib/vedeu/repositories/repository.rb', line 42 def repository self.class end |
#reset! ⇒ void Also known as: reset Originally defined in module Storage
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.
This method returns an undefined value.
Remove all currently stored data for this repository.
#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(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.
Converts a class name to a lowercase snake case string.
#store(model, &block) ⇒ void Also known as: register, add
If a block is given, store the model, return the model after yielding.
This method returns an undefined value.
Stores the model instance by name in the repository of the model.
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/vedeu/repositories/repository.rb', line 156 def store(model, &block) valid_model?(model) log_store(model) storage[model.name] = model yield if block_given? model end |
#stream_model? ⇒ 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 the model is a Views::Stream.
#string?(value) ⇒ 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 the value is a Fixnum.
#truthy?(value) ⇒ 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 the value should be considered true.
#valid_model?(model) ⇒ Boolean (private)
184 185 186 187 188 |
# File 'lib/vedeu/repositories/repository.rb', line 184 def valid_model?(model) fail Vedeu::Error::MissingRequired, "Cannot store model '#{model.class}' without a name " \ 'attribute.' unless present?(model.name) end |
#view_model? ⇒ 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 the model is a Views::View.