Class: Vedeu::Repositories::Repository

Inherits:
Object
  • Object
show all
Includes:
Common, Registerable, Store
Defined in:
lib/vedeu/repositories/repository.rb

Overview

Provides common methods for accessing the various repositories Vedeu uses.

Examples:

{ 'models' => [Model, Model, Model] }

{ 'models' => [Model] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model = nil, storage = {}) ⇒ Vedeu::Repositories::Repository

Returns a new instance of Vedeu::Repositories::Repository.

Parameters:

  • model (Class) (defaults to: nil)
  • storage (Class|Hash) (defaults to: {})


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

#modelvoid (readonly)

This method returns an undefined value.



21
22
23
# File 'lib/vedeu/repositories/repository.rb', line 21

def model
  @model
end

#storagevoid (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.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#allArray<void>

Return all the registered models.

Returns:

  • (Array<void>)

    An array containing each stored model.



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.

Examples:

# Fetch the cursor belonging to the interface of the same
# name.
Vedeu.cursors.by_name(:some_name)

# Fetch the names of the interfaces belonging to this group.
Vedeu.groups.by_name(name)

Parameters:

  • name (String|Symbol) (defaults to: Vedeu.focus)

    The name of the stored model.



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

#currentString|NilClass

Return the model for the interface currently in focus.

Returns:

  • (String|NilClass)


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.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (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.

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)

#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.

Returns:

  • (Boolean)

#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.

Parameters:

  • name (String|Symbol)

Returns:

  • (Boolean)

#find(name) ⇒ Hash<String => Object>|NilClass

Find the model by name.

Parameters:

  • name (String|Symbol)

Returns:

  • (Hash<String => Object>|NilClass)


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.

Parameters:

  • name (String|Symbol)

Returns:

  • (Hash<String => Object>)

Raises:



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.

Parameters:

  • name (String|Symbol)


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_memoryHash 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:

  • (Hash)

#inspectString

Returns:

  • (String)


124
125
126
# File 'lib/vedeu/repositories/repository.rb', line 124

def inspect
  "<#{self.class.name}>".freeze
end

#log_store(model) ⇒ String (private)

Parameters:

  • model (void)

    A model instance.

Returns:

  • (String)


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.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#registeredArray 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.

Returns:

  • (Array)

#registered?(name) ⇒ Boolean

Returns a boolean indicating whether the named model is registered.

Parameters:

  • name (String|Symbol)

Returns:

  • (Boolean)


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.

Parameters:

  • name (String|Symbol)

Returns:

  • (Hash|FalseClass)


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

#repositoryClass

Returns the repository class.

Returns:

  • (Class)


40
41
42
# File 'lib/vedeu/repositories/repository.rb', line 40

def repository
  self.class
end

#resetArray|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.

Returns:

  • (Array|Hash|Set)

#sizeFixnum 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.

Returns:

  • (Fixnum)

#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.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (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.

Parameters:

  • model (void)

    A model instance.

Raises:



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