Class: Vedeu::Repositories::Repository Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Methods included from Store

#each, #empty?, #exists?, #in_memory, #registered, #size

Methods included from Storage

#reset!

Methods included from Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Methods included from Registerable

included

Constructor Details

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

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 new instance of Vedeu::Repositories::Repository.

Parameters:

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


36
37
38
39
# File 'lib/vedeu/repositories/repository.rb', line 36

def initialize(model = nil, storage = {})
  @model   = model
  @storage = storage
end

Instance Attribute Details

#modelvoid (readonly)

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.



25
26
27
# File 'lib/vedeu/repositories/repository.rb', line 25

def model
  @model
end

#storagevoid (readonly)

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.



29
30
31
# File 'lib/vedeu/repositories/repository.rb', line 29

def storage
  @storage
end

Instance Method Details

#allArray<void>

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 all the registered models.

Returns:

  • (Array<void>)

    An array containing each stored model.



51
52
53
54
55
# File 'lib/vedeu/repositories/repository.rb', line 51

def all
  return storage.values if hash?(storage)

  registered
end

#by_name(name = nil) ⇒ void

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.

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 (NilClass|Symbol|String) (defaults to: nil)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.



69
70
71
72
73
74
75
# File 'lib/vedeu/repositories/repository.rb', line 69

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

#currentString|NilClass

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 model for the interface currently in focus.

Returns:

  • (String|NilClass)


80
81
82
# File 'lib/vedeu/repositories/repository.rb', line 80

def current
  find_or_create(Vedeu.focus) if Vedeu.focus
end

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

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.

Find the model by name.

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

Returns:

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


88
89
90
# File 'lib/vedeu/repositories/repository.rb', line 88

def find(name)
  storage[name]
end

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

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.

Find the model attributes by name, raises an exception when the model cannot be found.

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

Returns:

  • (Hash<String => Object>)

Raises:



99
100
101
102
# File 'lib/vedeu/repositories/repository.rb', line 99

def find!(name)
  find(name) || raise(Vedeu::Error::ModelNotFound,
                      "Cannot find model by name: '#{name}'")
end

#find_or_create(name) ⇒ void

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.

Find a model by name, registers the model by name when not found.

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.



109
110
111
112
113
114
115
116
117
# File 'lib/vedeu/repositories/repository.rb', line 109

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

#inspectString

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:

  • (String)


120
121
122
# File 'lib/vedeu/repositories/repository.rb', line 120

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

#log_store(model) ⇒ String (private)

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:

  • model (void)

    A model instance.

Returns:

  • (String)


176
177
178
179
180
181
# File 'lib/vedeu/repositories/repository.rb', line 176

def log_store(model)
  type = registered?(model.name) ? :update : :create

  Vedeu.log(type:    type,
            message: "#{model.class.name}: '#{model.name}'")
end

#registered?(name) ⇒ Boolean

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 (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

Returns:



129
130
131
132
133
134
# File 'lib/vedeu/repositories/repository.rb', line 129

def registered?(name)
  return false if absent?(name)
  return false if empty?

  storage.include?(name)
end

#remove(name) ⇒ Hash|Boolean Also known as: delete

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 the storage with the named model removed, or false when the model does not exist.

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

Returns:



141
142
143
144
145
146
147
# File 'lib/vedeu/repositories/repository.rb', line 141

def remove(name)
  return false if empty?
  return false unless registered?(name)

  storage.delete(name)
  storage unless storage.is_a?(Set)
end

#repositoryClass

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 the repository class.

Returns:

  • (Class)


44
45
46
# File 'lib/vedeu/repositories/repository.rb', line 44

def repository
  self.class
end

#store(model, &block) ⇒ void Also known as: register, add

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.

Note:

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.

Parameters:

  • model (void)

    A model instance.

Raises:



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/vedeu/repositories/repository.rb', line 158

def store(model, &block)
  valid_model?(model)

  log_store(model)

  storage[model.name] = model

  yield if block_given?

  model
end

#valid_model?(model) ⇒ Boolean (private)

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:

  • model (void)

    A model instance.

Returns:

Raises:



186
187
188
189
190
# File 'lib/vedeu/repositories/repository.rb', line 186

def valid_model?(model)
  raise Vedeu::Error::MissingRequired,
        "Cannot store model '#{model.class}' without a name " \
        'attribute.' unless present?(model.name)
end