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!



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
# File 'lib/controlrepo/group.rb', line 15

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

  if Controlrepo::Group.valid_members?(members)
    # If it's already a valid list just chuck it in there
    @members = members
  elsif members.is_a?(Hash)
    # if it's a hash then do subtractive stiff
    @members = Controlrepo::Group.subtractive_to_list(members)
  else
    # Turn it into a full list
    member_objects = []

    # This should also handle lists that include groups
    members.each { |member| member_objects << Controlrepo::TestConfig.find_list(member) }
    member_objects.flatten!

    # Check that they are all the same type
    unless Controlrepo::Group.valid_members?(member_objects)
      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



55
56
57
# File 'lib/controlrepo/group.rb', line 55

def self.all
  @@all
end

.find(group_name) ⇒ Object



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

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

.subtractive_to_list(subtractive_hash) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/controlrepo/group.rb', line 75

def self.subtractive_to_list(subtractive_hash)
  # Take a hash that looks like this:
  # { 'include' => 'somegroup'
  #   'exclude' => 'other'}
  # and return a list of classes/nodes
  include_list = Controlrepo::TestConfig.find_list(subtractive_hash['include'])
  exclude_list = Controlrepo::TestConfig.find_list(subtractive_hash['exclude'])
  include_list - exclude_list
end

.valid_members?(members) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/controlrepo/group.rb', line 59

def self.valid_members?(members)
  # Check that they are all the same type
  # Also catch any errors to assume it's invalid
  begin
    if members.all? { |item| item.is_a?(Controlrepo::Class) }
      return true
    elsif members.all? { |item| item.is_a?(Controlrepo::Node) }
      return true
    else
      return false
    end
  rescue
    return false
  end
end