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
# File 'lib/vedeu/repositories/repository.rb', line 65

def by_name(name = Vedeu.focus)
  return find(name) if present?(name) && registered?(name)

  null_model.new(name: name)
end

#currentString|NilClass

Return the model for the interface currently in focus.

Returns:

  • (String|NilClass)


74
75
76
# File 'lib/vedeu/repositories/repository.rb', line 74

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)


82
83
84
# File 'lib/vedeu/repositories/repository.rb', line 82

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:



93
94
95
96
# File 'lib/vedeu/repositories/repository.rb', line 93

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)


103
104
105
106
107
108
109
110
111
# File 'lib/vedeu/repositories/repository.rb', line 103

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)


114
115
116
# File 'lib/vedeu/repositories/repository.rb', line 114

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

#log_store(model) ⇒ String (private)

Parameters:

  • model (void)

    A model instance.

Returns:

  • (String)


169
170
171
172
173
174
# File 'lib/vedeu/repositories/repository.rb', line 169

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)


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

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)


135
136
137
138
139
140
141
# File 'lib/vedeu/repositories/repository.rb', line 135

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:



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vedeu/repositories/repository.rb', line 151

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