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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Store

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

Methods included from Registerable

included

Methods included from Common

#defined_value?

Constructor Details

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

Returns a new instance of Vedeu::Repository.

Parameters:

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


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

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

Instance Attribute Details

#modelvoid (readonly)

This method returns an undefined value.



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

def model
  @model
end

#storagevoid (readonly)

This method returns an undefined value.



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

def storage
  @storage
end

Class Method Details

.register(model = nil, storage = {}) ⇒ Vedeu::Repository

Parameters:

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

Returns:



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

def self.register(model = nil, storage = {})
  new(model, storage).tap do |klass|
    Vedeu::Repositories.register(klass.repository)
  end
end

Instance Method Details

#by_name(name) ⇒ void

This method returns an undefined value.

Parameters:

  • name (String)

    The name of the stored model.



53
54
55
56
57
58
59
60
61
# File 'lib/vedeu/repositories/repository.rb', line 53

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

  else
    null_model.new(name)

  end
end

#currentString|NilClass

Return the model for the interface currently in focus.

Returns:

  • (String|NilClass)


66
67
68
# File 'lib/vedeu/repositories/repository.rb', line 66

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

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

Find the model by name.

Parameters:

  • name (String)

Returns:

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


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

def find(name)
  storage[name]
end

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

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

Parameters:

  • name (String)

Returns:

  • (Hash<String => Object>)

Raises:

  • (ModelNotFound)

    When the model cannot be found with this name.



84
85
86
# File 'lib/vedeu/repositories/repository.rb', line 84

def find!(name)
  find(name) || fail(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 if not found.

Parameters:

  • name (String)


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vedeu/repositories/repository.rb', line 92

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

#in_memoryHash (private)

Returns:

  • (Hash)


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

def in_memory
  {}
end

#inspectString

Returns:

  • (String)


105
106
107
# File 'lib/vedeu/repositories/repository.rb', line 105

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

#log_store(model) ⇒ String (private)

Returns:

  • (String)


176
177
178
179
180
# 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

#registeredArray

Returns a collection of the names of all the registered entities.

Returns:

  • (Array)


112
113
114
115
116
117
118
119
# File 'lib/vedeu/repositories/repository.rb', line 112

def registered
  return [] if empty?

  return storage.keys if storage.is_a?(Hash)
  return storage.to_a if storage.is_a?(Set)

  storage
end

#registered?(name) ⇒ Boolean

Returns a boolean indicating whether the named model is registered.

Parameters:

  • name (String)

Returns:

  • (Boolean)


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

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 if the model does not exist.

Parameters:

  • name (String)

Returns:

  • (Hash|FalseClass)


137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/vedeu/repositories/repository.rb', line 137

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.

Returns:

  • (Class)


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

def repository
  self.class # .name
end

#store(model) ⇒ void Also known as: register

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:

  • (MissingRequired)

    When the name attribute is not defined.



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

def store(model)
  fail MissingRequired, "Cannot store model '#{model.class}' without a " \
                        'name attribute.' unless defined_value?(model.name)

  log_store(model)

  storage[model.name] = model
end