Class: Vedeu::Models::Group

Inherits:
Object
  • Object
show all
Includes:
Vedeu::Model, Toggleable
Defined in:
lib/vedeu/models/group.rb,
lib/vedeu/models/groups.rb

Overview

Groups

Instance Attribute Summary collapse

Attributes included from Toggleable

#visible

Attributes included from Vedeu::Model

#repository

Instance Method Summary collapse

Methods included from Toggleable

included

Methods included from Vedeu::Model

#deputy, #dsl_class, included, #store

Methods included from Common

#demodulize, #present?, #snake_case

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Models::Group

Note:

A group being visible or not may not necessarily mean the members are of the same state.

Return a new instance of Vedeu::Models::Group.

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):

  • members (Set)

    A collection of names of interfaces belonging to this group.

  • name (String)

    The name of the group.

  • repository (Vedeu::Repository)

    The storage for all Group models.

  • visible (Boolean)

    Whether the group is visible or not.



32
33
34
35
36
37
38
# File 'lib/vedeu/models/group.rb', line 32

def initialize(attributes = {})
  @attributes = defaults.merge!(attributes)

  @attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#nameString

Returns:

  • (String)


15
16
17
# File 'lib/vedeu/models/group.rb', line 15

def name
  @name
end

Instance Method Details

#add(member) ⇒ Vedeu::Models::Group

Add a member to the group by name.

Parameters:

  • member (String)

Returns:



44
45
46
47
48
# File 'lib/vedeu/models/group.rb', line 44

def add(member)
  attrs = attributes.merge!(members: members.add(member))

  Vedeu::Models::Group.new(attrs).store
end

#attributesHash<Symbol => void>

Returns the attributes of the group.

Returns:

  • (Hash<Symbol => void>)


53
54
55
56
57
58
59
60
# File 'lib/vedeu/models/group.rb', line 53

def attributes
  {
    name:       name,
    members:    members,
    repository: repository,
    visible:    visible,
  }
end

#by_zindexArray<String>

Return the members of the group sorted by the zindex of the members.

Returns:

  • (Array<String>)


66
67
68
# File 'lib/vedeu/models/group.rb', line 66

def by_zindex
  interfaces.sort { |a, b| a.zindex <=> b.zindex }.map(&:name)
end

#defaultsHash (private)

Returns the default options/attributes for this class.

Returns:

  • (Hash)


155
156
157
158
159
160
161
162
# File 'lib/vedeu/models/group.rb', line 155

def defaults
  {
    members:    Set.new,
    name:       '',
    repository: Vedeu.groups,
    visible:    true,
  }
end

#hideVedeu::Models::Group

Note:

The action of showing a group will effectively clear the terminal and show the new group, therefore hiding the group may not be necessary.

Hide the named group of interfaces, or without a name, the group of the currently focussed interface. Useful for hiding part of that which is currently displaying in the terminal.

Examples:

Vedeu.trigger(:_hide_group_, name)
Vedeu.hide_group(name)

Returns:



83
84
85
86
87
88
89
90
91
# File 'lib/vedeu/models/group.rb', line 83

def hide
  super

  @members.each do |member|
    Vedeu::Models::Interface.hide_interface(member)
  end

  self
end

#interfacesArray<Vedeu::Models::Interface] (private)

Return the interfaces for all members of the group.

Returns:



167
168
169
# File 'lib/vedeu/models/group.rb', line 167

def interfaces
  members.map { |name| Vedeu.interfaces.by_name(name) }
end

#membersSet

Return the members of the group.

Returns:

  • (Set)


96
97
98
# File 'lib/vedeu/models/group.rb', line 96

def members
  @_members ||= Set.new(@members)
end

#remove(member) ⇒ Vedeu::Models::Group

Remove a member from the group by name.

Parameters:

  • member (String)

Returns:



104
105
106
107
108
# File 'lib/vedeu/models/group.rb', line 104

def remove(member)
  attrs = attributes.merge!(members: members.delete(member))

  Vedeu::Models::Group.new(attrs).store
end

#resetVedeu::Models::Group

Remove all members from the group.



113
114
115
116
117
# File 'lib/vedeu/models/group.rb', line 113

def reset
  attrs = defaults.merge!(name: name)

  Vedeu::Models::Group.new(attrs).store
end

#showVedeu::Models::Group

Show the named group of interfaces, or without a name, the group of the currently focussed interface.

Examples:

Vedeu.trigger(:_show_group_, name)
Vedeu.show_group(name)

Returns:



127
128
129
130
131
132
133
134
135
# File 'lib/vedeu/models/group.rb', line 127

def show
  super

  @members.each do |member|
    Vedeu::Models::Interface.show_interface(member)
  end

  self
end

#toggleVedeu::Models::Group

Toggle the visibility of the group with the given name.

Examples:

Vedeu.trigger(:_toggle_group, name)
Vedeu.toggle_group(name)

Returns:



144
145
146
147
148
# File 'lib/vedeu/models/group.rb', line 144

def toggle
  super

  self
end