Sha256: f88d647c6d63d47e7536ddd36dd1099e92f964d2141d6bba9a19c883638203a6

Contents?: true

Size: 1.75 KB

Versions: 12

Compression:

Stored size: 1.75 KB

Contents

module Vedeu

  # Interfaces can be associated with one another by being members of the same
  # Group. A Group is a collection of interface names.
  #
  # @api private
  class Group

    include Vedeu::Model

    # @!attribute [rw] name
    # @return [String]
    attr_accessor :name

    # Return a new instance of Vedeu::Group.
    #
    # @param attributes [Hash]
    # @option attributes members [Array] A collection of names of interfaces
    #   belonging to this group.
    # @option attributes name [String] The name of the group.
    # @option attributes repository [Vedeu::Repository] The storage for all
    #   Group models.
    # @return [Group]
    def initialize(attributes = {})
      @attributes = defaults.merge!(attributes)

      @members    = Array(@attributes[:members])
      @name       = @attributes[:name]
      @repository = @attributes[:repository]
    end

    # Add a member to the group by name.
    #
    # @param member [String]
    # @return [Group]
    def add(member)
      @members = members.add(member)

      Vedeu::Group.new(name: name, members: @members).store
    end

    # Return the members as a Set.
    #
    # @return [Set]
    def members
      @members.to_set
    end

    # Remove a member from the group by name.
    #
    # @param member [String]
    # @return [Group]
    def remove(member)
      @members = members.delete(member)

      Vedeu::Group.new(name: name, members: @members).store
    end

    # Remove all members from the group.
    #
    # @return [Group]
    def reset
      Vedeu::Group.new(defaults.merge!(name: name)).store
    end

    private

    # @return [Hash]
    def defaults
      {
        members:    [],
        name:       '',
        repository: Vedeu.groups,
      }
    end

  end # Group

end # Vedeu

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
vedeu-0.4.44 lib/vedeu/models/group.rb
vedeu-0.4.43 lib/vedeu/models/group.rb
vedeu-0.4.42 lib/vedeu/models/group.rb
vedeu-0.4.41 lib/vedeu/models/group.rb
vedeu-0.4.40 lib/vedeu/models/group.rb
vedeu-0.4.39 lib/vedeu/models/group.rb
vedeu-0.4.38 lib/vedeu/models/group.rb
vedeu-0.4.37 lib/vedeu/models/group.rb
vedeu-0.4.36 lib/vedeu/models/group.rb
vedeu-0.4.35 lib/vedeu/models/group.rb
vedeu-0.4.34 lib/vedeu/models/group.rb
vedeu-0.4.33 lib/vedeu/models/group.rb