Class: Squad::Team
- Inherits:
-
Object
- Object
- Squad::Team
- Defined in:
- lib/terraorg/model/squad.rb
Instance Attribute Summary collapse
-
#associates ⇒ Object
Returns the value of attribute associates.
-
#location ⇒ Object
Returns the value of attribute location.
-
#members ⇒ Object
Returns the value of attribute members.
Instance Method Summary collapse
- #everyone ⇒ Object
-
#initialize(parsed_data, people) ⇒ Team
constructor
A new instance of Team.
-
#to_h ⇒ Object
Output a canonical (sorted, formatted) version of this Team.
- #to_md ⇒ Object
- #validate! ⇒ Object
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
#associates ⇒ Object
Returns the value of attribute associates.
24 25 26 |
# File 'lib/terraorg/model/squad.rb', line 24 def associates @associates end |
#location ⇒ Object
Returns the value of attribute location.
24 25 26 |
# File 'lib/terraorg/model/squad.rb', line 24 def location @location end |
#members ⇒ Object
Returns the value of attribute members.
24 25 26 |
# File 'lib/terraorg/model/squad.rb', line 24 def members @members end |
Instance Method Details
#everyone ⇒ Object
61 62 63 |
# File 'lib/terraorg/model/squad.rb', line 61 def everyone @associates + @members end |
#to_h ⇒ Object
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_md ⇒ Object
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 |