Module: Vedeu::Models::Focus

Extended by:
Focus
Included in:
Focus
Defined in:
lib/vedeu/models/focus.rb

Overview

Note:
  • Interfaces are added to the collection in the order they are defined.

  • If the interface definition contains ‘focus!`, (see Vedeu::DSL::Interface#focus!) then that interface is prepended to the list.

  • If the ‘Vedeu.focus_by_name ’some_interface’‘ declaration is used, then the list pointer (`current`) is set to the interface of that name.

The Focus repository is simply a collection of interface names, this module serving to store and manipulate the which interface is currently being focussed.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(name, focus = false) ⇒ Array

Add an interface name to the focus list unless it is already registered.

Parameters:

  • name (String)

    The name of the interface.

  • focus (Boolean) (defaults to: false)

    When true, prepends the interface name to the collection, making that interface the currently focussed interface.

Returns:

  • (Array)

    The collection of interface names.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vedeu/models/focus.rb', line 28

def add(name, focus = false)
  if registered?(name)
    return storage unless focus

    by_name(name)
    storage

  else
    Vedeu.log(type: :store, message: "Storing focus entry: '#{name}'")

    if focus
      storage.unshift(name)

    else
      storage.push(name)

    end
  end
end

.by_name(name) ⇒ String Also known as: focus_by_name

Focus an interface by name. Used after defining an interface or interfaces to set the initially focussed interface.

Examples:

Vedeu.trigger(:_focus_by_name_, name)
Vedeu.focus_by_name('name')

Parameters:

  • name (String)

    The interface to focus; must be defined.

Returns:

  • (String)

    The name of the interface now in focus.

Raises:



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vedeu/models/focus.rb', line 58

def by_name(name)
  unless registered?(name)
    fail Vedeu::Error::ModelNotFound,
         "Cannot focus '#{name}' as this interface has not been " \
         'registered.'
  end

  storage.rotate!(storage.index(name))

  update
end

.currentString Also known as: focus

Return the interface currently focussed.

Examples:

Vedeu.focus

Returns:

  • (String)


77
78
79
# File 'lib/vedeu/models/focus.rb', line 77

def current
  storage[0]
end

.current?(name) ⇒ Boolean Also known as: focussed?

Returns a boolean indicating whether the named interface is focussed.

Examples:

Vedeu.focussed?(name)

Parameters:

  • name (String)

Returns:

  • (Boolean)


89
90
91
# File 'lib/vedeu/models/focus.rb', line 89

def current?(name)
  current == name
end

.empty?Boolean

Return a boolean indicating whether the storage is empty.

Returns:

  • (Boolean)


97
98
99
# File 'lib/vedeu/models/focus.rb', line 97

def empty?
  storage.empty?
end

.in_memoryArray (private)

Returns an empty collection ready for the storing of interface names.

Returns:

  • (Array)


221
222
223
# File 'lib/vedeu/models/focus.rb', line 221

def in_memory
  []
end

.next_itemString Also known as: next

Put the next interface relative to the current interfaces in focus.

Examples:

Vedeu.trigger(:_focus_next_)
Vedeu.focus_next

Returns:

  • (String)


108
109
110
111
112
# File 'lib/vedeu/models/focus.rb', line 108

def next_item
  storage.rotate!

  update
end

.next_visible_itemString Also known as: focus_next

Put the next visible interface relative to the current interfaces in focus.

Returns:

  • (String)


119
120
121
122
123
124
125
126
127
128
# File 'lib/vedeu/models/focus.rb', line 119

def next_visible_item
  return update unless visible_items?

  loop do
    storage.rotate!
    break if Vedeu.interfaces.by_name(current).visible?
  end

  update
end

.prev_itemString Also known as: prev, previous

Put the previous interface relative to the current interface in focus.

Examples:

Vedeu.trigger(:_focus_prev_)
Vedeu.focus_previous

Returns:

  • (String)


138
139
140
141
142
# File 'lib/vedeu/models/focus.rb', line 138

def prev_item
  storage.rotate!(-1)

  update
end

.prev_visible_itemString Also known as: focus_previous

Put the previous visible interface relative to the current interfaces in focus.

Returns:

  • (String)


150
151
152
153
154
155
156
157
158
159
# File 'lib/vedeu/models/focus.rb', line 150

def prev_visible_item
  return update unless visible_items?

  loop do
    storage.rotate!(-1)
    break if Vedeu.interfaces.by_name(current).visible?
  end

  update
end

.refreshArray

Refresh the interface in focus.

Returns:

  • (Array)


165
166
167
# File 'lib/vedeu/models/focus.rb', line 165

def refresh
  Vedeu.trigger(:_refresh_, current)
end

.registeredArray

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

Returns:

  • (Array)


172
173
174
175
176
# File 'lib/vedeu/models/focus.rb', line 172

def registered
  return [] if empty?

  storage
end

.registered?(name) ⇒ Boolean

Returns a boolean indicating whether the named model is registered.

Parameters:

  • name (String)

Returns:

  • (Boolean)


182
183
184
185
186
# File 'lib/vedeu/models/focus.rb', line 182

def registered?(name)
  return false if empty?

  storage.include?(name)
end

.resetArray|Hash|Set

Reset the repository.

Returns:

  • (Array|Hash|Set)


191
192
193
# File 'lib/vedeu/models/focus.rb', line 191

def reset
  @storage = in_memory
end

.storageArray (private)

Access to the storage for this repository.

Returns:

  • (Array)


214
215
216
# File 'lib/vedeu/models/focus.rb', line 214

def storage
  @storage ||= in_memory
end

.updateString|FalseClass (private)

Return the name of the interface in focus after triggering the refresh event for that interface. Returns false when the storage is empty.

Returns:

  • (String|FalseClass)


201
202
203
204
205
206
207
208
209
# File 'lib/vedeu/models/focus.rb', line 201

def update
  return false if empty?

  Vedeu.log(type: :info, message: "Interface in focus: '#{current}'")

  refresh

  current
end

.visible_items?Boolean (private)

Returns:

  • (Boolean)


226
227
228
# File 'lib/vedeu/models/focus.rb', line 226

def visible_items?
  storage.any? { |name| Vedeu.interfaces.by_name(name).visible? }
end

Instance Method Details

#add(name, focus = false) ⇒ Array

Add an interface name to the focus list unless it is already registered.

Parameters:

  • name (String)

    The name of the interface.

  • focus (Boolean) (defaults to: false)

    When true, prepends the interface name to the collection, making that interface the currently focussed interface.

Returns:

  • (Array)

    The collection of interface names.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vedeu/models/focus.rb', line 28

def add(name, focus = false)
  if registered?(name)
    return storage unless focus

    by_name(name)
    storage

  else
    Vedeu.log(type: :store, message: "Storing focus entry: '#{name}'")

    if focus
      storage.unshift(name)

    else
      storage.push(name)

    end
  end
end

#by_name(name) ⇒ String Also known as: focus_by_name

Focus an interface by name. Used after defining an interface or interfaces to set the initially focussed interface.

Examples:

Vedeu.trigger(:_focus_by_name_, name)
Vedeu.focus_by_name('name')

Parameters:

  • name (String)

    The interface to focus; must be defined.

Returns:

  • (String)

    The name of the interface now in focus.

Raises:



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vedeu/models/focus.rb', line 58

def by_name(name)
  unless registered?(name)
    fail Vedeu::Error::ModelNotFound,
         "Cannot focus '#{name}' as this interface has not been " \
         'registered.'
  end

  storage.rotate!(storage.index(name))

  update
end

#currentString Also known as: focus

Return the interface currently focussed.

Examples:

Vedeu.focus

Returns:

  • (String)


77
78
79
# File 'lib/vedeu/models/focus.rb', line 77

def current
  storage[0]
end

#current?(name) ⇒ Boolean Also known as: focussed?

Returns a boolean indicating whether the named interface is focussed.

Examples:

Vedeu.focussed?(name)

Parameters:

  • name (String)

Returns:

  • (Boolean)


89
90
91
# File 'lib/vedeu/models/focus.rb', line 89

def current?(name)
  current == name
end

#empty?Boolean

Return a boolean indicating whether the storage is empty.

Returns:

  • (Boolean)


97
98
99
# File 'lib/vedeu/models/focus.rb', line 97

def empty?
  storage.empty?
end

#in_memoryArray (private)

Returns an empty collection ready for the storing of interface names.

Returns:

  • (Array)


221
222
223
# File 'lib/vedeu/models/focus.rb', line 221

def in_memory
  []
end

#next_itemString Also known as: next

Put the next interface relative to the current interfaces in focus.

Examples:

Vedeu.trigger(:_focus_next_)
Vedeu.focus_next

Returns:

  • (String)


108
109
110
111
112
# File 'lib/vedeu/models/focus.rb', line 108

def next_item
  storage.rotate!

  update
end

#next_visible_itemString Also known as: focus_next

Put the next visible interface relative to the current interfaces in focus.

Returns:

  • (String)


119
120
121
122
123
124
125
126
127
128
# File 'lib/vedeu/models/focus.rb', line 119

def next_visible_item
  return update unless visible_items?

  loop do
    storage.rotate!
    break if Vedeu.interfaces.by_name(current).visible?
  end

  update
end

#prev_itemString Also known as: prev, previous

Put the previous interface relative to the current interface in focus.

Examples:

Vedeu.trigger(:_focus_prev_)
Vedeu.focus_previous

Returns:

  • (String)


138
139
140
141
142
# File 'lib/vedeu/models/focus.rb', line 138

def prev_item
  storage.rotate!(-1)

  update
end

#prev_visible_itemString Also known as: focus_previous

Put the previous visible interface relative to the current interfaces in focus.

Returns:

  • (String)


150
151
152
153
154
155
156
157
158
159
# File 'lib/vedeu/models/focus.rb', line 150

def prev_visible_item
  return update unless visible_items?

  loop do
    storage.rotate!(-1)
    break if Vedeu.interfaces.by_name(current).visible?
  end

  update
end

#refreshArray

Refresh the interface in focus.

Returns:

  • (Array)


165
166
167
# File 'lib/vedeu/models/focus.rb', line 165

def refresh
  Vedeu.trigger(:_refresh_, current)
end

#registeredArray

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

Returns:

  • (Array)


172
173
174
175
176
# File 'lib/vedeu/models/focus.rb', line 172

def registered
  return [] if empty?

  storage
end

#registered?(name) ⇒ Boolean

Returns a boolean indicating whether the named model is registered.

Parameters:

  • name (String)

Returns:

  • (Boolean)


182
183
184
185
186
# File 'lib/vedeu/models/focus.rb', line 182

def registered?(name)
  return false if empty?

  storage.include?(name)
end

#resetArray|Hash|Set

Reset the repository.

Returns:

  • (Array|Hash|Set)


191
192
193
# File 'lib/vedeu/models/focus.rb', line 191

def reset
  @storage = in_memory
end

#storageArray (private)

Access to the storage for this repository.

Returns:

  • (Array)


214
215
216
# File 'lib/vedeu/models/focus.rb', line 214

def storage
  @storage ||= in_memory
end

#updateString|FalseClass (private)

Return the name of the interface in focus after triggering the refresh event for that interface. Returns false when the storage is empty.

Returns:

  • (String|FalseClass)


201
202
203
204
205
206
207
208
209
# File 'lib/vedeu/models/focus.rb', line 201

def update
  return false if empty?

  Vedeu.log(type: :info, message: "Interface in focus: '#{current}'")

  refresh

  current
end

#visible_items?Boolean (private)

Returns:

  • (Boolean)


226
227
228
# File 'lib/vedeu/models/focus.rb', line 226

def visible_items?
  storage.any? { |name| Vedeu.interfaces.by_name(name).visible? }
end