Class: Squad::Team

Inherits:
Object
  • Object
show all
Defined in:
lib/terraorg/model/squad.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_data, people) ⇒ Team

Returns a new instance of Team.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/terraorg/model/squad.rb', line 26

def initialize(parsed_data, people)
  location = parsed_data.fetch('location')
  country = ISO3166::Country.new(location)
  raise "Location is invalid: #{location}" unless country
  @location = country.alpha2
  @members = parsed_data.fetch('members', []).map do |n|
    people.get_or_create!(n)
  end
  @associates = parsed_data.fetch('associates', []).map do |n|
    people.get_or_create!(n)
  end
end

Instance Attribute Details

#associatesObject

Returns the value of attribute associates.



24
25
26
# File 'lib/terraorg/model/squad.rb', line 24

def associates
  @associates
end

#locationObject

Returns the value of attribute location.



24
25
26
# File 'lib/terraorg/model/squad.rb', line 24

def location
  @location
end

#membersObject

Returns the value of attribute members.



24
25
26
# File 'lib/terraorg/model/squad.rb', line 24

def members
  @members
end

Instance Method Details

#everyoneObject



61
62
63
# File 'lib/terraorg/model/squad.rb', line 61

def everyone
  @associates + @members
end

#to_hObject

Output a canonical (sorted, formatted) version of this Team.

  • Sort the members in each team

  • Only add an associates field if it’s present



53
54
55
56
57
58
59
# File 'lib/terraorg/model/squad.rb', line 53

def to_h
  {
    'associates' => @associates.map(&:id).sort,
    'location' => @location,
    'members' => @members.map(&:id).sort,
  }
end

#to_mdObject



65
66
67
# File 'lib/terraorg/model/squad.rb', line 65

def to_md
  "**#{@location}**: #{@members.map(&:name).sort.join(', ')}, #{@associates.map { |m| "_#{m.name}_" }.sort.join(', ')}"
end

#validate!Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/terraorg/model/squad.rb', line 39

def validate!
  raise 'Subteam has no full time members' if @members.size == 0
  # location validation done at initialize time
  # associates can be empty

  # associates and members must have zero intersection
  associate_set = Set.new(@associates.map(&:id))
  member_set = Set.new(@members.map(&:id))
  raise 'A member cannot also be an associate of the same team' if associate_set.intersection(member_set)
end