Method: Onceover::Group#initialize

Defined in:
lib/onceover/group.rb

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

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

  case
  when Onceover::Group.valid_members?(members)
    # If it's already a valid list just chuck it in there
    @members = members
  when members.is_a?(Hash)
    # if it's a hash then do subtractive stuff
    @members = Onceover::TestConfig.subtractive_to_list(members)
  when members.nil?
    # Support empty groups yo
    @members = []
  else
    # Turn it into a full list
    # This should also handle lists that include groups
    member_objects = members.map { |member| Onceover::TestConfig.find_list(member) }
    member_objects.flatten!

    # Check that they are all the same type
    unless Onceover::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