Class: Vedeu::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

Methods included from Store

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

Methods included from Registerable

included

Methods included from Common

#demodulize, #present?, #snake_case

Constructor Details

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

Returns a new instance of Vedeu::Repository.



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

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

Instance Attribute Details

#modelvoid (readonly)



19
20
21
# File 'lib/vedeu/repositories/repository.rb', line 19

def model
  @model
end

#storagevoid (readonly)



23
24
25
# File 'lib/vedeu/repositories/repository.rb', line 23

def storage
  @storage
end

Instance Method Details

#allArray<void>

Return all the registered models.



45
46
47
48
49
# File 'lib/vedeu/repositories/repository.rb', line 45

def all
  return storage.values if storage.is_a?(Hash)

  registered
end

#by_name(name) ⇒ 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)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vedeu/repositories/repository.rb', line 63

def by_name(name)
  name ||= Vedeu.focus

  if registered?(name)
    find(name)

  else
    null_model.new(name: name)

  end
end

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

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

Raises:



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

def find_or_create(name)
  if registered?(name)
    find(name)

  else
    Vedeu.log(type: :store,
              message: "Model (#{model}) not found, registering: '#{name}'")

    model.new(name).store
  end
end

#inspectString



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

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

#log_store(model) ⇒ String (private)



180
181
182
183
184
# File 'lib/vedeu/repositories/repository.rb', line 180

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

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

#registered?(name) ⇒ Boolean

Returns a boolean indicating whether the named model is registered.



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

def registered?(name)
  return false if name.nil? || name.empty?
  return false if empty?

  storage.include?(name)
end

#remove(name) ⇒ Hash|FalseClass Also known as: destroy, delete, deregister

Returns the storage with the named model removed, or false when the model does not exist.



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vedeu/repositories/repository.rb', line 141

def remove(name)
  return false if empty?

  if registered?(name)
    storage.delete(name)
    storage unless storage.is_a?(Set)

  else
    false

  end
end

#repositoryClass

Returns the repository class.



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

def repository
  self.class
end

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

Raises:



164
165
166
167
168
169
170
171
172
173
# File 'lib/vedeu/repositories/repository.rb', line 164

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

  log_store(model)

  storage[model.name] = model
end