Class: Controlrepo::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/controlrepo/group.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, members = []) ⇒ Group

You need to pass in an array of strings for members, not objects, it will find the objects by itself, and yes it will reference them, not just create additional ones, woo!



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/controlrepo/group.rb', line 14

def initialize(name = nil, members = [])
  @name = name
  @members = []

  if members.any?
    member_objects = []
    members.each do |member|
      # Try to find the type for each member
      if Controlrepo::Class.find(member)
        member_objects << Controlrepo::Class.find(member)
      elsif Controlrepo::Node.find(member)
        member_objects << Controlrepo::Node.find(member)
      else
        raise "#{member} was not found in the list of nodes or classes!"
      end
    end

    # Check that they are all the same type
    if member_objects.all? { |item| item.is_a?(Controlrepo::Class) }
      type = Controlrepo::Class
    elsif member_objects.all? { |item| item.is_a?(Controlrepo::Node) }
      type = Controlrepo::Node
    else
      raise 'Groups must contain either all nodes or all classes. Either there was a mix, or something was spelled wrong'
    end

    # Smash it into the instance variable
    @members = member_objects
  end

  # Finally add it to the list of all grops
  @@all << self
end

Instance Attribute Details

#membersObject

Returns the value of attribute members.



10
11
12
# File 'lib/controlrepo/group.rb', line 10

def members
  @members
end

#nameObject

Work out how to do class veriables so that I can keep track of all the groups easily



9
10
11
# File 'lib/controlrepo/group.rb', line 9

def name
  @name
end

Class Method Details

.allObject



57
58
59
# File 'lib/controlrepo/group.rb', line 57

def self.all
  @@all
end

.find(group_name) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/controlrepo/group.rb', line 48

def self.find(group_name)
  @@all.each do |group|
    if group.name == group_name
      return group
    end
  end
  nil
end